hatchery-harness-api 0.2.10

Read and operator wire contract for the hatchery harness localhost hosts: grant-filtered reads plus the operator credential/action/session-verb set
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
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
//! Privacy-minimized read wire contract for the harness localhost host.
//!
//! Each loopback TCP connection carries exactly one JSON request frame followed
//! by `\n`. The caller must then half-close its write side; EOF is the request
//! boundary and the host sends exactly one newline-terminated reply. A newline
//! without the write half-close is incomplete and expires categorically as
//! `HarnessReadHostErrorV1::Deadline`.

use std::{collections::BTreeMap, fmt};

pub use hatchery_harness_protocol::{
    HarnessActorV1, HarnessApprovalLevelV1,
    HarnessArtifactRef, HarnessEntityReadScopeV1, HarnessExecutionModeV1,
    HarnessFailureCategoryV1, HarnessIdempotencyRef, HarnessMonitoringVisibilityV1,
    HarnessOperationId,
    HarnessOperationKindV1, HarnessOperationStateV1, HarnessOutcomeUnknownReasonV1,
    HarnessCancelTaskRequestV1, HarnessCreateTaskRequestV1, HarnessDispatchIntentV1,
    HarnessExpectedExecutionSpecRevisionV1, HarnessExecutionSpecId,
    HarnessContinuationOutcomeUnknownReasonV1, HarnessContinuationRef,
    HarnessContinuationStateV1, HarnessDeliveryBundleDigestV1,
    HarnessDeliveryBundleIdV1, HarnessDeliveryBundleRevisionV1, HarnessDeliveryBundleV1,
    HarnessDeliveryBundleSelectionV1, HarnessDeliveryComponentCountV1,
    HarnessDeliveryComponentKindV1, HarnessContextSourceSelectionV1,
    HarnessContextSourceAvailabilityV1,
    HarnessDeliveryManifestDigestV2, HarnessDeliveryRef, HarnessDeliveryStateV1,
    HarnessGrantTargetV1,
    HarnessLaunchAuthorityRefV1, HarnessLaunchPlanRefV1, HarnessMoveTaskRequestV1,
    HarnessOperatorAuthorityV1, HarnessReplaceTaskExecutionSpecRequestV1,
    HarnessReplaceTaskRequestV1, HarnessRequestDigest, HarnessRetryTaskRequestV1,
    HarnessScheduledLaunchRefV2, HarnessScheduleNextRequestV1, HarnessScheduleOutcomeV1,
    HarnessStartTaskRequestV1, HarnessTaskExecutionSpecInputV1,
    HarnessTaskExecutionSpecV1, HarnessTaskExecutionSpecV2,
    HarnessTaskLaunchIssuanceId, HarnessTaskLaunchIssuanceRefV1,
    HarnessTaskReviewPolicyV1,
    HarnessTaskStartOutcomeV1,
    HarnessContextPackLineageV1,
    HarnessReadPermissionsV1, HarnessReconciliationOutcomeV1, HarnessResolvedContextPackReceiptV1,
    HarnessResultDispositionV1,
    HarnessInlineRef, HarnessReceiptRef, HarnessResultRef, HarnessRevision, HarnessRunId, HarnessRunIntentV1,
    HarnessRunFinishOutcomeV1, HarnessRunFinishResultV1,
    HarnessRunGitFactsV1, HarnessRunLifecycleV1,
    HarnessRuntimeIdentityV1, HarnessSelectorV1, HarnessTaskId, HarnessTaskStateV1,
    HarnessTaskCreateResultV1, HarnessTaskMoveResultV1,
    HarnessValidationError, HarnessWorktreeIntentV1, SessionGrantId,
    HARNESS_ARTIFACTS_MAX, HARNESS_BODY_MAX_BYTES,
    HARNESS_CHILD_COUNT_MAX, HARNESS_CHILD_DEPTH_MAX, HARNESS_DEPENDENCIES_MAX,
    HARNESS_CONTEXT_PACK_MAX_BYTES, HARNESS_CONTEXT_PACK_RETAINED_MESSAGES_MAX,
    HARNESS_LINKS_MAX, HARNESS_RESULTS_MAX, HARNESS_TITLE_MAX_BYTES,
    HARNESS_RUN_GIT_PATH_MAX_BYTES,
};
use serde::{Deserialize, Deserializer, Serialize};
use thiserror::Error;

pub const HARNESS_READ_REQUEST_MAX_BYTES: usize = 64 * 1024;
pub const HARNESS_READ_RESPONSE_MAX_BYTES: usize = 1024 * 1024;
pub const HARNESS_READ_LIMIT_MAX: u16 = 256;
pub const HARNESS_ENTITY_PAGE_LIMIT_MAX: u16 = 64;
pub const HARNESS_TIMELINE_PAGE_LIMIT_MAX: u16 = 128;
pub const HARNESS_MONITOR_FACTS_MAX: usize = 128;
pub const HARNESS_OBSERVATION_LABEL_MAX_BYTES: usize = 64;
pub const HARNESS_OBSERVATION_TODO_TEXT_MAX_BYTES: usize = 256;
pub const HARNESS_OBSERVATION_PATH_MAX_BYTES: usize = 1_024;
/// Max bytes for `BlockFactV1::label` (`<authority slug>: <reason>`,
/// producer-truncated to fit) -- sized like
/// `HARNESS_OBSERVATION_TODO_TEXT_MAX_BYTES` rather than
/// `HARNESS_OBSERVATION_LABEL_MAX_BYTES`: a block's `reason` is verbatim
/// provider/gate prose, bounded at 1024 bytes where it is minted (see
/// `hatchery_observation_protocol::OBSERVATION_ACTION_BLOCKED_REASON_MAX_BYTES`),
/// not a short class slug like "bash"/"editor" the 64-byte activity-label
/// bound was sized for.
pub const HARNESS_OBSERVATION_BLOCK_LABEL_MAX_BYTES: usize = 256;
pub const HARNESS_READ_CREDENTIAL_MAX_BYTES: usize = 8 * 1024;
/// Credential audience for harness MCP read grants.
///
/// Wire string kept `gate4agent-harness-mcp-read-v1` until §11.4 wave E
/// (digest/audience cutover). Deferred alias: `hatchery-harness-mcp-read-v1`.
/// Do not rename alone — invalidates issued credentials.
pub const HARNESS_MCP_AUDIENCE: &str = "gate4agent-harness-mcp-read-v1";
/// The harness operator wire is a loopback protocol between processes built
/// from the same tree and rolled together (`hatchery-harness` and its two
/// clients, `hatchery-tui` / `hatchery-tui-light`, via
/// `hatchery-harness-client`). There is exactly one peer shape at a time --
/// no peer that cannot be rebuilt exists -- so the envelope now checks
/// [`BUILD_STAMP`] for exact equality instead of a hand-typed version
/// counter: `BUILD_STAMP` is a content hash of the working tree computed at
/// compile time (see `gate4agent-build-stamp`), so any wire-shape change
/// anywhere in the co-built tree moves it automatically, and a peer built
/// from a byte-identical checkout always matches. An additive read-wire
/// field still just needs `#[serde(default)]`, same as before -- nothing
/// about that discipline changes. A persisted format version (a checkpoint,
/// database schema, or any other record that must survive forward migration
/// across releases) is a different thing entirely and keeps its own
/// explicit, hand-bumped integer.
pub use gate4agent_build_stamp::BUILD_STAMP;
// Realistic multi-pane ceiling with headroom; bounds the harness-side
// per-subscriber HashSet<RuntimeSessionKey> and the connect-time seed burst
// `SubscribeTerminal`'s handler sends immediately after registering (see
// `hatchery-harness-service::runtime`'s `HostCommand::SubscribeTerminal`
// arm).
pub const HARNESS_TERMINAL_SUBSCRIPTION_SESSIONS_MAX: usize = 32;
// Same rationale as immediately above, for `SubscribeAgentStream` -- bounds
// the harness-side per-subscriber HashSet<RuntimeSessionKey> and the
// connect-time seed burst its handler sends immediately after registering.
// A distinct constant from the terminal one even though the value matches:
// the two subscription kinds track separate per-subscriber sets and could
// diverge later without forcing a shared bound.
pub const HARNESS_AGENT_STREAM_SUBSCRIPTION_SESSIONS_MAX: usize = 32;
pub const HARNESS_OPERATOR_REQUEST_MAX_BYTES: usize = 64 * 1024;
pub const HARNESS_OPERATOR_RESPONSE_MAX_BYTES: usize = 1024 * 1024;
pub const HARNESS_OPERATOR_CREDENTIAL_MAX_BYTES: usize = 256;
pub const HARNESS_RUNTIME_INVENTORY_PAGE_LIMIT_MAX: u16 = 64;
pub const HARNESS_NATIVE_SESSION_CATALOG_LIMIT_MAX: u16 = 64;
pub const HARNESS_NATIVE_SESSION_PREVIEW_MESSAGE_LIMIT_MAX: u16 = 24;
pub const HARNESS_NATIVE_SESSION_PREVIEW_TEXT_MAX_BYTES: usize = 4_096;
pub const HARNESS_SESSION_RECORD_DISPLAY_NAME_MAX_BYTES: usize = 256;
pub const HARNESS_SESSION_RECORD_TASK_ID_MAX_BYTES: usize = 128;
pub const HARNESS_PROVIDER_SESSION_ID_MAX_BYTES: usize = 512;
pub const HARNESS_PROVIDER_SESSION_TRANSCRIPT_PATH_MAX_BYTES: usize = 32_768;
pub const HARNESS_LAUNCH_PLAN_PAGE_LIMIT_MAX: u16 = 64;
pub const HARNESS_TASK_LAUNCH_OPTIONS_MAX: usize = 64;
/// Bounds `HarnessTaskLaunchOptionsV1::context_source_exclusions` -- a
/// diagnostic dump of every run `context_source_option` declined to surface,
/// not an operator-facing selection page, so it gets its own smaller ceiling
/// rather than reusing [`HARNESS_TASK_LAUNCH_OPTIONS_MAX`].
pub const HARNESS_CONTEXT_SOURCE_EXCLUSIONS_MAX: usize = 32;
/// Bounds `ContextSourceExclusionV1::NodeIncarnationUnknown`'s own
/// `known_incarnations` list -- the incarnation(s) the runtime inventory
/// currently holds for the excluded run's `node_id`.
pub const CONTEXT_SOURCE_EXCLUSION_KNOWN_INCARNATIONS_MAX: usize = 8;
pub const HARNESS_RUNTIME_SPAWN_PROFILES_MAX: usize = 64;
pub const HARNESS_RUNTIME_LAUNCH_BUNDLES_MAX: usize = 128;
pub const HARNESS_REPOSITORY_PATH_MAX_BYTES: usize = 1_024;
pub const HARNESS_WORKSPACE_FILE_MAX_BYTES: usize = 256 * 1_024;
pub const HARNESS_WORKSPACE_TREE_ENTRIES_MAX: usize = 512;
pub const HARNESS_GIT_STATUS_ENTRIES_MAX: usize = 128;
pub const HARNESS_GIT_RECENT_COMMITS_MAX: usize = 12;
pub const HARNESS_GIT_HISTORY_LIMIT_MAX: u16 = 50;
pub const HARNESS_GIT_DIFF_MAX_BYTES: usize = 512 * 1_024;
pub const HARNESS_GIT_COMMIT_PARENTS_MAX: usize = 32;
pub const HARNESS_GIT_SUMMARY_MAX_BYTES: usize = 1_024;
pub const HARNESS_GIT_IDENTITY_MAX_BYTES: usize = 512;
pub const HARNESS_GIT_TIMESTAMP_MAX_BYTES: usize = 128;
pub const HARNESS_GIT_SIGNER_MAX_BYTES: usize = 1_024;
pub const HARNESS_REVERSE_ATTRIBUTION_LINKS_MAX: usize = 64;
pub const HARNESS_TERMINAL_PAGE_LIMIT_MAX: u16 = 64;
pub const HARNESS_TERMINAL_SCROLLBACK_LINES_MAX: usize = 512;
// Ceiling for one wire terminal frame. Must stay above the node's real maximum
// live frame: PTY_TERMINAL_SCROLLBACK_ROWS_MAX (256) styled rows plus the screen,
// which on a wide, heavily styled terminal can exceed 512 KiB. 2 MiB gives
// headroom so validate() never rejects a legitimate frame while still bounding a
// malformed one.
pub const HARNESS_TERMINAL_FRAME_MAX_BYTES: usize = 2 * 1_024 * 1_024;
// Matches the node's own `MAX_NODE_TEXT_BYTES` (gate4agent-node-protocol):
// this crate has no dependency on that crate, so the bound is mirrored here
// as the wire-level ceiling `WriteSessionInput` is rejected above; the node
// re-checks the same limit authoritatively on its own side regardless.
// `PromptSession`/`PasteSession` reuse this exact constant: the node's own
// `validate_node_text` checks "prompt"/"paste"/"terminal input" against the
// same single `MAX_NODE_TEXT_BYTES` ceiling, not a per-verb one.
pub const HARNESS_SESSION_INPUT_MAX_BYTES: usize = 32 * 1_024;
// Matches the node's own `MAX_NODE_TERMINAL_BYTES` (gate4agent-node-protocol)
// / `gate4agent_types::TERMINAL_BYTES_MAX_BYTES`, mirrored here for the same
// reason as `HARNESS_SESSION_INPUT_MAX_BYTES` above: this is the wire-level
// ceiling `WriteSessionBytes` is rejected above, not a substitute for the
// node's own authoritative re-check.
pub const HARNESS_SESSION_BYTES_MAX_BYTES: usize = 64;
// Matches the node's own `MAX_WORKSPACE_ROOT_BYTES` (gate4agent-node-protocol,
// itself `gate4agent_types::WORKING_DIRECTORY_MAX_BYTES`): the wire-level
// ceiling `HarnessHostPathV1` (a host filesystem path -- workspace/worktree
// root, host-directory-browse cursor) is rejected above. Not shared with
// `HARNESS_REPOSITORY_PATH_MAX_BYTES`: that bound is for repo-relative paths,
// a categorically smaller and differently-shaped value.
pub const HARNESS_HOST_PATH_MAX_BYTES: usize = 32 * 1_024;
// Matches the node's own `MAX_HOST_DIRECTORY_ENTRIES`/
// `MAX_HOST_DIRECTORY_DISPLAY_NAME_BYTES` (gate4agent-node-protocol).
pub const HARNESS_HOST_DIRECTORY_ENTRIES_MAX: usize = 256;
pub const HARNESS_HOST_DIRECTORY_DISPLAY_NAME_MAX_BYTES: usize = 1_024;
// Mirrors `gate4agent-node-protocol::MAX_ACP_CONTROL_ID_BYTES`
// (`gate4agent_types::PROVIDER_EVENT_ID_MAX_BYTES`): this crate has no
// dependency on either (see the doc comment on `HarnessTerminalControlV1`),
// so the ACP setter verbs' `mode_id`/`option_id`/`model_id` and the agent
// stream's `tool_name`/catalog id fields are bounded to the same ceiling the
// provider event stream already validates them against.
pub const HARNESS_AGENT_STREAM_ID_MAX_BYTES: usize = 512;
// Mirrors `gate4agent-node-protocol::MAX_ACP_CONTROL_TEXT_BYTES`
// (`gate4agent_types::PROVIDER_EVENT_TEXT_MAX_BYTES`): free text carried on
// the agent content stream (`Text`, `Thinking`, interaction `prompt`/
// `title`, catalog entry descriptions) and `SetSessionConfigOption`'s
// `value_json`.
pub const HARNESS_AGENT_STREAM_TEXT_MAX_BYTES: usize = 262_144;
// Mirrors `gate4agent-node-protocol::MAX_ACP_INTERACTION_OPTIONS`
// (`gate4agent_types::PROVIDER_CONFIG_OPTION_CHOICES_MAX`): the option list
// on one `InteractionPrompt` chunk, and (the same source constant reused for
// the same reason on the node side) the `choices` list on one
// `ConfigOptions` entry.
pub const HARNESS_AGENT_STREAM_INTERACTION_OPTIONS_MAX: usize = 256;
// Mirrors `gate4agent-node-protocol::MAX_ACP_CATALOG_ENTRIES`
// (`gate4agent_types::PROVIDER_CONFIG_OPTIONS_MAX`): the catalog list on a
// `ModeCatalog`/`ConfigOptions`/`ModelCatalog` chunk.
pub const HARNESS_AGENT_STREAM_CATALOG_ENTRIES_MAX: usize = 256;
// Mirrors `gate4agent_types::PROVIDER_INTERACTION_RESPONSE_MAX_BYTES`: the
// bound on `ResolveInteraction`'s `Answer { text }` payload. This wire does
// not also mirror that source type's `EmptyAnswer` rule (an `Answer` must be
// non-empty only when the live interaction is a `Question`) -- see
// `HarnessProviderInteractionResponseV1`'s own doc comment for why that
// check stays the node's job.
pub const HARNESS_ACP_INTERACTION_RESPONSE_MAX_BYTES: usize = 32_768;

pub const HARNESS_READ_TOOL_IDS: [&str; 8] = [
    "g4a_context_get",
    "g4a_monitor_get",
    "g4a_timeline_read",
    "g4a_tasks_list",
    "g4a_tasks_get",
    "g4a_runs_list",
    "g4a_runs_get",
    "g4a_operation_get",
];

/// Slice D's `g4a_task_create`/`g4a_task_move` (D5) and S10's
/// `g4a_run_finish`. Kept separate from `HARNESS_READ_TOOL_IDS` rather than
/// folded into it -- that array's own test
/// (`all_eight_tool_schemas_are_stable_and_closed`) pins it as exactly the
/// eight read-only tools zipped in `tool_definitions()`'s declared order.
/// `g4a_task_create`/`g4a_task_move` are gated by
/// `grant.task_permissions.create`/`.mutate` respectively. `g4a_run_finish`
/// is the one member of this array gated by no grant permission at all
/// (`expected_allowed_tool_ids` carries it unconditionally, the same way it
/// carries `g4a_context_get` out of `HARNESS_READ_TOOL_IDS`) -- every
/// session needs the ability to report its own work finished regardless of
/// what else its grant allows. D7: this array, and `HARNESS_READ_TOOL_IDS`,
/// are the only two names `tools/call` ever admits; `ResolveInteraction`
/// (or any other name) is refused by name regardless of what a grant's
/// `allowed_tool_ids` claims.
pub const HARNESS_WRITE_TOOL_IDS: [&str; 3] = [
    "g4a_run_finish",
    "g4a_task_create",
    "g4a_task_move",
];

const TOKEN_PREFIX: &str = "g4ah2_";
const OPERATOR_TOKEN_PREFIX: &str = "g4aho_";
const OPERATOR_REQUEST_REF_PREFIX: &str = "hireq_";

#[derive(Clone, Eq, PartialEq)]
pub struct HarnessReadCredential(String);

impl HarnessReadCredential {
    pub fn parse(value: impl Into<String>) -> Result<Self, HarnessReadApiError> {
        let value = value.into();
        validate_credential(&value)?;
        Ok(Self(value))
    }

    pub fn expose(&self) -> &str { &self.0 }
}

impl fmt::Debug for HarnessReadCredential {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("HarnessReadCredential([REDACTED])")
    }
}

impl Serialize for HarnessReadCredential {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where S: serde::Serializer {
        serializer.serialize_str(&self.0)
    }
}

impl<'de> Deserialize<'de> for HarnessReadCredential {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where D: Deserializer<'de> {
        Self::parse(String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
    }
}

#[derive(Clone, Eq, PartialEq)]
pub struct HarnessOperatorCredential(String);

impl HarnessOperatorCredential {
    pub fn parse(value: impl Into<String>) -> Result<Self, HarnessOperatorApiError> {
        let value = value.into();
        validate_operator_credential(&value)?;
        Ok(Self(value))
    }

    pub fn expose(&self) -> &str { &self.0 }
}

impl fmt::Debug for HarnessOperatorCredential {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("HarnessOperatorCredential([REDACTED])")
    }
}

impl Serialize for HarnessOperatorCredential {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where S: serde::Serializer {
        serializer.serialize_str(&self.0)
    }
}

impl<'de> Deserialize<'de> for HarnessOperatorCredential {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where D: Deserializer<'de> {
        Self::parse(String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessOperatorEnvelopeV1 {
    pub build_stamp: String,
    pub credential: HarnessOperatorCredential,
    pub request: HarnessOperatorRequestV1,
}

impl HarnessOperatorEnvelopeV1 {
    /// Builds an envelope carrying this binary's own [`BUILD_STAMP`] -- the
    /// one place every caller in this tree (this crate's own tests,
    /// `hatchery-harness-service`, `hatchery-harness-client`) should
    /// build one from, rather than hand-filling `build_stamp` at each site.
    pub fn new(credential: HarnessOperatorCredential, request: HarnessOperatorRequestV1) -> Self {
        Self {
            build_stamp: BUILD_STAMP.to_string(),
            credential,
            request,
        }
    }

    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if self.build_stamp != BUILD_STAMP {
            return Err(HarnessOperatorApiError::BuildStampMismatch {
                expected: BUILD_STAMP.to_string(),
                received: self.build_stamp.clone(),
            });
        }
        validate_operator_credential(self.credential.expose())?;
        self.request.validate()
    }
}

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
pub struct HarnessOperatorRequestRefV1(String);

impl HarnessOperatorRequestRefV1 {
    pub fn new(value: impl Into<String>) -> Result<Self, HarnessOperatorApiError> {
        let value = value.into();
        validate_operator_request_ref(&value)?;
        Ok(Self(value))
    }

    pub fn as_str(&self) -> &str { &self.0 }

    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        validate_operator_request_ref(&self.0)
    }
}

impl<'de> Deserialize<'de> for HarnessOperatorRequestRefV1 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        Self::new(String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessOperatorIntentV1 {
    pub request_ref: HarnessOperatorRequestRefV1,
    pub submitted_at_unix_ms: u64,
    pub action: HarnessOperatorActionV1,
}

impl HarnessOperatorIntentV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.request_ref.validate()?;
        if self.submitted_at_unix_ms == 0 {
            return Err(HarnessOperatorApiError::InvalidSubmittedAt);
        }
        let authority = HarnessOperatorAuthorityV1 {
            operation_id: HarnessOperationId::new(format!("hop_{}", "0".repeat(24)))
                .map_err(HarnessOperatorApiError::Protocol)?,
            idempotency_ref: HarnessIdempotencyRef::new(format!("hidem_{}", "0".repeat(24)))
                .map_err(HarnessOperatorApiError::Protocol)?,
            actor_id: HarnessSelectorV1::new("harness-operator")
                .map_err(HarnessOperatorApiError::Protocol)?,
            now_unix_ms: self.submitted_at_unix_ms,
        };
        let task_id = HarnessTaskId::new(format!("htask_{}", "0".repeat(24)))
            .map_err(HarnessOperatorApiError::Protocol)?;
        self.action.clone().authorize(authority, task_id).validate()
    }

    pub fn authorize(
        self,
        authority: HarnessOperatorAuthorityV1,
        create_task_id: HarnessTaskId,
    ) -> HarnessOperatorRequestV1 {
        self.action.authorize(authority, create_task_id)
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessOperatorActionV1 {
    CreateTask {
        title: String,
        body: String,
        parent_task_id: Option<HarnessTaskId>,
        dependencies: Vec<HarnessTaskId>,
        initial_state: HarnessTaskStateV1,
    },
    ReplaceTask {
        task_id: HarnessTaskId,
        expected_revision: HarnessRevision,
        title: String,
        body: String,
        parent_task_id: Option<HarnessTaskId>,
        dependencies: Vec<HarnessTaskId>,
    },
    MoveTask {
        task_id: HarnessTaskId,
        expected_revision: HarnessRevision,
        state: HarnessTaskStateV1,
    },
    CancelTask {
        task_id: HarnessTaskId,
        expected_revision: HarnessRevision,
    },
    RetryTask {
        task_id: HarnessTaskId,
        expected_revision: HarnessRevision,
    },
    ScheduleNext {
        plan_id: Option<HarnessSelectorV1>,
    },
    ReplaceTaskExecutionSpec {
        task_id: HarnessTaskId,
        expected_task_revision: HarnessRevision,
        expected_execution_spec_revision: HarnessExpectedExecutionSpecRevisionV1,
        spec: HarnessTaskExecutionSpecInputV1,
    },
    StartTask {
        task_id: HarnessTaskId,
        expected_task_revision: HarnessRevision,
        expected_execution_spec_revision: HarnessRevision,
        expected_scheduled_launch_digest: HarnessRequestDigest,
    },
    ReplaceTaskExecutionSpecV2 {
        task_id: HarnessTaskId,
        expected_task_revision: HarnessRevision,
        expected_execution_spec_revision: HarnessExpectedExecutionSpecRevisionV1,
        selection: HarnessReviewedTaskLaunchSelectionV1,
    },
    StartTaskV2 {
        task_id: HarnessTaskId,
        expected_task_revision: HarnessRevision,
        expected_execution_spec_revision: HarnessRevision,
        expected_launch_issuance: HarnessTaskLaunchIssuanceRefV1,
    },
}

impl HarnessOperatorActionV1 {
    pub fn authorize(
        self,
        authority: HarnessOperatorAuthorityV1,
        create_task_id: HarnessTaskId,
    ) -> HarnessOperatorRequestV1 {
        match self {
            Self::CreateTask {
                title,
                body,
                parent_task_id,
                dependencies,
                initial_state,
            } => HarnessOperatorRequestV1::CreateTask {
                request: HarnessCreateTaskRequestV1 {
                    authority,
                    task_id: create_task_id,
                    title,
                    body,
                    parent_task_id,
                    dependencies,
                    initial_state,
                },
            },
            Self::ReplaceTask {
                task_id,
                expected_revision,
                title,
                body,
                parent_task_id,
                dependencies,
            } => HarnessOperatorRequestV1::ReplaceTask {
                request: HarnessReplaceTaskRequestV1 {
                    authority,
                    task_id,
                    expected_revision,
                    title,
                    body,
                    parent_task_id,
                    dependencies,
                },
            },
            Self::MoveTask { task_id, expected_revision, state } => {
                HarnessOperatorRequestV1::MoveTask {
                    request: HarnessMoveTaskRequestV1 {
                        authority,
                        task_id,
                        expected_revision,
                        state,
                    },
                }
            }
            Self::CancelTask { task_id, expected_revision } => {
                HarnessOperatorRequestV1::CancelTask {
                    request: HarnessCancelTaskRequestV1 {
                        authority,
                        task_id,
                        expected_revision,
                    },
                }
            }
            Self::RetryTask { task_id, expected_revision } => {
                HarnessOperatorRequestV1::RetryTask {
                    request: HarnessRetryTaskRequestV1 {
                        authority,
                        task_id,
                        expected_revision,
                    },
                }
            }
            Self::ScheduleNext { plan_id } => HarnessOperatorRequestV1::ScheduleNext {
                request: HarnessScheduleNextRequestV1 { authority, plan_id },
            },
            Self::ReplaceTaskExecutionSpec {
                task_id,
                expected_task_revision,
                expected_execution_spec_revision,
                spec,
            } => HarnessOperatorRequestV1::ReplaceTaskExecutionSpec {
                request: HarnessReplaceTaskExecutionSpecRequestV1 {
                    authority,
                    task_id,
                    expected_task_revision,
                    expected_execution_spec_revision,
                    spec,
                },
            },
            Self::StartTask {
                task_id,
                expected_task_revision,
                expected_execution_spec_revision,
                expected_scheduled_launch_digest,
            } => HarnessOperatorRequestV1::StartTask {
                request: HarnessStartTaskRequestV1 {
                    authority,
                    task_id,
                    expected_task_revision,
                    expected_execution_spec_revision,
                    expected_scheduled_launch_digest,
                },
            },
            Self::ReplaceTaskExecutionSpecV2 {
                task_id,
                expected_task_revision,
                expected_execution_spec_revision,
                selection,
            } => HarnessOperatorRequestV1::ReplaceTaskExecutionSpecV2 {
                request: HarnessReplaceTaskExecutionSpecRequestV2 {
                    authority,
                    task_id,
                    expected_task_revision,
                    expected_execution_spec_revision,
                    selection,
                },
            },
            Self::StartTaskV2 {
                task_id,
                expected_task_revision,
                expected_execution_spec_revision,
                expected_launch_issuance,
            } => HarnessOperatorRequestV1::StartTaskV2 {
                request: HarnessStartTaskRequestV2 {
                    authority,
                    task_id,
                    expected_task_revision,
                    expected_execution_spec_revision,
                    expected_launch_issuance,
                },
            },
        }
    }

}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessLaunchPlanSummaryV1 {
    pub scheduled_launch: HarnessScheduledLaunchRefV2,
    pub node_id: HarnessSelectorV1,
    pub workspace_id: HarnessSelectorV1,
    pub worktree: HarnessWorktreeIntentV1,
    pub provider_profile: HarnessSelectorV1,
    pub provider_id: HarnessSelectorV1,
    pub mode: HarnessExecutionModeV1,
}

impl HarnessLaunchPlanSummaryV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.scheduled_launch.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.node_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.workspace_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.worktree.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.provider_profile.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.provider_id.validate().map_err(HarnessOperatorApiError::Protocol)
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessLaunchPlanPageV1 {
    pub plans: Vec<HarnessLaunchPlanSummaryV1>,
    pub next_plan_id: Option<HarnessSelectorV1>,
}

impl HarnessLaunchPlanPageV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if self.plans.len() > usize::from(HARNESS_LAUNCH_PLAN_PAGE_LIMIT_MAX) {
            return Err(HarnessOperatorApiError::InvalidLaunchPlans);
        }
        for plan in &self.plans { plan.validate()?; }
        if self.plans.windows(2).any(|plans| {
            plans[0].scheduled_launch.plan.plan_id.as_str()
                >= plans[1].scheduled_launch.plan.plan_id.as_str()
        }) {
            return Err(HarnessOperatorApiError::InvalidLaunchPlans);
        }
        if let Some(next_plan_id) = &self.next_plan_id {
            next_plan_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
            if self.plans.last().map(|plan| &plan.scheduled_launch.plan.plan_id)
                != Some(next_plan_id)
            {
                return Err(HarnessOperatorApiError::InvalidLaunchPlans);
            }
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessOrdinaryLaunchPlanOptionV1 {
    pub plan: HarnessLaunchPlanRefV1,
    pub node_id: HarnessSelectorV1,
    pub source_workspace_id: HarnessSelectorV1,
    pub provider_profile: HarnessSelectorV1,
    pub provider_id: HarnessSelectorV1,
    pub mode: HarnessExecutionModeV1,
}

impl HarnessOrdinaryLaunchPlanOptionV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.plan.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.node_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.source_workspace_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.provider_profile.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.provider_id.validate().map_err(HarnessOperatorApiError::Protocol)
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessManagedWorktreeRetentionV1 {
    RemoveWhenReleased,
    Retain,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessManagedWorktreeProfileOptionV1 {
    pub node_id: HarnessSelectorV1,
    pub node_incarnation: HarnessSelectorV1,
    pub source_workspace_id: HarnessSelectorV1,
    pub profile_id: HarnessSelectorV1,
    pub profile_revision: HarnessSelectorV1,
    pub retention: HarnessManagedWorktreeRetentionV1,
    pub observed_at_unix_ms: u64,
}

impl HarnessManagedWorktreeProfileOptionV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.node_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.node_incarnation.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.source_workspace_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.profile_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.profile_revision.validate().map_err(HarnessOperatorApiError::Protocol)?;
        if self.observed_at_unix_ms == 0 {
            return Err(HarnessOperatorApiError::InvalidTaskLaunchOptions);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessReviewedWorktreeSelectionV1 {
    Existing,
    Managed { profile: HarnessManagedWorktreeProfileOptionV1 },
}

impl HarnessReviewedWorktreeSelectionV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        match self {
            Self::Existing => Ok(()),
            Self::Managed { profile } => profile.validate(),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessReviewedTaskLaunchSelectionV1 {
    pub plan: HarnessOrdinaryLaunchPlanOptionV1,
    pub worktree: HarnessReviewedWorktreeSelectionV1,
    pub context_source: Option<HarnessContextSourceSelectionV1>,
    pub delivery: Option<HarnessDeliveryBundleSelectionV1>,
    pub review_policy: HarnessTaskReviewPolicyV1,
}

impl HarnessReviewedTaskLaunchSelectionV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.plan.validate()?;
        self.worktree.validate()?;
        if let HarnessReviewedWorktreeSelectionV1::Managed { profile } = &self.worktree {
            if profile.node_id != self.plan.node_id
                || profile.source_workspace_id != self.plan.source_workspace_id
            {
                return Err(HarnessOperatorApiError::InvalidTaskLaunchSelection);
            }
        }
        if let Some(context_source) = &self.context_source {
            context_source.validate().map_err(HarnessOperatorApiError::Protocol)?;
        }
        if let Some(delivery) = &self.delivery {
            delivery.validate().map_err(HarnessOperatorApiError::Protocol)?;
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessIssuedExecutionSpecSummaryV1 {
    pub task_id: HarnessTaskId,
    pub execution_spec_id: HarnessExecutionSpecId,
    pub revision: HarnessRevision,
    pub launch_issuance: HarnessTaskLaunchIssuanceRefV1,
    pub review_policy: HarnessTaskReviewPolicyV1,
    pub created_at_unix_ms: u64,
    pub updated_at_unix_ms: u64,
}

impl HarnessIssuedExecutionSpecSummaryV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if self.launch_issuance.revision != self.revision {
            return Err(HarnessOperatorApiError::InvalidTaskLaunchOptions);
        }
        HarnessTaskExecutionSpecV2 {
            execution_spec_id: self.execution_spec_id.clone(),
            revision: self.revision,
            task_id: self.task_id.clone(),
            launch_issuance: self.launch_issuance.clone(),
            review_policy: self.review_policy,
            created_at_unix_ms: self.created_at_unix_ms,
            updated_at_unix_ms: self.updated_at_unix_ms,
        }.validate().map_err(HarnessOperatorApiError::Protocol)
    }
}

/// Names, with its compared inputs, the exact reason `context_source_option`
/// (`hatchery-harness-service::runtime`) declined to surface a run as a
/// `context_sources` candidate. Instrumentation only, never a policy
/// decision -- the admission logic itself is unchanged by this type's
/// existence; it only makes an already-silent exclusion observable from
/// `launch-options` and the harness's own logs (`context source excluded`).
/// One variant per early exit in that function, Live and Durable branches
/// alike; a Live-branch-only or Durable-branch-only condition says so in its
/// own doc comment.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "reason", rename_all = "kebab-case", deny_unknown_fields)]
pub enum ContextSourceExclusionV1 {
    /// The run's `lifecycle` is neither `Running` nor `Waiting`, and it
    /// carries no durable context pack (which would have taken the Durable
    /// branch instead of this lifecycle gate).
    LifecycleNotLive { lifecycle: HarnessRunLifecycleV1 },
    /// The run carries no `binding` at all -- Live or Durable branch alike.
    NoBinding,
    /// The bound session identity is not `Managed` at all (Durable branch),
    /// or (Live branch) it is `Managed` with no `active_session`.
    NoActiveManagedSession,
    /// Live branch only: no entry in the runtime inventory carries this
    /// run's exact `node_id` + `node_incarnation` pair (the Durable branch
    /// re-resolves the node's CURRENT incarnation by `node_id` alone
    /// instead; see `DurableNodeUnknown`). `known_incarnations` names the
    /// incarnation(s) the inventory DOES hold for that `node_id`, bounded at
    /// [`CONTEXT_SOURCE_EXCLUSION_KNOWN_INCARNATIONS_MAX`].
    NodeIncarnationUnknown {
        node_id: HarnessSelectorV1,
        node_incarnation: HarnessSelectorV1,
        known_incarnations: Vec<HarnessSelectorV1>,
    },
    /// The matched node's `managed_sessions` page carries no record whose
    /// `record_id`/`workspace_id`/`active_binding` (workspace, instance,
    /// generation) match the run's binding, and that page was NOT cut for
    /// size -- the record genuinely does not exist on this node right now.
    /// `node_has_records` is the page's own length, for scale.
    ManagedSessionRecordMismatch {
        record_id: HarnessSelectorV1,
        workspace_id: HarnessSelectorV1,
        instance_id: u64,
        generation: u64,
        node_has_records: u32,
    },
    /// Same missing-record shape as `ManagedSessionRecordMismatch`, but the
    /// node's `managed_sessions_truncated` flag was set: the page the
    /// harness received is a PREFIX of the node's full record set, so the
    /// matching record may simply sit past that cut rather than genuinely be
    /// absent. `page_len` is the page's own length, `total` the node's full
    /// `managed_session_count`.
    ManagedSessionsPageTruncated {
        record_id: HarnessSelectorV1,
        page_len: u32,
        total: u32,
    },
    /// Live branch only: `execute_operator_monitor` returned an error for
    /// this run.
    MonitorUnavailable,
    /// Live branch only: the monitor projection resolved but is not live
    /// enough to route an export through right now.
    ProjectionNotLive {
        availability: ProjectionAvailabilityV1,
        freshness: ProjectionFreshnessV1,
        transport_incomplete: bool,
    },
    /// Durable branch only: the pack's `node_id` is not currently known by
    /// the runtime inventory at all (any incarnation), so there is no route
    /// to resolve the pack through right now.
    DurableNodeUnknown { node_id: HarnessSelectorV1 },
}

impl ContextSourceExclusionV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        match self {
            Self::LifecycleNotLive { .. }
            | Self::NoBinding
            | Self::NoActiveManagedSession
            | Self::MonitorUnavailable
            | Self::ProjectionNotLive { .. } => Ok(()),
            Self::NodeIncarnationUnknown { node_id, node_incarnation, known_incarnations } => {
                node_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                node_incarnation.validate().map_err(HarnessOperatorApiError::Protocol)?;
                if known_incarnations.len() > CONTEXT_SOURCE_EXCLUSION_KNOWN_INCARNATIONS_MAX {
                    return Err(HarnessOperatorApiError::InvalidTaskLaunchOptions);
                }
                for incarnation in known_incarnations {
                    incarnation.validate().map_err(HarnessOperatorApiError::Protocol)?;
                }
                Ok(())
            }
            Self::ManagedSessionRecordMismatch {
                record_id, workspace_id, instance_id, generation, ..
            } => {
                record_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                workspace_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                if *instance_id == 0 || *generation == 0 {
                    return Err(HarnessOperatorApiError::InvalidTaskLaunchOptions);
                }
                Ok(())
            }
            Self::ManagedSessionsPageTruncated { record_id, page_len, total } => {
                record_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                if total < page_len {
                    return Err(HarnessOperatorApiError::InvalidTaskLaunchOptions);
                }
                Ok(())
            }
            Self::DurableNodeUnknown { node_id } => {
                node_id.validate().map_err(HarnessOperatorApiError::Protocol)
            }
        }
    }
}

/// One [`ContextSourceExclusionV1`] pinned to the run it was computed for --
/// `HarnessTaskLaunchOptionsV1::context_source_exclusions`'s element type.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ContextSourceExclusionEntryV1 {
    pub run_id: HarnessRunId,
    pub exclusion: ContextSourceExclusionV1,
}

impl ContextSourceExclusionEntryV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.run_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.exclusion.validate()
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessTaskLaunchOptionsV1 {
    pub task_id: HarnessTaskId,
    pub task_revision: HarnessRevision,
    pub policy_digest: HarnessRequestDigest,
    pub plans: Vec<HarnessOrdinaryLaunchPlanOptionV1>,
    pub managed_worktree_profiles: Vec<HarnessManagedWorktreeProfileOptionV1>,
    pub context_sources: Vec<HarnessContextSourceSelectionV1>,
    pub delivery_bundles: Vec<HarnessDeliveryBundleSelectionV1>,
    pub current_issued_spec: Option<HarnessIssuedExecutionSpecSummaryV1>,
    pub truncated: bool,
    /// Resumes specifically the `plans` list's own page (mirrors
    /// `HarnessLaunchPlanPageV1::next_plan_id`) -- `Some` only when `plans`
    /// itself was cut for paging, `None` whenever that list's page is
    /// complete even if `truncated` is `true` for one of the other three
    /// lists. `#[serde(default)]`: additive over the original response
    /// shape, an older caller that never asked for it still deserializes.
    #[serde(default)]
    pub next_after: Option<HarnessSelectorV1>,
    /// Every early exit `context_source_option` took while building
    /// `context_sources` above, one entry per excluded run --
    /// instrumentation only, see [`ContextSourceExclusionV1`]. Bounded
    /// independently of the other four lists at
    /// [`HARNESS_CONTEXT_SOURCE_EXCLUSIONS_MAX`]: their own
    /// `HARNESS_TASK_LAUNCH_OPTIONS_MAX` sizes an operator-facing selection
    /// page, this one sizes a diagnostic dump. `#[serde(default)]`:
    /// additive over the pre-instrumentation wire shape, an older caller
    /// that never asked for it still deserializes.
    #[serde(default)]
    pub context_source_exclusions: Vec<ContextSourceExclusionEntryV1>,
}

impl HarnessTaskLaunchOptionsV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.task_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.task_revision.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.policy_digest.validate().map_err(HarnessOperatorApiError::Protocol)?;
        if self.plans.len() > HARNESS_TASK_LAUNCH_OPTIONS_MAX
            || self.managed_worktree_profiles.len() > HARNESS_TASK_LAUNCH_OPTIONS_MAX
            || self.context_sources.len() > HARNESS_TASK_LAUNCH_OPTIONS_MAX
            || self.delivery_bundles.len() > HARNESS_TASK_LAUNCH_OPTIONS_MAX
            || self.context_source_exclusions.len() > HARNESS_CONTEXT_SOURCE_EXCLUSIONS_MAX
        {
            return Err(HarnessOperatorApiError::InvalidTaskLaunchOptions);
        }
        for plan in &self.plans { plan.validate()?; }
        for profile in &self.managed_worktree_profiles { profile.validate()?; }
        for source in &self.context_sources {
            source.validate().map_err(HarnessOperatorApiError::Protocol)?;
        }
        for delivery in &self.delivery_bundles {
            delivery.validate().map_err(HarnessOperatorApiError::Protocol)?;
        }
        for exclusion in &self.context_source_exclusions {
            exclusion.validate()?;
        }
        if self.context_source_exclusions.windows(2).any(|items| {
            items[0].run_id >= items[1].run_id
        }) {
            return Err(HarnessOperatorApiError::InvalidTaskLaunchOptions);
        }
        if self.plans.windows(2).any(|items| {
            (&items[0].plan.plan_id, items[0].plan.revision)
                >= (&items[1].plan.plan_id, items[1].plan.revision)
        }) || self.managed_worktree_profiles.windows(2).any(|items| {
            (
                &items[0].node_id,
                &items[0].source_workspace_id,
                &items[0].profile_id,
                &items[0].profile_revision,
            ) >= (
                &items[1].node_id,
                &items[1].source_workspace_id,
                &items[1].profile_id,
                &items[1].profile_revision,
            )
        }) || self.context_sources.windows(2).any(|items| {
            (&items[0].source_run_id, items[0].source_run_revision)
                >= (&items[1].source_run_id, items[1].source_run_revision)
        }) || self.delivery_bundles.windows(2).any(|items| {
            (&items[0].bundle.bundle_id, &items[0].bundle.revision)
                >= (&items[1].bundle.bundle_id, &items[1].bundle.revision)
        }) {
            return Err(HarnessOperatorApiError::InvalidTaskLaunchOptions);
        }
        if let Some(current) = &self.current_issued_spec {
            current.validate()?;
            if current.task_id != self.task_id {
                return Err(HarnessOperatorApiError::InvalidTaskLaunchOptions);
            }
        }
        if let Some(next_after) = &self.next_after {
            next_after.validate().map_err(HarnessOperatorApiError::Protocol)?;
            if !self.truncated
                || self.plans.last().map(|plan| &plan.plan.plan_id) != Some(next_after)
            {
                return Err(HarnessOperatorApiError::InvalidTaskLaunchOptions);
            }
        }
        Ok(())
    }

    pub fn validate_for(&self, task_id: &HarnessTaskId) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        if &self.task_id != task_id {
            return Err(HarnessOperatorApiError::InvalidTaskLaunchOptions);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessReplaceTaskExecutionSpecRequestV2 {
    pub authority: HarnessOperatorAuthorityV1,
    pub task_id: HarnessTaskId,
    pub expected_task_revision: HarnessRevision,
    pub expected_execution_spec_revision: HarnessExpectedExecutionSpecRevisionV1,
    pub selection: HarnessReviewedTaskLaunchSelectionV1,
}

impl HarnessReplaceTaskExecutionSpecRequestV2 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.authority.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.task_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.expected_task_revision.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.expected_execution_spec_revision.validate()
            .map_err(HarnessOperatorApiError::Protocol)?;
        self.selection.validate()
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessStartTaskRequestV2 {
    pub authority: HarnessOperatorAuthorityV1,
    pub task_id: HarnessTaskId,
    pub expected_task_revision: HarnessRevision,
    pub expected_execution_spec_revision: HarnessRevision,
    pub expected_launch_issuance: HarnessTaskLaunchIssuanceRefV1,
}

impl HarnessStartTaskRequestV2 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.authority.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.task_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.expected_task_revision.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.expected_execution_spec_revision.validate()
            .map_err(HarnessOperatorApiError::Protocol)?;
        self.expected_launch_issuance.validate().map_err(HarnessOperatorApiError::Protocol)?;
        if self.expected_launch_issuance.revision != self.expected_execution_spec_revision {
            return Err(HarnessOperatorApiError::InvalidTaskLaunchSelection);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
pub struct HarnessRepositoryPathV1(String);

impl HarnessRepositoryPathV1 {
    pub fn new(value: impl Into<String>) -> Result<Self, HarnessOperatorApiError> {
        let value = value.into();
        validate_repository_path(&value)?;
        Ok(Self(value))
    }

    pub fn as_str(&self) -> &str { &self.0 }

    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        validate_repository_path(&self.0)
    }
}

impl<'de> Deserialize<'de> for HarnessRepositoryPathV1 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where D: Deserializer<'de> {
        Self::new(String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
    }
}

/// A bounded, UTF-8 host filesystem path: a workspace/worktree root, a
/// worktree target, or a host-directory-browse cursor (`directory`/`after`/
/// `parent`/`next_after`). Mirrors the node's own `OpaqueHostPath` narrowed
/// to its UTF-8 representation only, the same way `HarnessRepositoryPathV1`
/// narrows the node's dual-representation `RepositoryPath` -- see
/// `repository_path_from_api`/`project_repository_path` in
/// `hatchery-harness-service` for that precedent: a genuinely non-UTF-8
/// host path from the node is a projection failure, never silently
/// lossy-displayed. Validation intentionally stays as minimal as the node's
/// own `validate_opaque_host_path` (non-empty, bounded, no NUL byte): unlike
/// a repository-relative path, an absolute host path legitimately contains
/// backslashes, drive letters, and colons (Windows), so this type imposes no
/// format assumptions beyond that.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
pub struct HarnessHostPathV1(String);

impl HarnessHostPathV1 {
    pub fn new(value: impl Into<String>) -> Result<Self, HarnessOperatorApiError> {
        let value = value.into();
        validate_host_path(&value)?;
        Ok(Self(value))
    }

    pub fn as_str(&self) -> &str { &self.0 }

    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        validate_host_path(&self.0)
    }
}

impl<'de> Deserialize<'de> for HarnessHostPathV1 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where D: Deserializer<'de> {
        Self::new(String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
    }
}

fn validate_host_path(value: &str) -> Result<(), HarnessOperatorApiError> {
    if value.is_empty() || value.len() > HARNESS_HOST_PATH_MAX_BYTES || value.contains('\0') {
        return Err(HarnessOperatorApiError::InvalidHostPath);
    }
    Ok(())
}

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
pub struct HarnessGitObjectIdV1(String);

impl HarnessGitObjectIdV1 {
    pub fn new(value: impl Into<String>) -> Result<Self, HarnessOperatorApiError> {
        let value = value.into();
        validate_git_object_id(&value)?;
        Ok(Self(value))
    }

    pub fn as_str(&self) -> &str { &self.0 }

    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        validate_git_object_id(&self.0)
    }
}

impl<'de> Deserialize<'de> for HarnessGitObjectIdV1 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where D: Deserializer<'de> {
        Self::new(String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
    }
}

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
pub struct HarnessWorkspaceFileRevisionV1(String);

impl HarnessWorkspaceFileRevisionV1 {
    pub fn new(value: impl Into<String>) -> Result<Self, HarnessOperatorApiError> {
        let value = value.into();
        if value.len() != 64 || !value.bytes().all(is_lower_hex) {
            return Err(HarnessOperatorApiError::InvalidWorkspaceFile);
        }
        Ok(Self(value))
    }

    pub fn as_str(&self) -> &str { &self.0 }

    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if self.0.len() != 64 || !self.0.bytes().all(is_lower_hex) {
            return Err(HarnessOperatorApiError::InvalidWorkspaceFile);
        }
        Ok(())
    }
}

impl<'de> Deserialize<'de> for HarnessWorkspaceFileRevisionV1 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where D: Deserializer<'de> {
        Self::new(String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRunWorkspaceOriginV1 {
    pub run_id: HarnessRunId,
    pub run_revision: HarnessRevision,
    pub node_id: HarnessSelectorV1,
    pub node_incarnation_id: HarnessNodeIncarnationV1,
    pub workspace_id: HarnessSelectorV1,
}

impl HarnessRunWorkspaceOriginV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.run_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.run_revision.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.node_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.node_incarnation_id.validate()?;
        self.workspace_id.validate().map_err(HarnessOperatorApiError::Protocol)
    }

    pub fn validate_for(&self, run_id: &HarnessRunId) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        if &self.run_id != run_id {
            return Err(HarnessOperatorApiError::InvalidWorkspaceOrigin);
        }
        Ok(())
    }
}

/// Node-scoped sibling of `HarnessRunWorkspaceOriginV1`: identifies a
/// workspace read routed straight from a node/workspace pair, with no run
/// binding to descend from.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNodeWorkspaceOriginV1 {
    pub node_id: String,
    pub node_incarnation_id: String,
    pub workspace_id: String,
}

impl HarnessNodeWorkspaceOriginV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if !valid_runtime_id(&self.node_id, 128)
            || self.node_incarnation_id.len() != 32
            || !self.node_incarnation_id.bytes().all(is_lower_hex)
            || !valid_runtime_id(&self.workspace_id, 128)
        {
            return Err(HarnessOperatorApiError::InvalidWorkspaceOrigin);
        }
        Ok(())
    }

    pub fn validate_for(
        &self,
        node_id: &str,
        workspace_id: &str,
    ) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        if self.node_id != node_id || self.workspace_id != workspace_id {
            return Err(HarnessOperatorApiError::InvalidWorkspaceOrigin);
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessWorkspaceEntryKindV1 {
    File,
    Directory,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessWorkspaceTreeEntryV1 {
    pub relative_path: HarnessRepositoryPathV1,
    pub kind: HarnessWorkspaceEntryKindV1,
}

impl HarnessWorkspaceTreeEntryV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.relative_path.validate()
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessGitStatusCodeV1 {
    Unmodified,
    Added,
    Modified,
    Deleted,
    Renamed,
    Copied,
    Unmerged,
    Untracked,
    Ignored,
    TypeChanged,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessGitStatusEntryV1 {
    pub index_status: HarnessGitStatusCodeV1,
    pub worktree_status: HarnessGitStatusCodeV1,
    pub path: HarnessRepositoryPathV1,
    pub previous_path: Option<HarnessRepositoryPathV1>,
}

impl HarnessGitStatusEntryV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.path.validate()?;
        if let Some(previous_path) = &self.previous_path {
            previous_path.validate()?;
            if previous_path == &self.path {
                return Err(HarnessOperatorApiError::InvalidGitStatus);
            }
        }
        let renamed_or_copied = matches!(self.index_status, HarnessGitStatusCodeV1::Renamed | HarnessGitStatusCodeV1::Copied)
            || matches!(self.worktree_status, HarnessGitStatusCodeV1::Renamed | HarnessGitStatusCodeV1::Copied);
        if self.previous_path.is_some() != renamed_or_copied {
            return Err(HarnessOperatorApiError::InvalidGitStatus);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessGitCommitSummaryV1 {
    pub id: HarnessGitObjectIdV1,
    pub summary: String,
}

impl HarnessGitCommitSummaryV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.id.validate()?;
        validate_git_single_line(&self.summary, HARNESS_GIT_SUMMARY_MAX_BYTES, false)
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessGitSummaryV1 {
    pub is_repository: bool,
    pub branch: Option<String>,
    pub status: Vec<HarnessGitStatusEntryV1>,
    pub recent_commits: Vec<HarnessGitCommitSummaryV1>,
    pub truncated: bool,
}

impl HarnessGitSummaryV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if self.status.len() > HARNESS_GIT_STATUS_ENTRIES_MAX
            || self.recent_commits.len() > HARNESS_GIT_RECENT_COMMITS_MAX
            || !self.is_repository && (self.branch.is_some() || !self.status.is_empty() || !self.recent_commits.is_empty() || self.truncated)
        {
            return Err(HarnessOperatorApiError::InvalidGitSummary);
        }
        if let Some(branch) = &self.branch {
            validate_git_single_line(branch, HARNESS_REPOSITORY_PATH_MAX_BYTES, true)?;
        }
        for status in &self.status { status.validate()?; }
        if self.status.windows(2).any(|entries| entries[0].path >= entries[1].path) {
            return Err(HarnessOperatorApiError::InvalidGitStatus);
        }
        for commit in &self.recent_commits { commit.validate()?; }
        if self.recent_commits.iter().enumerate().any(|(index, commit)| {
            self.recent_commits[..index].iter().any(|existing| existing.id == commit.id)
        }) {
            return Err(HarnessOperatorApiError::InvalidGitSummary);
        }
        Ok(())
    }
}

/// Additive mirror of `gate4agent_node_protocol::WorkspaceInspectionTruncationV1`
/// carried onto both `HarnessRunWorkspaceInspectionV1` and
/// `HarnessNodeWorkspaceInspectionV1` — see that type for field meaning.
/// Kept as this crate's own type (rather than reused directly) to match
/// every other leaf field on these two structs, which project into
/// `Harness*V1` types rather than exposing node-protocol/c2-protocol types
/// on the operator-facing API surface.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessWorkspaceInspectionTruncationV1 {
    pub walk_time_budget_exceeded: bool,
    pub walk_entry_cap_exceeded: bool,
    pub git_time_budget_exceeded: bool,
    pub entries_visited: u64,
    pub elapsed_ms: u64,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRunWorkspaceInspectionV1 {
    pub origin: HarnessRunWorkspaceOriginV1,
    pub entries: Vec<HarnessWorkspaceTreeEntryV1>,
    pub tree_truncated: bool,
    pub git: HarnessGitSummaryV1,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub truncation: Option<HarnessWorkspaceInspectionTruncationV1>,
}

impl HarnessRunWorkspaceInspectionV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.origin.validate()?;
        if self.entries.len() > HARNESS_WORKSPACE_TREE_ENTRIES_MAX {
            return Err(HarnessOperatorApiError::InvalidWorkspaceTree);
        }
        for entry in &self.entries { entry.validate()?; }
        if self.entries.windows(2).any(|entries| entries[0].relative_path >= entries[1].relative_path) {
            return Err(HarnessOperatorApiError::InvalidWorkspaceTree);
        }
        self.git.validate()
    }

    pub fn validate_for(&self, run_id: &HarnessRunId) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        self.origin.validate_for(run_id)
    }
}

/// Node-scoped sibling of `HarnessRunWorkspaceInspectionV1`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNodeWorkspaceInspectionV1 {
    pub origin: HarnessNodeWorkspaceOriginV1,
    pub entries: Vec<HarnessWorkspaceTreeEntryV1>,
    pub tree_truncated: bool,
    pub git: HarnessGitSummaryV1,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub truncation: Option<HarnessWorkspaceInspectionTruncationV1>,
}

impl HarnessNodeWorkspaceInspectionV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.origin.validate()?;
        if self.entries.len() > HARNESS_WORKSPACE_TREE_ENTRIES_MAX {
            return Err(HarnessOperatorApiError::InvalidWorkspaceTree);
        }
        for entry in &self.entries { entry.validate()?; }
        if self.entries.windows(2).any(|entries| entries[0].relative_path >= entries[1].relative_path) {
            return Err(HarnessOperatorApiError::InvalidWorkspaceTree);
        }
        self.git.validate()
    }

    pub fn validate_for(
        &self,
        node_id: &str,
        workspace_id: &str,
    ) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        self.origin.validate_for(node_id, workspace_id)
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessWorkspaceFileContentV1 {
    Utf8 { text: String, byte_len: u32 },
    NonUtf8 { byte_len: u32 },
    TooLarge { limit_bytes: u32 },
}

impl HarnessWorkspaceFileContentV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        match self {
            Self::Utf8 { text, byte_len } => {
                if text.len() > HARNESS_WORKSPACE_FILE_MAX_BYTES || usize::try_from(*byte_len).ok() != Some(text.len()) {
                    return Err(HarnessOperatorApiError::InvalidWorkspaceFile);
                }
            }
            Self::NonUtf8 { byte_len } => {
                if usize::try_from(*byte_len).map_or(true, |length| length > HARNESS_WORKSPACE_FILE_MAX_BYTES) {
                    return Err(HarnessOperatorApiError::InvalidWorkspaceFile);
                }
            }
            Self::TooLarge { limit_bytes } => {
                if usize::try_from(*limit_bytes).ok() != Some(HARNESS_WORKSPACE_FILE_MAX_BYTES) {
                    return Err(HarnessOperatorApiError::InvalidWorkspaceFile);
                }
            }
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRunWorkspaceFileV1 {
    pub origin: HarnessRunWorkspaceOriginV1,
    pub path: HarnessRepositoryPathV1,
    pub content: HarnessWorkspaceFileContentV1,
    pub revision: Option<HarnessWorkspaceFileRevisionV1>,
}

impl HarnessRunWorkspaceFileV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.origin.validate()?;
        self.path.validate()?;
        self.content.validate()?;
        if let Some(revision) = &self.revision { revision.validate()?; }
        if !matches!(&self.content, HarnessWorkspaceFileContentV1::Utf8 { .. }) && self.revision.is_some() {
            return Err(HarnessOperatorApiError::InvalidWorkspaceFile);
        }
        Ok(())
    }


    pub fn validate_for(
        &self,
        run_id: &HarnessRunId,
        path: &HarnessRepositoryPathV1,
    ) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        self.origin.validate_for(run_id)?;
        if &self.path != path {
            return Err(HarnessOperatorApiError::InvalidWorkspaceFile);
        }
        Ok(())
    }
}

/// Node-scoped sibling of `HarnessRunWorkspaceFileV1`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNodeWorkspaceFileV1 {
    pub origin: HarnessNodeWorkspaceOriginV1,
    pub path: HarnessRepositoryPathV1,
    pub content: HarnessWorkspaceFileContentV1,
    pub revision: Option<HarnessWorkspaceFileRevisionV1>,
}

impl HarnessNodeWorkspaceFileV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.origin.validate()?;
        self.path.validate()?;
        self.content.validate()?;
        if let Some(revision) = &self.revision { revision.validate()?; }
        if !matches!(&self.content, HarnessWorkspaceFileContentV1::Utf8 { .. }) && self.revision.is_some() {
            return Err(HarnessOperatorApiError::InvalidWorkspaceFile);
        }
        Ok(())
    }

    pub fn validate_for(
        &self,
        node_id: &str,
        workspace_id: &str,
        path: &HarnessRepositoryPathV1,
    ) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        self.origin.validate_for(node_id, workspace_id)?;
        if &self.path != path {
            return Err(HarnessOperatorApiError::InvalidWorkspaceFile);
        }
        Ok(())
    }
}

/// Response payload for `CreateNodeWorkspaceDirectory`: the one node-scoped
/// creation response (`WorkspaceDirectoryCreated`) that carries a
/// `WorkspaceEntry` instead of a `WorkspaceFileRead` -- `HarnessNodeWorkspace
/// FileV1` above covers `WriteNodeWorkspaceFile`/`CreateNodeWorkspaceFile`,
/// both of which echo a file. Reuses `HarnessWorkspaceTreeEntryV1`, already
/// the entry shape inside `HarnessNodeWorkspaceInspectionV1`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNodeWorkspaceDirectoryV1 {
    pub origin: HarnessNodeWorkspaceOriginV1,
    pub entry: HarnessWorkspaceTreeEntryV1,
}

impl HarnessNodeWorkspaceDirectoryV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.origin.validate()?;
        self.entry.validate()?;
        if self.entry.kind != HarnessWorkspaceEntryKindV1::Directory {
            return Err(HarnessOperatorApiError::InvalidWorkspaceTree);
        }
        Ok(())
    }

    pub fn validate_for(
        &self,
        node_id: &str,
        workspace_id: &str,
        path: &HarnessRepositoryPathV1,
    ) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        self.origin.validate_for(node_id, workspace_id)?;
        if &self.entry.relative_path != path {
            return Err(HarnessOperatorApiError::InvalidWorkspaceTree);
        }
        Ok(())
    }
}

/// One entry in a `BrowseHostDirectories` page. Mirrors the node's own
/// `HostDirectoryEntry` field-for-field; that type's own constructor already
/// rejects a non-UTF-8 `path`, so the node never actually produces the case
/// `HarnessHostPathV1` would reject here.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessHostDirectoryEntryV1 {
    pub path: HarnessHostPathV1,
    pub display_name: String,
    pub is_link: bool,
}

impl HarnessHostDirectoryEntryV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.path.validate()?;
        if self.display_name.is_empty()
            || self.display_name.len() > HARNESS_HOST_DIRECTORY_DISPLAY_NAME_MAX_BYTES
            || self.display_name.chars().any(char::is_control)
        {
            return Err(HarnessOperatorApiError::InvalidHostDirectoryBrowseRequest);
        }
        Ok(())
    }
}

/// `BrowseHostDirectories`'s reply: mirrors the node's own
/// `HostDirectoryListing` field-for-field. Entries are deliberately not
/// asserted to be sorted (unlike most other bounded collections on this
/// wire, e.g. `HarnessWorkspaceTreeEntryV1`): a live directory listing has no
/// such guarantee from the underlying filesystem, so imposing one here would
/// reject a legitimate page.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessHostDirectoryListingV1 {
    pub directory: Option<HarnessHostPathV1>,
    pub parent: Option<HarnessHostPathV1>,
    pub entries: Vec<HarnessHostDirectoryEntryV1>,
    pub next_after: Option<HarnessHostPathV1>,
    /// True only when another page of supported directory entries is
    /// available.
    pub incomplete: bool,
}

impl HarnessHostDirectoryListingV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if let Some(directory) = &self.directory { directory.validate()?; }
        if let Some(parent) = &self.parent { parent.validate()?; }
        if self.entries.len() > HARNESS_HOST_DIRECTORY_ENTRIES_MAX {
            return Err(HarnessOperatorApiError::InvalidHostDirectoryBrowseRequest);
        }
        for entry in &self.entries { entry.validate()?; }
        if let Some(next_after) = &self.next_after { next_after.validate()?; }
        Ok(())
    }
}

/// Mirrors the node's own `WorktreeServiceMode`.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessWorktreeServiceModeV1 {
    Manual,
    Managed,
    Off,
}

/// `RegisterWorkspace`/`CreateStandaloneWorkspace`'s reply, and half of
/// `CreateWorktree`'s. A deliberately narrowed projection of the node's own
/// `WorkspaceSnapshot`: `sessions` is dropped (redundant with the runtime
/// inventory, which every roster-affecting resource mutation already
/// invalidates on success -- see `HarnessOperatorRequestV1::RegisterWorkspace`'s
/// doc comment) and `managed_worktree_profiles` is dropped (`SpawnManagedWorktree`
/// has no typed operator verb -- see that request variant's own doc comment
/// for why -- so a profile-catalog listing here would serve no reachable
/// caller). `workspace_id` stays a bounded opaque string rather than a typed
/// selector, matching every other node-scoped workspace id on this wire
/// (`HarnessNodeWorkspaceOriginV1.workspace_id`, etc).
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessWorkspaceSnapshotV1 {
    pub workspace_id: String,
    pub canonical_root: HarnessHostPathV1,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub worktree_service_mode: Option<HarnessWorktreeServiceModeV1>,
}

impl HarnessWorkspaceSnapshotV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if !valid_runtime_id(&self.workspace_id, 128) {
            return Err(HarnessOperatorApiError::InvalidResourceMutationRequest);
        }
        self.canonical_root.validate()
    }
}

/// The other half of `CreateWorktree`'s reply. Mirrors the node's own
/// `GitWorktreeSnapshot`, minus `lock_reason`/`prunable_reason` (free-text
/// reason strings the node's own C2-facing mirror, `C2GitWorktreeSnapshot`,
/// already drops -- this wire never receives them to begin with).
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessGitWorktreeSnapshotV1 {
    pub path: HarnessHostPathV1,
    pub head: String,
    pub branch: Option<String>,
    pub is_bare: bool,
    pub is_main: bool,
    pub locked: bool,
    pub prunable: bool,
    pub workspace_id: Option<String>,
}

impl HarnessGitWorktreeSnapshotV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.path.validate()?;
        if !valid_native_single_line(&self.head, HARNESS_GIT_SUMMARY_MAX_BYTES, true) {
            return Err(HarnessOperatorApiError::InvalidResourceMutationRequest);
        }
        if let Some(branch) = &self.branch {
            if !valid_native_single_line(branch, HARNESS_REPOSITORY_PATH_MAX_BYTES, false) {
                return Err(HarnessOperatorApiError::InvalidResourceMutationRequest);
            }
        }
        if let Some(workspace_id) = &self.workspace_id {
            if !valid_runtime_id(workspace_id, 128) {
                return Err(HarnessOperatorApiError::InvalidResourceMutationRequest);
            }
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessGitSignatureStatusV1 {
    Good,
    Bad,
    UnknownValidity,
    ExpiredSignature,
    ExpiredKey,
    RevokedKey,
    CannotCheck,
    NoSignature,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessGitCommitV1 {
    pub id: HarnessGitObjectIdV1,
    pub parents: Vec<HarnessGitObjectIdV1>,
    pub subject: String,
    pub author_name: String,
    pub authored_at: String,
    pub committer_name: String,
    pub committed_at: String,
    pub signature_status: HarnessGitSignatureStatusV1,
    pub signer: Option<String>,
}

impl HarnessGitCommitV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.id.validate()?;
        if self.parents.len() > HARNESS_GIT_COMMIT_PARENTS_MAX {
            return Err(HarnessOperatorApiError::InvalidGitHistory);
        }
        for parent in &self.parents { parent.validate()?; }
        if self.parents.iter().any(|parent| parent == &self.id) {
            return Err(HarnessOperatorApiError::InvalidGitHistory);
        }
        validate_git_single_line(&self.subject, HARNESS_GIT_SUMMARY_MAX_BYTES, false)?;
        validate_git_single_line(&self.author_name, HARNESS_GIT_IDENTITY_MAX_BYTES, false)?;
        validate_git_single_line(&self.authored_at, HARNESS_GIT_TIMESTAMP_MAX_BYTES, true)?;
        validate_git_single_line(&self.committer_name, HARNESS_GIT_IDENTITY_MAX_BYTES, false)?;
        validate_git_single_line(&self.committed_at, HARNESS_GIT_TIMESTAMP_MAX_BYTES, true)?;
        if let Some(signer) = &self.signer {
            validate_git_single_line(signer, HARNESS_GIT_SIGNER_MAX_BYTES, true)?;
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRunGitHistoryPageV1 {
    pub origin: HarnessRunWorkspaceOriginV1,
    pub path: Option<HarnessRepositoryPathV1>,
    pub commits: Vec<HarnessGitCommitV1>,
    pub next_before: Option<HarnessGitObjectIdV1>,
    pub truncated: bool,
}

impl HarnessRunGitHistoryPageV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.origin.validate()?;
        if let Some(path) = &self.path { path.validate()?; }
        if self.commits.len() > usize::from(HARNESS_GIT_HISTORY_LIMIT_MAX) {
            return Err(HarnessOperatorApiError::InvalidGitHistory);
        }
        for commit in &self.commits { commit.validate()?; }
        if self.commits.iter().enumerate().any(|(index, commit)| {
            self.commits[..index].iter().any(|existing| existing.id == commit.id)
        }) {
            return Err(HarnessOperatorApiError::InvalidGitHistory);
        }
        if let Some(next_before) = &self.next_before {
            next_before.validate()?;
            if self.commits.last().map(|commit| &commit.id) != Some(next_before) {
                return Err(HarnessOperatorApiError::InvalidGitHistory);
            }
        }
        Ok(())
    }


    pub fn validate_for(
        &self,
        run_id: &HarnessRunId,
        path: Option<&HarnessRepositoryPathV1>,
        limit: u16,
    ) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        self.origin.validate_for(run_id)?;
        if self.path.as_ref() != path || self.commits.len() > usize::from(limit) {
            return Err(HarnessOperatorApiError::InvalidGitHistory);
        }
        Ok(())
    }
}

/// Node-scoped sibling of `HarnessRunGitHistoryPageV1`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNodeGitHistoryPageV1 {
    pub origin: HarnessNodeWorkspaceOriginV1,
    pub path: Option<HarnessRepositoryPathV1>,
    pub commits: Vec<HarnessGitCommitV1>,
    pub next_before: Option<HarnessGitObjectIdV1>,
    pub truncated: bool,
}

impl HarnessNodeGitHistoryPageV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.origin.validate()?;
        if let Some(path) = &self.path { path.validate()?; }
        if self.commits.len() > usize::from(HARNESS_GIT_HISTORY_LIMIT_MAX) {
            return Err(HarnessOperatorApiError::InvalidGitHistory);
        }
        for commit in &self.commits { commit.validate()?; }
        if self.commits.iter().enumerate().any(|(index, commit)| {
            self.commits[..index].iter().any(|existing| existing.id == commit.id)
        }) {
            return Err(HarnessOperatorApiError::InvalidGitHistory);
        }
        if let Some(next_before) = &self.next_before {
            next_before.validate()?;
            if self.commits.last().map(|commit| &commit.id) != Some(next_before) {
                return Err(HarnessOperatorApiError::InvalidGitHistory);
            }
        }
        Ok(())
    }

    pub fn validate_for(
        &self,
        node_id: &str,
        workspace_id: &str,
        path: Option<&HarnessRepositoryPathV1>,
        limit: u16,
    ) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        self.origin.validate_for(node_id, workspace_id)?;
        if self.path.as_ref() != path || self.commits.len() > usize::from(limit) {
            return Err(HarnessOperatorApiError::InvalidGitHistory);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessGitDiffModeV1 {
    Working,
    Staged,
    Commit { revision: HarnessGitObjectIdV1 },
}

impl HarnessGitDiffModeV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        match self {
            Self::Working | Self::Staged => Ok(()),
            Self::Commit { revision } => revision.validate(),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRunGitDiffV1 {
    pub origin: HarnessRunWorkspaceOriginV1,
    pub mode: HarnessGitDiffModeV1,
    pub path: Option<HarnessRepositoryPathV1>,
    pub text: String,
    pub truncated: bool,
}

impl HarnessRunGitDiffV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.origin.validate()?;
        self.mode.validate()?;
        if let Some(path) = &self.path { path.validate()?; }
        if self.text.len() > HARNESS_GIT_DIFF_MAX_BYTES {
            return Err(HarnessOperatorApiError::InvalidGitDiff);
        }
        Ok(())
    }


    pub fn validate_for(
        &self,
        run_id: &HarnessRunId,
        mode: &HarnessGitDiffModeV1,
        path: Option<&HarnessRepositoryPathV1>,
    ) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        self.origin.validate_for(run_id)?;
        if &self.mode != mode || self.path.as_ref() != path {
            return Err(HarnessOperatorApiError::InvalidGitDiff);
        }
        Ok(())
    }
}

/// Node-scoped sibling of `HarnessRunGitDiffV1`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNodeGitDiffV1 {
    pub origin: HarnessNodeWorkspaceOriginV1,
    pub mode: HarnessGitDiffModeV1,
    pub path: Option<HarnessRepositoryPathV1>,
    pub text: String,
    pub truncated: bool,
}

impl HarnessNodeGitDiffV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.origin.validate()?;
        self.mode.validate()?;
        if let Some(path) = &self.path { path.validate()?; }
        if self.text.len() > HARNESS_GIT_DIFF_MAX_BYTES {
            return Err(HarnessOperatorApiError::InvalidGitDiff);
        }
        Ok(())
    }

    pub fn validate_for(
        &self,
        node_id: &str,
        workspace_id: &str,
        mode: &HarnessGitDiffModeV1,
        path: Option<&HarnessRepositoryPathV1>,
    ) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        self.origin.validate_for(node_id, workspace_id)?;
        if &self.mode != mode || self.path.as_ref() != path {
            return Err(HarnessOperatorApiError::InvalidGitDiff);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessReverseAttributionWorkspaceV1 {
    pub node_id: HarnessSelectorV1,
    pub node_incarnation_id: HarnessNodeIncarnationV1,
    pub workspace_id: HarnessSelectorV1,
}

impl HarnessReverseAttributionWorkspaceV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.node_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.node_incarnation_id.validate()?;
        self.workspace_id.validate().map_err(HarnessOperatorApiError::Protocol)
    }
}

#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessReverseAttributionSubjectV1 {
    ManagedRecord {
        workspace: HarnessReverseAttributionWorkspaceV1,
        record_id: HarnessSelectorV1,
    },
    RuntimeSession {
        workspace: HarnessReverseAttributionWorkspaceV1,
        instance_id: u64,
        generation: u64,
    },
    Workspace {
        workspace: HarnessReverseAttributionWorkspaceV1,
    },
    FileScope {
        workspace: HarnessReverseAttributionWorkspaceV1,
        relative_path: HarnessRepositoryPathV1,
    },
    CommitScope {
        workspace: HarnessReverseAttributionWorkspaceV1,
        object_id: HarnessGitObjectIdV1,
    },
}

impl HarnessReverseAttributionSubjectV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        let workspace = match self {
            Self::ManagedRecord { workspace, record_id } => {
                record_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                workspace
            }
            Self::RuntimeSession { workspace, instance_id, generation } => {
                if *instance_id == 0 || *generation == 0 {
                    return Err(HarnessOperatorApiError::InvalidReverseAttribution);
                }
                workspace
            }
            Self::Workspace { workspace } => workspace,
            Self::FileScope { workspace, relative_path } => {
                relative_path.validate()?;
                workspace
            }
            Self::CommitScope { workspace, object_id } => {
                object_id.validate()?;
                workspace
            }
        };
        workspace.validate()
    }

    fn workspace(&self) -> &HarnessReverseAttributionWorkspaceV1 {
        match self {
            Self::ManagedRecord { workspace, .. }
            | Self::RuntimeSession { workspace, .. }
            | Self::Workspace { workspace }
            | Self::FileScope { workspace, .. }
            | Self::CommitScope { workspace, .. } => workspace,
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessReverseAttributionRelationV1 {
    ManagedRecordBinding,
    RuntimeSessionBinding,
    WorkspaceBinding,
    WorkspaceScope,
}

#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessReverseAttributionBindingV1 {
    ManagedRecord {
        workspace: HarnessReverseAttributionWorkspaceV1,
        record_id: HarnessSelectorV1,
        active_instance_id: Option<u64>,
        active_generation: Option<u64>,
    },
    RuntimeSession {
        workspace: HarnessReverseAttributionWorkspaceV1,
        instance_id: u64,
        generation: u64,
    },
    Workspace {
        workspace: HarnessReverseAttributionWorkspaceV1,
    },
}

impl HarnessReverseAttributionBindingV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        let workspace = match self {
            Self::ManagedRecord {
                workspace,
                record_id,
                active_instance_id,
                active_generation,
            } => {
                record_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                if active_instance_id.is_some() != active_generation.is_some()
                    || active_instance_id == &Some(0)
                    || active_generation == &Some(0)
                {
                    return Err(HarnessOperatorApiError::InvalidReverseAttribution);
                }
                workspace
            }
            Self::RuntimeSession { workspace, instance_id, generation } => {
                if *instance_id == 0 || *generation == 0 {
                    return Err(HarnessOperatorApiError::InvalidReverseAttribution);
                }
                workspace
            }
            Self::Workspace { workspace } => workspace,
        };
        workspace.validate()
    }

    fn workspace(&self) -> &HarnessReverseAttributionWorkspaceV1 {
        match self {
            Self::ManagedRecord { workspace, .. }
            | Self::RuntimeSession { workspace, .. }
            | Self::Workspace { workspace } => workspace,
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessReverseAttributionLinkV1 {
    pub task_id: HarnessTaskId,
    pub run_id: HarnessRunId,
    pub run_revision: HarnessRevision,
    pub binding: HarnessReverseAttributionBindingV1,
    pub relation: HarnessReverseAttributionRelationV1,
}

impl HarnessReverseAttributionLinkV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.task_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.run_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.run_revision.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.binding.validate()?;
        let relation_matches = matches!(
            (&self.relation, &self.binding),
            (
                HarnessReverseAttributionRelationV1::ManagedRecordBinding,
                HarnessReverseAttributionBindingV1::ManagedRecord { .. },
            ) | (
                HarnessReverseAttributionRelationV1::RuntimeSessionBinding,
                HarnessReverseAttributionBindingV1::RuntimeSession { .. },
            ) | (
                HarnessReverseAttributionRelationV1::WorkspaceBinding
                    | HarnessReverseAttributionRelationV1::WorkspaceScope,
                HarnessReverseAttributionBindingV1::Workspace { .. },
            )
        );
        if !relation_matches {
            return Err(HarnessOperatorApiError::InvalidReverseAttribution);
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessReverseAttributionOutcomeV1 {
    Attributed,
    Unattributed,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessReverseAttributionV1 {
    pub subject: HarnessReverseAttributionSubjectV1,
    pub outcome: HarnessReverseAttributionOutcomeV1,
    pub links: Vec<HarnessReverseAttributionLinkV1>,
}

impl HarnessReverseAttributionV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.subject.validate()?;
        if self.links.len() > HARNESS_REVERSE_ATTRIBUTION_LINKS_MAX
            || matches!(self.outcome, HarnessReverseAttributionOutcomeV1::Attributed)
                != !self.links.is_empty()
            || self.links.windows(2).any(|pair| pair[0] >= pair[1])
        {
            return Err(HarnessOperatorApiError::InvalidReverseAttribution);
        }
        for link in &self.links {
            link.validate()?;
            if link.binding.workspace() != self.subject.workspace()
                || !self.link_matches_subject(link)
            {
                return Err(HarnessOperatorApiError::InvalidReverseAttribution);
            }
        }
        Ok(())
    }

    pub fn validate_for(
        &self,
        subject: &HarnessReverseAttributionSubjectV1,
    ) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        if &self.subject != subject {
            return Err(HarnessOperatorApiError::InvalidReverseAttribution);
        }
        Ok(())
    }

    fn link_matches_subject(&self, link: &HarnessReverseAttributionLinkV1) -> bool {
        match (&self.subject, &link.relation, &link.binding) {
            (
                HarnessReverseAttributionSubjectV1::ManagedRecord { record_id, .. },
                HarnessReverseAttributionRelationV1::ManagedRecordBinding,
                HarnessReverseAttributionBindingV1::ManagedRecord {
                    record_id: binding_record_id,
                    ..
                },
            ) => record_id == binding_record_id,
            (
                HarnessReverseAttributionSubjectV1::RuntimeSession {
                    instance_id,
                    generation,
                    ..
                },
                HarnessReverseAttributionRelationV1::RuntimeSessionBinding,
                HarnessReverseAttributionBindingV1::RuntimeSession {
                    instance_id: binding_instance_id,
                    generation: binding_generation,
                    ..
                },
            ) => instance_id == binding_instance_id && generation == binding_generation,
            (
                HarnessReverseAttributionSubjectV1::Workspace { .. },
                HarnessReverseAttributionRelationV1::WorkspaceBinding,
                HarnessReverseAttributionBindingV1::Workspace { .. },
            )
            | (
                HarnessReverseAttributionSubjectV1::FileScope { .. }
                    | HarnessReverseAttributionSubjectV1::CommitScope { .. },
                HarnessReverseAttributionRelationV1::WorkspaceScope,
                HarnessReverseAttributionBindingV1::Workspace { .. },
            ) => true,
            _ => false,
        }
    }
}

/// Mirrors `gate4agent_types::ProviderSessionKey` exactly, for the same
/// reason `HarnessTerminalControlV1` duplicates `TerminalControl` rather than
/// importing it -- see that type's own doc comment.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessProviderSessionKeyV1 {
    SessionId,
    ConversationId,
}

/// Mirrors `gate4agent_types::ProviderSessionIdentity`: the caller-supplied
/// reference `IndexProviderSession` binds a durable managed-session record
/// to. Bounds match the node's own `PROVIDER_EVENT_ID_MAX_BYTES`/
/// `PROVIDER_SESSION_LOCATOR_MAX_BYTES` (the node re-validates on its own
/// terms regardless; this bound exists to reject an obviously-oversized
/// request before it ever reaches C2).
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessProviderSessionIdentityV1 {
    pub key: HarnessProviderSessionKeyV1,
    pub id: String,
    pub transcript_path: Option<String>,
}

impl HarnessProviderSessionIdentityV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if self.id.is_empty()
            || self.id.len() > HARNESS_PROVIDER_SESSION_ID_MAX_BYTES
            || self.id.starts_with('-')
            || self.id.chars().any(char::is_control)
            || self.transcript_path.as_ref().is_some_and(|path| {
                path.is_empty()
                    || path.len() > HARNESS_PROVIDER_SESSION_TRANSCRIPT_PATH_MAX_BYTES
                    || path.chars().any(char::is_control)
            })
        {
            return Err(HarnessOperatorApiError::InvalidSessionRecordRequest);
        }
        Ok(())
    }
}

/// Mirrors the node's own `SessionTaskTargetV1` shape exactly (`New` starts
/// a fresh correlation, `Existing` binds to a caller-named task, `Clear`
/// removes the binding). The node's `task_id` there is its own local
/// identifier space (format `task-<24 lowercase hex>`, unrelated to this
/// crate's `HarnessTaskId` despite the similar name -- the node layer has no
/// dependency on `hatchery-harness-protocol` and never mints one from a
/// `HarnessTaskId`), so `task_id` here stays a bounded opaque string rather
/// than the typed `HarnessTaskId`: the harness-service boundary parses and
/// validates it against the node's own format when building the C2 request.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessSessionTaskTargetV1 {
    New,
    Existing { task_id: String },
    Clear,
}

impl HarnessSessionTaskTargetV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if let Self::Existing { task_id } = self {
            if !valid_runtime_id(task_id, HARNESS_SESSION_RECORD_TASK_ID_MAX_BYTES) {
                return Err(HarnessOperatorApiError::InvalidSessionRecordRequest);
            }
        }
        Ok(())
    }
}

/// Exact mirror of `gate4agent_types::ProviderInteractionKind`: this crate
/// has no dependency on `gate4agent-types` (see the doc comment on
/// `HarnessTerminalControlV1`), so the ACP interaction classification the
/// node resolves a `correlation_id` against is duplicated here as its own
/// closed wire enum, the same way `HarnessAgentStreamChunkKindV1::
/// InteractionPrompt` carries it on the outbound side.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessProviderInteractionKindV1 {
    Approval,
    Question,
}

/// Exact mirror of `gate4agent_types::ProviderInteractionResponse`: this
/// crate has no dependency on `gate4agent-types` (see the doc comment on
/// `HarnessTerminalControlV1`), so `ResolveInteraction`'s answer payload is
/// duplicated here as its own closed wire enum rather than imported.
/// Unlike the source type's own `validate_for`, `validate()` below does not
/// check `Answer`/`ApproveOnce`/`Deny` against the live interaction's own
/// `HarnessProviderInteractionKindV1` (`Approval` vs `Question`) -- that
/// check needs the interaction the node is holding under `correlation_id`
/// and stays the node's job, not this wire's, the same way
/// `WriteSessionInput` never checks that a PTY exists before sending. It
/// bounds `Answer { text }` to `HARNESS_ACP_INTERACTION_RESPONSE_MAX_BYTES`
/// and rejects unsafe control bytes, but not emptiness: the source type's
/// `EmptyAnswer` rule applies only when the interaction turns out to be a
/// `Question`, which this wire does not know either.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessProviderInteractionResponseV1 {
    ApproveOnce,
    Deny,
    Answer { text: String },
}

impl HarnessProviderInteractionResponseV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        match self {
            Self::ApproveOnce | Self::Deny => Ok(()),
            Self::Answer { text } => {
                if text.len() > HARNESS_ACP_INTERACTION_RESPONSE_MAX_BYTES
                    || contains_unsafe_control_bytes(text)
                {
                    return Err(HarnessOperatorApiError::InvalidSessionControl);
                }
                Ok(())
            }
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessOperatorRequestV1 {
    MonitorGet { run_id: HarnessRunId },
    TimelineRead {
        run_id: HarnessRunId,
        after_sequence: Option<u64>,
        limit: u16,
    },
    TasksList {
        after_task_id: Option<HarnessTaskId>,
        state: Option<HarnessTaskStateV1>,
        /// "What did this task spawn": restricts the page to tasks whose
        /// own `parent_task_id` equals this one -- direct children only,
        /// never deeper descendants. Additive over the original bare
        /// `{ after_task_id, state, limit }` shape (`#[serde(default)]`
        /// keeps an older caller decoding), mirrors `HarnessReadRequestV1::
        /// TasksList`'s own addition of the same filter.
        #[serde(default)]
        parent_task_id: Option<HarnessTaskId>,
        limit: u16,
    },
    TaskGet { task_id: HarnessTaskId },
    /// D5, Slice D: the operator's own per-task operations ledger --
    /// `HarnessEngine::operations_for_task`'s bounded, newest-first page,
    /// with the raw `HarnessActorV1` (the operator is already trusted with
    /// every other raw identity this wire exposes; the agent side never
    /// sees this, only the category -- see `HarnessOperationLedgerEntryV1`'s
    /// own doc comment).
    TaskOperations { task_id: HarnessTaskId, limit: u16 },
    RunsList {
        task_id: Option<HarnessTaskId>,
        after_run_id: Option<HarnessRunId>,
        lifecycle: Option<HarnessRunLifecycleV1>,
        /// "What did this run spawn": restricts the page to runs whose own
        /// `parent_run_id` equals this one. Additive (`#[serde(default)]`),
        /// mirrors `HarnessReadRequestV1::RunsList`'s own addition.
        #[serde(default)]
        parent_run_id: Option<HarnessRunId>,
        limit: u16,
    },
    RunGet { run_id: HarnessRunId },
    RunCorrelationGet { run_id: HarnessRunId },
    RunTransferGet { run_id: HarnessRunId },
    ReverseAttributionGet { subject: HarnessReverseAttributionSubjectV1 },
    ObserveRunContextSource { run_id: HarnessRunId },
    InspectRunWorkspace { run_id: HarnessRunId },
    ReadRunWorkspaceFile {
        run_id: HarnessRunId,
        path: HarnessRepositoryPathV1,
    },
    ReadRunGitHistory {
        run_id: HarnessRunId,
        path: Option<HarnessRepositoryPathV1>,
        before: Option<HarnessGitObjectIdV1>,
        limit: u16,
    },
    ReadRunGitDiff {
        run_id: HarnessRunId,
        mode: HarnessGitDiffModeV1,
        path: Option<HarnessRepositoryPathV1>,
    },
    // Node-scoped siblings of the `*RunWorkspace*`/`*RunGit*` family above:
    // routed straight from a node/workspace pair with no run binding, for
    // sidebar Files/Git reads that have no run in flight (harness-mode
    // parity with the direct-C2 TUI's `InspectWorkspace`).
    InspectNodeWorkspace { node_id: String, workspace_id: String },
    ReadNodeWorkspaceFile {
        node_id: String,
        workspace_id: String,
        path: HarnessRepositoryPathV1,
    },
    ReadNodeGitHistory {
        node_id: String,
        workspace_id: String,
        path: Option<HarnessRepositoryPathV1>,
        before: Option<HarnessGitObjectIdV1>,
        limit: u16,
    },
    ReadNodeGitDiff {
        node_id: String,
        workspace_id: String,
        mode: HarnessGitDiffModeV1,
        path: Option<HarnessRepositoryPathV1>,
    },
    // V11 write/create siblings of the four node-workspace read verbs above:
    // same direct node/workspace-pair routing (`exact_route`, no run
    // binding involved), just a write instead of a read -- the harness-mode
    // twin of the light TUI editor's save/create-file/create-directory
    // affordances. `WriteNodeWorkspaceFile` relays `NodeRequest::
    // WriteWorkspaceFile`: the node's own CAS write, gated on
    // `expected_revision` matching the file's current on-disk SHA-256. A
    // stale value comes back as the node's `RepositoryFileRevisionConflict`
    // failure code, which this wire surfaces as the typed `Conflict` host
    // error (see `map_node_workspace_write_error` in
    // `hatchery-harness-service`), never collapsed into `Internal`.
    // `CreateNodeWorkspaceFile`/`CreateNodeWorkspaceDirectory` relay the
    // node's own two distinct creation verbs (`NodeRequest::
    // CreateWorkspaceFile`/`CreateWorkspaceDirectory`) -- the node models
    // file and directory creation as two separate requests, not one verb
    // with a kind flag, so this wire mirrors that shape exactly rather than
    // inventing a combined one.
    WriteNodeWorkspaceFile {
        node_id: String,
        workspace_id: String,
        path: HarnessRepositoryPathV1,
        content: String,
        expected_revision: HarnessWorkspaceFileRevisionV1,
    },
    CreateNodeWorkspaceFile {
        node_id: String,
        workspace_id: String,
        path: HarnessRepositoryPathV1,
    },
    CreateNodeWorkspaceDirectory {
        node_id: String,
        workspace_id: String,
        path: HarnessRepositoryPathV1,
    },
    LaunchPlansList {
        after_plan_id: Option<HarnessSelectorV1>,
        limit: u16,
    },
    TaskExecutionSpecGet { task_id: HarnessTaskId },
    /// `provider`/`workspace`/`plan_id` filter the derived plan catalogue,
    /// `after` pages it (same idiom as `LaunchPlansList`'s `after_plan_id`):
    /// the catalogue itself has no cap (see `HarnessTaskLaunchOptionsV1`'s
    /// own doc comment), only a page of it does. All four are additive over
    /// the original bare `{ task_id }` shape -- `#[serde(default)]` so an
    /// older caller that only ever sent `task_id` keeps deserializing.
    TaskLaunchOptionsGet {
        task_id: HarnessTaskId,
        #[serde(default)]
        provider: Option<HarnessSelectorV1>,
        #[serde(default)]
        workspace: Option<HarnessSelectorV1>,
        #[serde(default)]
        plan_id: Option<HarnessSelectorV1>,
        #[serde(default)]
        after: Option<HarnessSelectorV1>,
    },
    RuntimeInventoryList {
        after_node_id: Option<String>,
        limit: u16,
    },
    TerminalRead {
        session: HarnessRuntimeSessionAddressV1,
        after_sequence: Option<u64>,
        limit: u16,
    },
    // Direct operator session verbs: unlike the CAS/task-mutation family
    // below (`CreateTask`..`StartTaskV2`, closed-set `HarnessOperatorActionV1`
    // shape), a session spawn/write/resize/stop has no task to CAS against --
    // it relays straight to C2 the same way `TerminalRead` reads straight
    // from C2, just as a write instead of a read. See the harness-service
    // host's async select loop (not `execute_operator_request`, which is
    // synchronous with no C2 handle in scope) for the dispatch.
    SpawnSession {
        node_id: String,
        workspace_id: String,
        provider: String,
        provider_profile: String,
        mode: HarnessExecutionModeV1,
        terminal_size: HarnessRuntimeTerminalSizeV1,
        // `None` means "the axis default" (`ApprovalLevel::FullAuto` --
        // not asking a human is the owner's stated norm and this field does
        // not change that default), so every caller that predates this
        // field, and every caller that simply does not care, is unchanged.
        // `Some(_)` is the one way `Moderate`/`ReadOnly`/`Unmanaged` becomes
        // reachable on a session spawned through this wire at all -- see
        // `hatchery-node`'s `require_session_runtime_policy` doc and
        // `HostPolicy::Yolo`'s own doc for why `FullAuto` alone left the
        // whole permission-deferral path dead code in production.
        #[serde(default)]
        approval_level: Option<HarnessApprovalLevelV1>,
    },
    WriteSessionInput {
        session: HarnessRuntimeSessionAddressV1,
        text: String,
    },
    // `PromptSession` relays to `NodeRequest::Prompt`, the node's semantic-
    // prompt verb -- a DIFFERENT node request from the one immediately
    // above. `WriteSessionInput` relays to `NodeRequest::Input` (typed/raw
    // PTY text) and keeps meaning exactly that; this is an additional verb,
    // not a reinterpretation of it, the same way `PasteSession` relaying
    // `NodeRequest::Paste` is additional to it rather than a replacement.
    // Bounded by the same `HARNESS_SESSION_INPUT_MAX_BYTES` ceiling
    // `WriteSessionInput`/`PasteSession` already use (see that constant's
    // doc comment) -- the node checks all three against the same single
    // `MAX_NODE_TEXT_BYTES`, so there is no separate per-verb bound to pick.
    //
    // Admissible only against an ACP- or inline-transport session, and
    // refused BY NAME for a PTY-transport one -- unconditionally, even for
    // the rare PTY session whose provider profile currently admits a node-
    // level `SemanticPrompt` runtime policy (reachable today only through
    // `ResumeSessionRecord`'s own `initial_prompt`, a narrow capability for
    // typing exactly one prompt into a raw terminal at resume time, for a
    // provider CLI a human is watching). This is a deliberate, permanent
    // boundary the owner drew, not a limitation waiting to be lifted: a PTY
    // session has a human sitting at it, choosing what runs and watching
    // what appears, so what lands on that screen is the operator's
    // business, never this wire's. ACP and inline are the two transports
    // this project opens with nobody at a terminal, and prompting is their
    // ordinary interface -- `PromptSession` and `WriteSessionInput` must
    // never silently fall back to each other across that line.
    //
    // Enforced in `hatchery-harness-service`, not here: this wire type
    // carries only a bare routing address (`HarnessRuntimeSessionAddressV1`
    // has no transport field of its own), and tightening the node's shared
    // `NodeRequest::Prompt` handler itself would also narrow the
    // `ResumeSessionRecord` capability above, which this change does not
    // touch. `hatchery-harness-service` checks its own cached runtime
    // inventory before ever dispatching to C2/the node and refuses by name
    // (`HarnessOperatorHostErrorV1::UnsupportedTransport`) when that cache
    // already knows the target is PTY; the node's existing
    // `require_session_runtime_policy(SemanticPrompt)` gate remains the
    // backstop for the narrow window right after a spawn/resume where the
    // cache cannot yet prove the transport either way.
    PromptSession {
        session: HarnessRuntimeSessionAddressV1,
        text: String,
    },
    ResizeSession {
        session: HarnessRuntimeSessionAddressV1,
        terminal_size: HarnessRuntimeTerminalSizeV1,
    },
    StopSession {
        session: HarnessRuntimeSessionAddressV1,
        force: bool,
    },
    // V11 siblings of the four V10 session verbs above -- the same direct,
    // no-CAS C2 relay, just five more `NodeRequest` shapes the light TUI's
    // terminal already sends. `ControlSession` relays `NodeRequest::
    // TerminalControl` (special keys: Enter, Ctrl-C, arrows, ...).
    // `WriteSessionBytes` relays `NodeRequest::TerminalBytes` (a short raw
    // byte sequence, bounded well below `WriteSessionInput`'s text ceiling --
    // see `HARNESS_SESSION_BYTES_MAX_BYTES`). `PasteSession` relays
    // `NodeRequest::Paste`: a distinct node-level verb from `WriteSessionInput`
    // (the node frames it as a semantic bracketed paste, not plain typed
    // text), so it gets its own wire verb rather than folding into
    // `WriteSessionInput`. `RemoveSession` relays `NodeRequest::Remove`,
    // clearing an exited/failed session's binding from the node -- the same
    // node-side call a successful `StopSession` already fires as a best-effort
    // follow-up reap (see `HarnessC2Adapter::remove_stopped_session`'s doc
    // comment in `hatchery-harness-service`), now exposed as its own
    // first-class verb for a session that reached that state on its own.
    // `ResumeSession` relays `NodeRequest::Resume` and, unlike `SpawnSession`,
    // acks dispatch only: the node never returns a new address for it (same
    // `instance_id`, generation bumped only once the resume actually settles,
    // reported through the runtime inventory rather than this reply).
    ControlSession {
        session: HarnessRuntimeSessionAddressV1,
        control: HarnessTerminalControlV1,
    },
    WriteSessionBytes {
        session: HarnessRuntimeSessionAddressV1,
        bytes: Vec<u8>,
    },
    PasteSession {
        session: HarnessRuntimeSessionAddressV1,
        text: String,
    },
    RemoveSession {
        session: HarnessRuntimeSessionAddressV1,
    },
    ResumeSession {
        session: HarnessRuntimeSessionAddressV1,
        terminal_size: HarnessRuntimeTerminalSizeV1,
    },
    // The four ACP control verbs, session-address-scoped exactly like the
    // eight session-control verbs above and relayed the same direct,
    // no-CAS way to C2/node -- gated node-side behind its own
    // `acp-control-v1` capability (a peer that predates it never negotiates
    // it and never sees these sent), so asking one of a PTY or inline
    // session is refused by name rather than silently accepted. They answer
    // what the outbound `SubscribeAgentStream` content stream reports (see
    // `HarnessOperatorAgentEventV1`), not what `SubscribeEvents`'s
    // task/run/node telemetry or `TimelineRead`'s `ObservationV1` stream
    // can: neither carries the question text, the option list, or the
    // mode/config/model catalogs these verbs act on
    // (`docs/gate4agent/plans/gate4agent-acp-control-plane-on-the-wire-2026-09-02.md`
    // §3-5). All four ack with a bare unit reply
    // (`InteractionResolved`/`SessionModeSet`/`SessionConfigOptionSet`/
    // `SessionModelSet`), the same shape `WriteSessionInput` acks with,
    // because the node itself acks all four with its own bare
    // `NodeResponse::Accepted`.
    //
    // `ResolveInteraction` answers the exact `correlation_id` the
    // observation stream already minted onto `ObservationKindV1::
    // ApprovalRequested`/`QuestionRequested`. `response` must additionally
    // match the interaction's own kind (`Approval` vs `Question`) -- that
    // check needs the live interaction the node holds and stays the node's
    // job, not this wire's, the same way `WriteSessionInput` never checks
    // that a PTY exists before sending.
    ResolveInteraction {
        session: HarnessRuntimeSessionAddressV1,
        correlation_id: String,
        response: HarnessProviderInteractionResponseV1,
    },
    SetSessionMode {
        session: HarnessRuntimeSessionAddressV1,
        mode_id: String,
    },
    SetSessionConfigOption {
        session: HarnessRuntimeSessionAddressV1,
        option_id: String,
        value_json: String,
    },
    SetSessionModel {
        session: HarnessRuntimeSessionAddressV1,
        model_id: String,
    },
    CatalogNativeSessions {
        route: HarnessNativeSessionRouteV1,
        limit: u16,
    },
    PageNativeSessions {
        route: HarnessNativeSessionRouteV1,
        window: HarnessNativeSessionCatalogWindowV1,
        catalog_revision: u64,
        recent_cutoff_unix_ms: u64,
        after_selection_id: Option<String>,
        limit: u16,
    },
    PreviewNativeSession {
        selection: HarnessNativeSessionSelectionV1,
        message_limit: u16,
    },
    // Session-record family: node-scoped like `InspectNodeWorkspace`
    // (`node_id` resolved live via `exact_route`, no caller-pinned
    // incarnation), not session-address-scoped like the eight session-
    // control verbs above -- a managed session record has its own
    // `record_id` identity independent of whether it currently has a live
    // PTY bound to it. `PreviewSessionRecord` relays `NodeRequest::
    // PreviewSessionRecord` and rides the same read-worker pool as
    // `CatalogNativeSessions`/`PageNativeSessions`/`PreviewNativeSession`
    // above (`is_native_history_request` in `hatchery-harness-service`) --
    // the light TUI sends this exact node request for both an initial
    // preview open and a background history refresh of an already-open
    // preview tab, so this wire stays a single verb the same way.
    PreviewSessionRecord {
        node_id: String,
        record_id: String,
        message_limit: u16,
    },
    // `ResumeSessionRecord`/`RenameSessionRecord`/`SetSessionTask`/
    // `ForgetSessionRecord`/`IndexProviderSession`/`IndexNativeSession` are
    // mutations, dispatched through the bounded session-record-mutation
    // worker pool (`is_session_record_mutation_request`) the same way the
    // eight session-control verbs use their own pool -- unlike those eight,
    // each of these six carries a distinct, non-`Accepted` node reply (a
    // managed-session record, or for `ResumeSessionRecord` a record plus the
    // freshly spawned session address), so the reply is relayed back
    // correlated rather than collapsed into one shared ack shape. Unlike
    // `ResumeSession` (existing live session, ack-only, generation bump
    // reported through the runtime inventory), `ResumeSessionRecord` spawns
    // a *new* session from a dormant record and the node returns that
    // session's address synchronously -- this wire mirrors that exactly.
    ResumeSessionRecord {
        node_id: String,
        record_id: String,
        terminal_size: HarnessRuntimeTerminalSizeV1,
        initial_prompt: Option<String>,
    },
    RenameSessionRecord {
        node_id: String,
        record_id: String,
        display_name: String,
    },
    SetSessionTask {
        node_id: String,
        record_id: String,
        expected_revision: u64,
        target: HarnessSessionTaskTargetV1,
    },
    ForgetSessionRecord {
        node_id: String,
        record_id: String,
    },
    IndexProviderSession {
        node_id: String,
        workspace_id: String,
        provider: String,
        identity: HarnessProviderSessionIdentityV1,
        display_name: String,
    },
    // Unlike the five siblings above, routed via `selection.route` (same
    // caller-pinned-incarnation shape `CatalogNativeSessions`/
    // `PageNativeSessions`/`PreviewNativeSession` already use) rather than a
    // bare `node_id`: the selection being committed into a durable record
    // was chosen from a previously fetched native-session catalog page, so
    // it carries its own route already. Still dispatched through the
    // session-record-mutation pool, not the native-history read pool --
    // it mutates the managed-session store, it does not read a catalog.
    IndexNativeSession {
        selection: HarnessNativeSessionSelectionV1,
        display_name: String,
    },
    CreateTask { request: HarnessCreateTaskRequestV1 },
    ReplaceTask { request: HarnessReplaceTaskRequestV1 },
    MoveTask { request: HarnessMoveTaskRequestV1 },
    CancelTask { request: HarnessCancelTaskRequestV1 },
    RetryTask { request: HarnessRetryTaskRequestV1 },
    ScheduleNext { request: HarnessScheduleNextRequestV1 },
    ReplaceTaskExecutionSpec { request: HarnessReplaceTaskExecutionSpecRequestV1 },
    StartTask { request: HarnessStartTaskRequestV1 },
    ReplaceTaskExecutionSpecV2 { request: HarnessReplaceTaskExecutionSpecRequestV2 },
    StartTaskV2 { request: HarnessStartTaskRequestV2 },
    /// Starts the issued task the same way `StartTaskV2` does, then mints
    /// the dispatch's default harness-MCP grant through
    /// `runtime::resolve_harness_mcp_grant` (the production mint, not a test
    /// helper). Refused unless the issued plan is `GrantBound` with an
    /// `Operator` grant policy. The reply is the grant id and revision; it
    /// does not carry a read credential.
    MintHarnessMcpGrant { request: HarnessStartTaskRequestV2 },
    SubmitIntent { intent: HarnessOperatorIntentV1 },
    // Node-scoped, paged host-directory listing behind the folder-browser
    // dialog: the harness-mode sibling of the light TUI's own
    // `AppAction::BrowseHostDirectories`, relaying `NodeRequest::
    // BrowseHostDirectories` directly. No `workspace_id`: unlike every read
    // above, a host-directory browse targets the node's host filesystem, not
    // a registered workspace, so it does not fit `InspectNodeWorkspace`'s
    // family (see `PreparedHostDirectoryBrowse`'s doc comment in
    // `hatchery-harness-service` for why this rides its own bounded read
    // lane rather than that family's). `directory: None` opens the node's
    // default root; `after` pages a previously returned `next_after` cursor.
    BrowseHostDirectories {
        node_id: String,
        directory: Option<HarnessHostPathV1>,
        after: Option<HarnessHostPathV1>,
    },
    // `RegisterWorkspace`/`UnregisterWorkspace`/`CreateStandaloneWorkspace`/
    // `CreateWorktree`/`RemoveWorktree`/`ExportContextPack`/
    // `ForgetContextPack` are the resource-mutation family: workspace/
    // worktree lifecycle plus context-pack export/forget, dispatched through
    // the bounded resource-mutation worker pool (`is_resource_mutation_
    // request` in `hatchery-harness-service`) the same way the session-
    // record family uses its own pool -- see `ResourceMutationKind`'s doc
    // comment there for why these seven heterogeneous verbs share one lane.
    // `RegisterWorkspace`'s success (like every workspace/worktree lifecycle
    // verb in this family) adds an entry to the node's `workspaces` map,
    // which the runtime inventory's own roster caches
    // (`HarnessRuntimeInventoryV1::workspaces`) -- the host invalidates that
    // route's cached entry unconditionally on success, so a harness-mode
    // sidebar converges the same way it does after a session-record
    // mutation.
    RegisterWorkspace {
        node_id: String,
        workspace_id: String,
        root: HarnessHostPathV1,
    },
    UnregisterWorkspace {
        node_id: String,
        workspace_id: String,
    },
    CreateStandaloneWorkspace {
        node_id: String,
        workspace_id: String,
        root: HarnessHostPathV1,
        initial_branch: Option<String>,
    },
    CreateWorktree {
        node_id: String,
        source_workspace_id: String,
        workspace_id: String,
        target_root: HarnessHostPathV1,
        branch: String,
        base: Option<String>,
    },
    RemoveWorktree {
        node_id: String,
        source_workspace_id: String,
        target_root: HarnessHostPathV1,
    },
    // Session-address-scoped, not node-id-scoped like the five workspace/
    // worktree verbs above: relays `NodeRequest::ExportContextPack` exactly
    // the way the eight session-control verbs relay their own node requests
    // from a `HarnessRuntimeSessionAddressV1` -- the node/incarnation this
    // targets comes from `session.node_id`/`session.incarnation_id`, so
    // there is no separate `node_id` field. Reuses
    // `HarnessResolvedContextPackReceiptV1` (already defined for the A2 run-
    // continuation arc) as its reply verbatim rather than inventing a
    // duplicate: the node's own `ResolvedContextPackReceipt` is the exact
    // same payload shape either way `ExportContextPack`/
    // `ExportContextPackForSessionRecord` produces it.
    ExportContextPack {
        session: HarnessRuntimeSessionAddressV1,
    },
    // Node-scoped like the five workspace/worktree verbs above, not session-
    // address-scoped like `ExportContextPack`: forgetting an exported
    // context pack from the node's `ContextPackStore` needs only the node
    // and the pack's own id, no session or incarnation binding.
    ForgetContextPack {
        node_id: String,
        context_id: HarnessSelectorV1,
    },
    // Opens a long-lived, server-push subscription instead of the usual
    // one-shot request/reply: see the framing note on `HarnessOperatorEventV1`
    // for the wire shape this switches the connection into.
    SubscribeEvents {},
    // Same "opens a long-lived, server-push subscription" framing as
    // SubscribeEvents immediately above, on its own connection: see
    // `HarnessOperatorTerminalEventV1`'s doc comment for why terminal
    // frames do not ride SubscribeEvents's own queue. `sessions` is
    // declared once, at subscribe time -- this wire has no representable
    // second client-to-host message (see the module doc), so a client
    // that opens or closes a pane reconnects with the updated list rather
    // than patching an existing subscription.
    SubscribeTerminal { sessions: Vec<HarnessRuntimeSessionAddressV1> },
    // Same "opens a long-lived, server-push subscription" framing as
    // SubscribeTerminal immediately above, on its own connection, for the
    // ACP agent-content stream (node-side `agent-stream-events-v1`) rather
    // than terminal frames -- see `HarnessOperatorAgentEventV1`'s doc
    // comment for why that push channel needs its own event type distinct
    // from both `HarnessOperatorEventV1` and `HarnessOperatorTerminalEventV1`.
    // `sessions` is declared once, at subscribe time, for the same reason
    // SubscribeTerminal's is (see the module doc): a client that opens or
    // closes a pane reconnects with the updated list rather than patching
    // an existing subscription.
    SubscribeAgentStream { sessions: Vec<HarnessRuntimeSessionAddressV1> },
}

impl HarnessOperatorRequestV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        match self {
            Self::MonitorGet { run_id } => {
                run_id.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::TimelineRead { run_id, after_sequence, limit } => {
                run_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                if after_sequence == &Some(0) {
                    return Err(HarnessOperatorApiError::InvalidCursor);
                }
                validate_operator_timeline_limit(*limit)
            }
            Self::TasksList { after_task_id, parent_task_id, limit, .. } => {
                if let Some(task_id) = after_task_id {
                    task_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                }
                if let Some(task_id) = parent_task_id {
                    task_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                }
                validate_operator_limit(*limit)
            }
            Self::TaskGet { task_id } => {
                task_id.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::TaskOperations { task_id, limit } => {
                task_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                validate_operator_limit(*limit)
            }
            Self::RunsList { task_id, after_run_id, parent_run_id, limit, .. } => {
                if let Some(task_id) = task_id {
                    task_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                }
                if let Some(run_id) = after_run_id {
                    run_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                }
                if let Some(run_id) = parent_run_id {
                    run_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                }
                validate_operator_limit(*limit)
            }
            Self::RunGet { run_id }
            | Self::RunCorrelationGet { run_id }
            | Self::RunTransferGet { run_id }
            | Self::ObserveRunContextSource { run_id }
            | Self::InspectRunWorkspace { run_id } => {
                run_id.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::ReverseAttributionGet { subject } => subject.validate(),
            Self::ReadRunWorkspaceFile { run_id, path } => {
                run_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                path.validate()
            }
            Self::ReadRunGitHistory { run_id, path, before, limit } => {
                run_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                if let Some(path) = path { path.validate()?; }
                if let Some(before) = before { before.validate()?; }
                if !(1..=HARNESS_GIT_HISTORY_LIMIT_MAX).contains(limit) {
                    return Err(HarnessOperatorApiError::InvalidLimit);
                }
                Ok(())
            }
            Self::ReadRunGitDiff { run_id, mode, path } => {
                run_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                mode.validate()?;
                if let Some(path) = path { path.validate()?; }
                Ok(())
            }
            Self::InspectNodeWorkspace { node_id, workspace_id } => {
                validate_node_workspace_route(node_id, workspace_id)
            }
            Self::ReadNodeWorkspaceFile { node_id, workspace_id, path } => {
                validate_node_workspace_route(node_id, workspace_id)?;
                path.validate()
            }
            Self::ReadNodeGitHistory { node_id, workspace_id, path, before, limit } => {
                validate_node_workspace_route(node_id, workspace_id)?;
                if let Some(path) = path { path.validate()?; }
                if let Some(before) = before { before.validate()?; }
                if !(1..=HARNESS_GIT_HISTORY_LIMIT_MAX).contains(limit) {
                    return Err(HarnessOperatorApiError::InvalidLimit);
                }
                Ok(())
            }
            Self::ReadNodeGitDiff { node_id, workspace_id, mode, path } => {
                validate_node_workspace_route(node_id, workspace_id)?;
                mode.validate()?;
                if let Some(path) = path { path.validate()?; }
                Ok(())
            }
            Self::WriteNodeWorkspaceFile {
                node_id, workspace_id, path, content, expected_revision,
            } => {
                validate_node_workspace_route(node_id, workspace_id)?;
                path.validate()?;
                expected_revision.validate()?;
                if content.len() > HARNESS_WORKSPACE_FILE_MAX_BYTES {
                    return Err(HarnessOperatorApiError::InvalidWorkspaceFile);
                }
                Ok(())
            }
            Self::CreateNodeWorkspaceFile { node_id, workspace_id, path }
            | Self::CreateNodeWorkspaceDirectory { node_id, workspace_id, path } => {
                validate_node_workspace_route(node_id, workspace_id)?;
                path.validate()
            }
            Self::LaunchPlansList { after_plan_id, limit } => {
                if let Some(plan_id) = after_plan_id {
                    plan_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                }
                if !(1..=HARNESS_LAUNCH_PLAN_PAGE_LIMIT_MAX).contains(limit) {
                    return Err(HarnessOperatorApiError::InvalidLimit);
                }
                Ok(())
            }
            Self::TaskExecutionSpecGet { task_id } => {
                task_id.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::TaskLaunchOptionsGet { task_id, provider, workspace, plan_id, after } => {
                task_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                if let Some(provider) = provider {
                    provider.validate().map_err(HarnessOperatorApiError::Protocol)?;
                }
                if let Some(workspace) = workspace {
                    workspace.validate().map_err(HarnessOperatorApiError::Protocol)?;
                }
                if let Some(plan_id) = plan_id {
                    plan_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
                }
                if let Some(after) = after {
                    after.validate().map_err(|_| HarnessOperatorApiError::InvalidCursor)?;
                }
                Ok(())
            }
            Self::RuntimeInventoryList { after_node_id, limit } => {
                if !(1..=HARNESS_RUNTIME_INVENTORY_PAGE_LIMIT_MAX).contains(limit) {
                    return Err(HarnessOperatorApiError::InvalidLimit);
                }
                if after_node_id.as_deref().is_some_and(|node_id| {
                    !valid_runtime_id(node_id, 128)
                }) {
                    return Err(HarnessOperatorApiError::InvalidCursor);
                }
                Ok(())
            }
            Self::TerminalRead { session, limit, .. } => {
                // after_sequence is unconstrained: node terminal sequences are
                // 0-based, so Some(0) legitimately means "after frame 0".
                session.validate()?;
                validate_operator_terminal_limit(*limit)
            }
            Self::SpawnSession { node_id, workspace_id, provider, provider_profile, terminal_size, .. } => {
                validate_node_workspace_route(node_id, workspace_id)?;
                if !valid_runtime_id(provider, 128) || !valid_runtime_id(provider_profile, 128) {
                    return Err(HarnessOperatorApiError::InvalidSessionSpawn);
                }
                if terminal_size.rows == 0 || terminal_size.columns == 0 {
                    return Err(HarnessOperatorApiError::InvalidSessionSpawn);
                }
                Ok(())
            }
            Self::WriteSessionInput { session, text } => {
                session.validate()?;
                if text.len() > HARNESS_SESSION_INPUT_MAX_BYTES {
                    return Err(HarnessOperatorApiError::InvalidSessionControl);
                }
                Ok(())
            }
            // Same bound as `WriteSessionInput` immediately above -- see
            // `PromptSession`'s own doc comment for why this is a separate
            // verb rather than a flag on that one, and why the PTY-transport
            // refusal it also carries lives in `hatchery-harness-service`
            // rather than here: this type alone cannot see the target
            // session's transport.
            Self::PromptSession { session, text } => {
                session.validate()?;
                if text.len() > HARNESS_SESSION_INPUT_MAX_BYTES {
                    return Err(HarnessOperatorApiError::InvalidSessionControl);
                }
                Ok(())
            }
            Self::ResizeSession { session, terminal_size } => {
                session.validate()?;
                if terminal_size.rows == 0 || terminal_size.columns == 0 {
                    return Err(HarnessOperatorApiError::InvalidSessionControl);
                }
                Ok(())
            }
            Self::StopSession { session, .. } => session.validate(),
            Self::ControlSession { session, .. } => session.validate(),
            Self::WriteSessionBytes { session, bytes } => {
                session.validate()?;
                if bytes.is_empty() || bytes.len() > HARNESS_SESSION_BYTES_MAX_BYTES {
                    return Err(HarnessOperatorApiError::InvalidSessionControl);
                }
                Ok(())
            }
            Self::PasteSession { session, text } => {
                session.validate()?;
                if text.len() > HARNESS_SESSION_INPUT_MAX_BYTES {
                    return Err(HarnessOperatorApiError::InvalidSessionControl);
                }
                Ok(())
            }
            Self::RemoveSession { session } => session.validate(),
            Self::ResumeSession { session, terminal_size } => {
                session.validate()?;
                if terminal_size.rows == 0 || terminal_size.columns == 0 {
                    return Err(HarnessOperatorApiError::InvalidSessionControl);
                }
                Ok(())
            }
            Self::ResolveInteraction { session, correlation_id, response } => {
                session.validate()?;
                if !valid_agent_stream_correlation_id(correlation_id) {
                    return Err(HarnessOperatorApiError::InvalidSessionControl);
                }
                response.validate()
            }
            Self::SetSessionMode { session, mode_id } => {
                session.validate()?;
                if !valid_agent_stream_id(mode_id) {
                    return Err(HarnessOperatorApiError::InvalidSessionControl);
                }
                Ok(())
            }
            Self::SetSessionConfigOption { session, option_id, value_json } => {
                session.validate()?;
                if !valid_agent_stream_id(option_id)
                    || value_json.is_empty()
                    || value_json.len() > HARNESS_AGENT_STREAM_TEXT_MAX_BYTES
                    || contains_unsafe_control_bytes(value_json)
                {
                    return Err(HarnessOperatorApiError::InvalidSessionControl);
                }
                Ok(())
            }
            Self::SetSessionModel { session, model_id } => {
                session.validate()?;
                if !valid_agent_stream_id(model_id) {
                    return Err(HarnessOperatorApiError::InvalidSessionControl);
                }
                Ok(())
            }
            Self::CatalogNativeSessions { route, limit } => {
                route.validate()?;
                validate_native_session_catalog_limit(*limit)
            }
            Self::PageNativeSessions {
                route,
                catalog_revision,
                after_selection_id,
                limit,
                ..
            } => {
                route.validate()?;
                if *catalog_revision == 0
                    || after_selection_id.as_deref().is_some_and(|value| {
                        !valid_native_selection_id(value)
                    })
                {
                    return Err(HarnessOperatorApiError::InvalidNativeHistory);
                }
                validate_native_session_catalog_limit(*limit)
            }
            Self::PreviewNativeSession { selection, message_limit } => {
                selection.validate()?;
                if !(1..=HARNESS_NATIVE_SESSION_PREVIEW_MESSAGE_LIMIT_MAX)
                    .contains(message_limit)
                {
                    return Err(HarnessOperatorApiError::InvalidLimit);
                }
                Ok(())
            }
            Self::PreviewSessionRecord { node_id, record_id, message_limit } => {
                if !valid_runtime_id(node_id, 128) || !valid_runtime_id(record_id, 128) {
                    return Err(HarnessOperatorApiError::InvalidSessionRecordRequest);
                }
                if !(1..=HARNESS_NATIVE_SESSION_PREVIEW_MESSAGE_LIMIT_MAX)
                    .contains(message_limit)
                {
                    return Err(HarnessOperatorApiError::InvalidLimit);
                }
                Ok(())
            }
            Self::ResumeSessionRecord { node_id, record_id, terminal_size, initial_prompt } => {
                if !valid_runtime_id(node_id, 128)
                    || !valid_runtime_id(record_id, 128)
                    || terminal_size.rows == 0
                    || terminal_size.columns == 0
                {
                    return Err(HarnessOperatorApiError::InvalidSessionRecordRequest);
                }
                if initial_prompt.as_deref().is_some_and(|prompt| {
                    prompt.is_empty() || prompt.len() > HARNESS_BODY_MAX_BYTES
                }) {
                    return Err(HarnessOperatorApiError::InvalidSessionRecordRequest);
                }
                Ok(())
            }
            Self::RenameSessionRecord { node_id, record_id, display_name } => {
                if !valid_runtime_id(node_id, 128)
                    || !valid_runtime_id(record_id, 128)
                    || display_name.is_empty()
                    || display_name.len() > HARNESS_SESSION_RECORD_DISPLAY_NAME_MAX_BYTES
                    || display_name.chars().any(char::is_control)
                {
                    return Err(HarnessOperatorApiError::InvalidSessionRecordRequest);
                }
                Ok(())
            }
            Self::SetSessionTask { node_id, record_id, target, .. } => {
                if !valid_runtime_id(node_id, 128) || !valid_runtime_id(record_id, 128) {
                    return Err(HarnessOperatorApiError::InvalidSessionRecordRequest);
                }
                target.validate()
            }
            Self::ForgetSessionRecord { node_id, record_id } => {
                if !valid_runtime_id(node_id, 128) || !valid_runtime_id(record_id, 128) {
                    return Err(HarnessOperatorApiError::InvalidSessionRecordRequest);
                }
                Ok(())
            }
            Self::IndexProviderSession { node_id, workspace_id, provider, identity, display_name } => {
                if !valid_runtime_id(node_id, 128)
                    || !valid_runtime_id(workspace_id, 128)
                    || !valid_runtime_id(provider, 128)
                    || display_name.is_empty()
                    || display_name.len() > HARNESS_SESSION_RECORD_DISPLAY_NAME_MAX_BYTES
                    || display_name.chars().any(char::is_control)
                {
                    return Err(HarnessOperatorApiError::InvalidSessionRecordRequest);
                }
                identity.validate()
            }
            Self::IndexNativeSession { selection, display_name } => {
                selection.validate()?;
                if display_name.is_empty()
                    || display_name.len() > HARNESS_SESSION_RECORD_DISPLAY_NAME_MAX_BYTES
                    || display_name.chars().any(char::is_control)
                {
                    return Err(HarnessOperatorApiError::InvalidSessionRecordRequest);
                }
                Ok(())
            }
            Self::CreateTask { request } => {
                request.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::ReplaceTask { request } => {
                request.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::MoveTask { request } => {
                request.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::CancelTask { request } => {
                request.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::RetryTask { request } => {
                request.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::ScheduleNext { request } => {
                request.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::ReplaceTaskExecutionSpec { request } => {
                request.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::StartTask { request } => {
                request.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::ReplaceTaskExecutionSpecV2 { request } => request.validate(),
            Self::StartTaskV2 { request } => request.validate(),
            Self::MintHarnessMcpGrant { request } => request.validate(),
            Self::SubmitIntent { intent } => intent.validate(),
            Self::BrowseHostDirectories { node_id, directory, after } => {
                if !valid_runtime_id(node_id, 128) {
                    return Err(HarnessOperatorApiError::InvalidHostDirectoryBrowseRequest);
                }
                if let Some(directory) = directory { directory.validate()?; }
                if let Some(after) = after { after.validate()?; }
                Ok(())
            }
            Self::RegisterWorkspace { node_id, workspace_id, root } => {
                if !valid_runtime_id(node_id, 128) || !valid_runtime_id(workspace_id, 128) {
                    return Err(HarnessOperatorApiError::InvalidResourceMutationRequest);
                }
                root.validate()
            }
            Self::UnregisterWorkspace { node_id, workspace_id } => {
                if !valid_runtime_id(node_id, 128) || !valid_runtime_id(workspace_id, 128) {
                    return Err(HarnessOperatorApiError::InvalidResourceMutationRequest);
                }
                Ok(())
            }
            Self::CreateStandaloneWorkspace { node_id, workspace_id, root, initial_branch } => {
                if !valid_runtime_id(node_id, 128) || !valid_runtime_id(workspace_id, 128) {
                    return Err(HarnessOperatorApiError::InvalidResourceMutationRequest);
                }
                root.validate()?;
                if let Some(branch) = initial_branch {
                    if !valid_native_single_line(branch, HARNESS_REPOSITORY_PATH_MAX_BYTES, true) {
                        return Err(HarnessOperatorApiError::InvalidResourceMutationRequest);
                    }
                }
                Ok(())
            }
            Self::CreateWorktree {
                node_id, source_workspace_id, workspace_id, target_root, branch, base,
            } => {
                if !valid_runtime_id(node_id, 128)
                    || !valid_runtime_id(source_workspace_id, 128)
                    || !valid_runtime_id(workspace_id, 128)
                    || !valid_native_single_line(branch, HARNESS_REPOSITORY_PATH_MAX_BYTES, true)
                {
                    return Err(HarnessOperatorApiError::InvalidResourceMutationRequest);
                }
                if let Some(base) = base {
                    if !valid_native_single_line(base, HARNESS_REPOSITORY_PATH_MAX_BYTES, true) {
                        return Err(HarnessOperatorApiError::InvalidResourceMutationRequest);
                    }
                }
                target_root.validate()
            }
            Self::RemoveWorktree { node_id, source_workspace_id, target_root } => {
                if !valid_runtime_id(node_id, 128) || !valid_runtime_id(source_workspace_id, 128) {
                    return Err(HarnessOperatorApiError::InvalidResourceMutationRequest);
                }
                target_root.validate()
            }
            Self::ExportContextPack { session } => session.validate(),
            Self::ForgetContextPack { node_id, context_id } => {
                if !valid_runtime_id(node_id, 128) {
                    return Err(HarnessOperatorApiError::InvalidResourceMutationRequest);
                }
                context_id.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::SubscribeEvents {} => Ok(()),
            Self::SubscribeTerminal { sessions } => {
                if sessions.is_empty()
                    || sessions.len() > HARNESS_TERMINAL_SUBSCRIPTION_SESSIONS_MAX
                {
                    return Err(HarnessOperatorApiError::InvalidTerminalPage);
                }
                for session in sessions { session.validate()?; }
                let mut seen = std::collections::HashSet::with_capacity(sessions.len());
                if !sessions.iter().all(|session| seen.insert(session)) {
                    return Err(HarnessOperatorApiError::InvalidTerminalPage);
                }
                Ok(())
            }
            Self::SubscribeAgentStream { sessions } => {
                if sessions.is_empty()
                    || sessions.len() > HARNESS_AGENT_STREAM_SUBSCRIPTION_SESSIONS_MAX
                {
                    return Err(HarnessOperatorApiError::InvalidAgentStream);
                }
                for session in sessions { session.validate()?; }
                let mut seen = std::collections::HashSet::with_capacity(sessions.len());
                if !sessions.iter().all(|session| seen.insert(session)) {
                    return Err(HarnessOperatorApiError::InvalidAgentStream);
                }
                Ok(())
            }
        }
    }

}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "status", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessOperatorReplyV1 {
    Ok { response: HarnessOperatorResponseV1 },
    Error { error: HarnessOperatorHostErrorV1 },
}

impl HarnessOperatorReplyV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        match self {
            Self::Ok { response } => response.validate(),
            Self::Error { .. } => Ok(()),
        }
    }
}

/// Long-lived, server-push counterpart to `HarnessOperatorReplyV1`. A
/// `SubscribeEvents` connection stops replying with a single frame and
/// instead writes one of these, newline-terminated, per change -- see the
/// module doc for the framing this switches the connection into.
/// `sequence` is a per-subscription monotonic counter the host mints for
/// that one connection alone; it never resets, including across a
/// `Lagged`/`SnapshotBaseline` pair, so a client can detect a gap without
/// cross-referencing wall time. An auth failure or an over-limit subscribe
/// never reaches this type at all -- both still use the ordinary
/// single-frame `HarnessOperatorReplyV1::Error` before the connection
/// closes, since neither one ever admits a subscription in the first place.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessOperatorEventV1 {
    /// Always the first frame after a successful subscribe, and re-sent as
    /// the recovery frame immediately after a `Lagged` -- the client must
    /// treat this as a full replacement of its task/run/node state, never a
    /// delta against whatever it held before.
    SnapshotBaseline {
        sequence: u64,
        tasks: Vec<RedactedTaskV1>,
        runs: Vec<RedactedRunV1>,
        nodes: Vec<HarnessRuntimeNodeInventoryV1>,
    },
    TaskChanged { sequence: u64, task: RedactedTaskV1 },
    RunChanged { sequence: u64, run: RedactedRunV1 },
    RuntimeInventoryChanged { sequence: u64, node: HarnessRuntimeNodeInventoryV1 },
    RuntimeInventoryRemoved { sequence: u64, node_id: String },
    /// This subscriber's outbound queue overflowed and one or more events
    /// were dropped for it specifically -- other subscribers are unaffected.
    /// The host always follows this with a fresh `SnapshotBaseline` once it
    /// can build one; until that frame arrives the client must treat every
    /// task/run/node it is holding as stale rather than trying to patch
    /// around the gap.
    Lagged { sequence: u64 },
    /// Server-side keep-alive, unconditional and periodic (see
    /// `hatchery-harness-service::runtime`'s own
    /// `HOST_SUBSCRIBER_KEEPALIVE_INTERVAL`): the registry's only way to
    /// discover a dead subscriber is a write to it actually failing, so
    /// without something to push on an otherwise-idle connection an
    /// abandoned subscriber can sit occupying its slot indefinitely (see
    /// `SubscriberRegistry`'s own doc comment). Deliberately its own
    /// variant rather than reusing an existing one: `Lagged` carries a
    /// promise (a `SnapshotBaseline` follows) that a bare keep-alive would
    /// break, and every other variant carries real task/run/node state a
    /// client applies -- faking either risks a client acting on a change
    /// that never happened. `Ping` carries nothing to act on, so it cannot
    /// be mistaken for either; every current reader
    /// (`hatchery-harness-client`'s generic `next_event` decode,
    /// `hatchery-tui`'s `project_harness_operator_event`) drops it on the
    /// floor by construction. `sequence` is kept only for schema symmetry
    /// with every other variant -- there is no gap to detect against a
    /// keep-alive that carries no state of its own.
    Ping { sequence: u64 },
}

impl HarnessOperatorEventV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        match self {
            Self::SnapshotBaseline { tasks, runs, nodes, .. } => {
                for task in tasks { task.validate().map_err(HarnessOperatorApiError::Read)?; }
                if tasks.windows(2).any(|pair| pair[0].task_id >= pair[1].task_id) {
                    return Err(HarnessOperatorApiError::Read(HarnessReadApiError::InvalidCollection));
                }
                for run in runs { run.validate().map_err(HarnessOperatorApiError::Read)?; }
                if runs.windows(2).any(|pair| pair[0].run_id >= pair[1].run_id) {
                    return Err(HarnessOperatorApiError::Read(HarnessReadApiError::InvalidCollection));
                }
                for node in nodes { node.validate()?; }
                if nodes.windows(2).any(|pair| pair[0].node_id >= pair[1].node_id) {
                    return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
                }
                Ok(())
            }
            Self::TaskChanged { task, .. } => task.validate().map_err(HarnessOperatorApiError::Read),
            Self::RunChanged { run, .. } => run.validate().map_err(HarnessOperatorApiError::Read),
            Self::RuntimeInventoryChanged { node, .. } => node.validate(),
            Self::RuntimeInventoryRemoved { node_id, .. } => {
                if !valid_runtime_id(node_id, 128) {
                    return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
                }
                Ok(())
            }
            Self::Lagged { .. } => Ok(()),
            Self::Ping { .. } => Ok(()),
        }
    }
}

/// Long-lived, server-push counterpart to a `SubscribeTerminal` connection --
/// see `HarnessOperatorEventV1`'s own doc comment for the shared framing this
/// also uses. Deliberately its own type, not a variant of
/// `HarnessOperatorEventV1`: that type's `Lagged`/`SnapshotBaseline` pair
/// promises a full task/run/node resync on overflow, which is both wrong for
/// a terminal frame (there is no "task/run/node" to resync, only a screen
/// that is already self-contained) and unaffordable at terminal-frame volume
/// (see `TerminalSubscriberRegistry`'s own doc comment for the coalescing
/// discipline this type's `coalesced_since_last` field reports on).
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessOperatorTerminalEventV1 {
    /// Sent once per session immediately after a successful subscribe (the
    /// tail of whatever `TerminalBufferRegistry` already holds, or nothing
    /// if the ring is empty for that session -- a legitimate state for a
    /// pane with no output yet), and again every time that session's ring
    /// gains a strictly newer frame. Always a full screen, never a delta
    /// (see backlog item 4) -- a client that missed intermediate frames is
    /// already caught up the moment this one arrives.
    TerminalFrame {
        sequence: u64,
        session: HarnessRuntimeSessionAddressV1,
        frame: HarnessRuntimeTerminalFrameV1,
        /// How many frames for THIS session were coalesced away (replaced
        /// before ever being sent) since the last frame actually delivered
        /// on this subscription. 0 on every frame delivered without
        /// contention, including the very first. Diagnostic only.
        coalesced_since_last: u32,
    },
    /// Same keep-alive rationale as `HarnessOperatorEventV1::Ping`: this is
    /// a physically separate connection with its own dead-peer-detection
    /// problem, independent of the task/run/node subscription's.
    Ping { sequence: u64 },
}

impl HarnessOperatorTerminalEventV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        match self {
            Self::TerminalFrame { session, frame, .. } => {
                session.validate()?;
                frame.validate()
            }
            Self::Ping { .. } => Ok(()),
        }
    }
}

/// One named catalog entry -- a selectable session mode or model on the
/// `ModeCatalog`/`ModelCatalog` chunk kinds. Exact mirror of
/// `gate4agent-node-protocol`'s own `AgentStreamNamedIdV1` (not a
/// `gate4agent_types` type -- it is minted on the node's own wire contract,
/// same duplication rationale as `HarnessTerminalControlV1`). `id` is what
/// `SetSessionMode`/`SetSessionModel` take back.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessAgentStreamNamedIdV1 {
    pub id: String,
    pub name: String,
    pub description: Option<String>,
}

/// One selectable answer to an `InteractionPrompt` chunk -- an ACP
/// permission option, named by the provider rather than invented here.
/// Exact mirror of `gate4agent-node-protocol`'s own
/// `AgentStreamInteractionOptionV1`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessAgentStreamInteractionOptionV1 {
    pub option_id: String,
    pub name: String,
    pub kind: String,
}

/// Exact mirror of `gate4agent_types::ProviderConfigOptionKind`.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessProviderConfigOptionKindV1 {
    Select,
    Boolean,
    Unknown,
}

/// Exact mirror of `gate4agent_types::ProviderConfigChoice`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessProviderConfigChoiceV1 {
    pub value_json: String,
    pub label: Option<String>,
}

/// Exact mirror of `gate4agent_types::ProviderConfigOption`: the mechanism
/// ACP uses to change model, reasoning effort, and similar settings.
/// `ConfigOptions` always carries the FULL current set, never a delta --
/// same contract as the source type's own doc comment states for
/// `ProviderEvent::ConfigOptionsUpdated`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessProviderConfigOptionV1 {
    pub id: String,
    pub name: String,
    pub description: Option<String>,
    pub category: Option<String>,
    pub kind: HarnessProviderConfigOptionKindV1,
    pub value_json: String,
    pub choices: Vec<HarnessProviderConfigChoiceV1>,
}

/// Exact mirror of `hatchery_observation_protocol::BlockAuthorityV1`: this
/// crate has no dependency on `gate4agent-observation-protocol` (see the
/// doc comment on `HarnessTerminalControlV1`), so the WHO/WHAT-blocked-it
/// vocabulary `HarnessAgentStreamChunkKindV1::Blocked` carries is duplicated
/// here as its own closed wire enum rather than imported, the same way
/// `HarnessProviderInteractionKindV1` mirrors `gate4agent_types::
/// ProviderInteractionKind`.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessBlockAuthorityV1 {
    HarnessGate,
    HarnessPolicy,
    HarnessDeadline,
    Operator,
    ProviderClassifier,
    ProviderPermissionRule,
    ProviderSandbox,
    ProviderRefusal,
    ProviderHook,
    UserRejected,
    /// Mirrors `hatchery_observation_protocol::BlockAuthorityV1::
    /// ProviderQuota` -- the provider's own account/plan quota, rate
    /// limit, or usage cap was exhausted.
    ProviderQuota,
    Unknown,
}

/// The kind of a single `HarnessAgentStreamChunkV1` -- content the operator
/// needs to act on a running ACP session: what the agent is saying, a
/// pending interaction it needs answered, and the catalogs the three ACP
/// setter verbs (`SetSessionMode`, `SetSessionConfigOption`,
/// `SetSessionModel`) operate over. Exact mirror of
/// `gate4agent-node-protocol`'s own `AgentStreamChunkKindV1`, which itself
/// mirrors `gate4agent_types::ProviderEvent`'s content variants
/// deliberately, in contrast to the collapsed `ObservationKindV1` telemetry
/// stream, which carries none of it
/// (`docs/gate4agent/plans/gate4agent-acp-control-plane-on-the-wire-2026-09-02.md`
/// §3-4).
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessAgentStreamChunkKindV1 {
    Text { text: String, is_delta: bool },
    Thinking { text: String },
    /// The same `correlation_id` `ObservationKindV1::ApprovalRequested`/
    /// `QuestionRequested` already minted, plus the question and option
    /// list `ResolveInteraction` answers blind without.
    InteractionPrompt {
        correlation_id: String,
        interaction_kind: HarnessProviderInteractionKindV1,
        tool_name: String,
        title: Option<String>,
        prompt: String,
        options: Vec<HarnessAgentStreamInteractionOptionV1>,
    },
    ModeCatalog {
        current: Option<String>,
        available: Vec<HarnessAgentStreamNamedIdV1>,
    },
    ConfigOptions { options: Vec<HarnessProviderConfigOptionV1> },
    ModelCatalog {
        current: Option<String>,
        available: Vec<HarnessAgentStreamNamedIdV1>,
    },
    /// The chunk twin of `ObservationKindV1::ActionBlocked`, delivered on
    /// the agent content stream instead of (never in place of) the
    /// observation channel -- see `gate4agent-node-protocol`'s own
    /// `AgentStreamChunkKindV1::Blocked` doc comment for why. An EVENT, not
    /// state: never seeded to a fresh subscriber, exactly like `Text`/
    /// `Thinking`.
    Blocked {
        correlation_id: Option<String>,
        tool_class: String,
        authority: HarnessBlockAuthorityV1,
        reason_kind: Option<String>,
        reason: String,
        help: Option<String>,
    },
}

impl HarnessAgentStreamChunkKindV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        match self {
            Self::Text { text, .. } | Self::Thinking { text } => {
                if text.len() > HARNESS_AGENT_STREAM_TEXT_MAX_BYTES
                    || contains_unsafe_control_bytes(text)
                {
                    return Err(HarnessOperatorApiError::InvalidAgentStream);
                }
                Ok(())
            }
            Self::InteractionPrompt { correlation_id, tool_name, title, prompt, options, .. } => {
                if !valid_agent_stream_correlation_id(correlation_id) {
                    return Err(HarnessOperatorApiError::InvalidAgentStream);
                }
                if !valid_agent_stream_id(tool_name) {
                    return Err(HarnessOperatorApiError::InvalidAgentStream);
                }
                if let Some(title) = title {
                    if title.len() > HARNESS_AGENT_STREAM_TEXT_MAX_BYTES
                        || contains_unsafe_control_bytes(title)
                    {
                        return Err(HarnessOperatorApiError::InvalidAgentStream);
                    }
                }
                if prompt.len() > HARNESS_AGENT_STREAM_TEXT_MAX_BYTES
                    || contains_unsafe_control_bytes(prompt)
                {
                    return Err(HarnessOperatorApiError::InvalidAgentStream);
                }
                if options.len() > HARNESS_AGENT_STREAM_INTERACTION_OPTIONS_MAX {
                    return Err(HarnessOperatorApiError::InvalidAgentStream);
                }
                for option in options {
                    if !valid_agent_stream_id(&option.option_id)
                        || !valid_agent_stream_id(&option.name)
                        || !valid_agent_stream_id(&option.kind)
                    {
                        return Err(HarnessOperatorApiError::InvalidAgentStream);
                    }
                }
                Ok(())
            }
            Self::ModeCatalog { current, available } | Self::ModelCatalog { current, available } => {
                if let Some(current) = current {
                    if !valid_agent_stream_id(current) {
                        return Err(HarnessOperatorApiError::InvalidAgentStream);
                    }
                }
                validate_agent_stream_catalog_entries(available)
            }
            Self::ConfigOptions { options } => {
                if options.len() > HARNESS_AGENT_STREAM_CATALOG_ENTRIES_MAX {
                    return Err(HarnessOperatorApiError::InvalidAgentStream);
                }
                for option in options {
                    if !valid_agent_stream_id(&option.id) || !valid_agent_stream_id(&option.name) {
                        return Err(HarnessOperatorApiError::InvalidAgentStream);
                    }
                    if let Some(description) = &option.description {
                        if description.len() > HARNESS_AGENT_STREAM_TEXT_MAX_BYTES
                            || contains_unsafe_control_bytes(description)
                        {
                            return Err(HarnessOperatorApiError::InvalidAgentStream);
                        }
                    }
                    if let Some(category) = &option.category {
                        if !valid_agent_stream_id(category) {
                            return Err(HarnessOperatorApiError::InvalidAgentStream);
                        }
                    }
                    if option.value_json.len() > HARNESS_AGENT_STREAM_TEXT_MAX_BYTES
                        || contains_unsafe_control_bytes(&option.value_json)
                    {
                        return Err(HarnessOperatorApiError::InvalidAgentStream);
                    }
                    if option.choices.len() > HARNESS_AGENT_STREAM_INTERACTION_OPTIONS_MAX {
                        return Err(HarnessOperatorApiError::InvalidAgentStream);
                    }
                    for choice in &option.choices {
                        if choice.value_json.len() > HARNESS_AGENT_STREAM_TEXT_MAX_BYTES
                            || contains_unsafe_control_bytes(&choice.value_json)
                        {
                            return Err(HarnessOperatorApiError::InvalidAgentStream);
                        }
                        if let Some(label) = &choice.label {
                            if label.len() > HARNESS_AGENT_STREAM_TEXT_MAX_BYTES
                                || contains_unsafe_control_bytes(label)
                            {
                                return Err(HarnessOperatorApiError::InvalidAgentStream);
                            }
                        }
                    }
                }
                Ok(())
            }
            Self::Blocked { correlation_id, tool_class, reason_kind, reason, help, .. } => {
                if let Some(correlation_id) = correlation_id {
                    if !valid_agent_stream_correlation_id(correlation_id) {
                        return Err(HarnessOperatorApiError::InvalidAgentStream);
                    }
                }
                if !valid_agent_stream_id(tool_class) {
                    return Err(HarnessOperatorApiError::InvalidAgentStream);
                }
                if let Some(reason_kind) = reason_kind {
                    if !valid_agent_stream_id(reason_kind) {
                        return Err(HarnessOperatorApiError::InvalidAgentStream);
                    }
                }
                if reason.is_empty()
                    || reason.len() > HARNESS_AGENT_STREAM_TEXT_MAX_BYTES
                    || contains_unsafe_control_bytes(reason)
                {
                    return Err(HarnessOperatorApiError::InvalidAgentStream);
                }
                if let Some(help) = help {
                    if help.is_empty()
                        || help.len() > HARNESS_AGENT_STREAM_TEXT_MAX_BYTES
                        || contains_unsafe_control_bytes(help)
                    {
                        return Err(HarnessOperatorApiError::InvalidAgentStream);
                    }
                }
                Ok(())
            }
        }
    }
}

fn validate_agent_stream_catalog_entries(
    entries: &[HarnessAgentStreamNamedIdV1],
) -> Result<(), HarnessOperatorApiError> {
    if entries.len() > HARNESS_AGENT_STREAM_CATALOG_ENTRIES_MAX {
        return Err(HarnessOperatorApiError::InvalidAgentStream);
    }
    for entry in entries {
        if !valid_agent_stream_id(&entry.id) || !valid_agent_stream_id(&entry.name) {
            return Err(HarnessOperatorApiError::InvalidAgentStream);
        }
        if let Some(description) = &entry.description {
            if description.len() > HARNESS_AGENT_STREAM_TEXT_MAX_BYTES
                || contains_unsafe_control_bytes(description)
            {
                return Err(HarnessOperatorApiError::InvalidAgentStream);
            }
        }
    }
    Ok(())
}

/// One chunk of the outbound agent content stream -- the operator-wire
/// counterpart of `gate4agent-node-protocol`'s `AgentStreamChunkV1`, itself
/// mirroring `NodeEvent::TerminalFrame`'s own precedent: its own
/// subscription, its own type, no `ObservationV1` resync promise (see
/// `HarnessOperatorAgentEventV1`'s doc comment below). `source_sequence`
/// orders chunks within one provider source the way
/// `ObservationV1::source_sequence` orders observations.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessAgentStreamChunkV1 {
    pub source_sequence: u64,
    pub kind: HarnessAgentStreamChunkKindV1,
}

impl HarnessAgentStreamChunkV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.kind.validate()
    }
}

/// Long-lived, server-push counterpart to a `SubscribeAgentStream`
/// connection -- see `HarnessOperatorEventV1`'s own doc comment for the
/// shared framing this also uses. Deliberately its own type, not a variant
/// of `HarnessOperatorEventV1` and not a variant of
/// `HarnessOperatorTerminalEventV1` either, for two separate reasons this
/// stream's overflow story below has to hold at once:
///
/// - Like `HarnessOperatorTerminalEventV1`, `HarnessOperatorEventV1`'s
///   `Lagged`/`SnapshotBaseline` pair is wrong here: there is no
///   task/run/node to resync, only content that is already self-contained.
/// - Unlike `HarnessOperatorTerminalEventV1`, a dropped chunk here cannot be
///   waved off with a diagnostic counter either. A terminal frame is always
///   a full screen, so `TerminalFrame`'s own `coalesced_since_last` can
///   silently supersede a stale frame with a fresher one and lose nothing
///   real. An agent-stream chunk has no such property: a `Text`/`Thinking`
///   delta or an `InteractionPrompt` is a fact about one instant, not a
///   snapshot of the whole session, so a chunk this stream drops is gone --
///   there is no later chunk that already contains it, and dropping an
///   `InteractionPrompt` in particular would strand the operator unable to
///   ever answer a correlation id they never saw.
///
/// So overflow here gets its own honest shape: `Lagged` reports a real,
/// unrecoverable loss for one subscribed session -- no snapshot follows it,
/// because none could repair what was lost -- and the client's only correct
/// response is to surface that loss to the operator, not to wait for a
/// recovery frame that is never coming.
///
/// `ReplayBoundary` is a separate, narrower concession to the same
/// "instants are gone once missed" rule: a subscriber routinely registers
/// seconds after a session's first turn already started (an operator can
/// only address a session that has been spawned), and every `Text`/
/// `Thinking`/`Blocked` chunk published into that gap used to be gone for
/// good. The harness now keeps a small bounded ring of the most recent ones
/// per session and replays it once at subscribe time, immediately marked by
/// this event so it is never mistaken for live content -- see this field's
/// own doc comment on `AgentChunk::published_at_ms` for how a replayed
/// chunk keeps proving it is history, not now.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessOperatorAgentEventV1 {
    AgentChunk {
        sequence: u64,
        session: HarnessRuntimeSessionAddressV1,
        chunk: HarnessAgentStreamChunkV1,
        /// The harness's own wall-clock reading of the moment this chunk
        /// was first published to `AgentStreamSubscriberRegistry`
        /// (`hatchery-harness-service::agent_stream`), carried unchanged
        /// through every delivery path -- live, state seed, and the bounded
        /// replay `ReplayBoundary` marks the end of. A replayed chunk keeps
        /// the timestamp it was born with here; nothing ever restamps it to
        /// the moment it happened to be replayed.
        published_at_ms: u64,
    },
    /// This subscriber's outbound queue overflowed for the named session and
    /// `dropped` chunks were lost for it specifically -- other subscribers,
    /// and this subscriber's other subscribed sessions, are unaffected. See
    /// this type's own doc comment for why there is no recovery frame to
    /// follow it.
    Lagged {
        sequence: u64,
        session: HarnessRuntimeSessionAddressV1,
        dropped: u64,
    },
    /// Sent at most once per `SubscribeAgentStream` subscription per
    /// session, right after that session's replayed instant chunks
    /// (`Text`/`Thinking`/`Blocked` pulled from the harness's own bounded
    /// replay ring -- see `hatchery-harness-service::agent_stream`'s own
    /// doc comment) and before any live chunk for that session: the marker
    /// that lets the operator tell "recent history it missed" apart from
    /// "happening right now". Sent for every session the harness has
    /// recorded ANYTHING for at all (state, replay, or both), even if the
    /// ring itself turned out empty for it (`replayed: 0`) -- but never for
    /// a session the harness has not seen even once, since there is nothing
    /// to mark a boundary against there and the first thing that subscriber
    /// sees for it will just be live content, unambiguously so.
    /// `dropped_before_replay` is the ring's own running eviction count --
    /// nonzero here means the replay itself is truncated, and it names that
    /// gap rather than silently handing back a partial history as if it
    /// were the whole one.
    ReplayBoundary {
        session: HarnessRuntimeSessionAddressV1,
        replayed: u32,
        dropped_before_replay: u64,
    },
    /// Same keep-alive rationale as `HarnessOperatorTerminalEventV1::Ping`:
    /// this is a physically separate connection with its own dead-peer-
    /// detection problem, independent of the terminal and task/run/node
    /// subscriptions' own.
    Ping { sequence: u64 },
}

impl HarnessOperatorAgentEventV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        match self {
            Self::AgentChunk { session, chunk, .. } => {
                session.validate()?;
                chunk.validate()
            }
            Self::Lagged { session, .. } => session.validate(),
            Self::ReplayBoundary { session, .. } => session.validate(),
            Self::Ping { .. } => Ok(()),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessMcpGrantMintV1 {
    pub grant_id: SessionGrantId,
    pub revision: HarnessRevision,
    /// The same dispatch `StartTaskV2` returns. Present so the host can
    /// notify subscribers; `hatchery-harnessctl grant mint` does not print it.
    pub dispatch: HarnessDispatchIntentV1,
    pub replayed: bool,
}

impl HarnessMcpGrantMintV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.grant_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.revision.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.dispatch.validate().map_err(HarnessOperatorApiError::Protocol)?;
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", content = "value", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessOperatorResponseV1 {
    Monitor(SessionMonitorV1),
    Timeline(TimelinePageV1),
    Tasks(TaskPageV1),
    Task(RedactedTaskV1),
    TaskOperations(Vec<HarnessOperationLedgerEntryV1>),
    Runs(RunPageV1),
    Run(RedactedRunV1),
    RunCorrelation(HarnessRunCorrelationV1),
    RunTransfer(HarnessRunTransferSummaryV1),
    ReverseAttribution(HarnessReverseAttributionV1),
    RunContextSourceObserved(HarnessRunContextSourceObservationV1),
    RunWorkspaceInspected(HarnessRunWorkspaceInspectionV1),
    RunWorkspaceFileRead(HarnessRunWorkspaceFileV1),
    RunGitHistoryRead(HarnessRunGitHistoryPageV1),
    RunGitDiffRead(HarnessRunGitDiffV1),
    NodeWorkspaceInspected(HarnessNodeWorkspaceInspectionV1),
    NodeWorkspaceFileRead(HarnessNodeWorkspaceFileV1),
    NodeGitHistoryRead(HarnessNodeGitHistoryPageV1),
    NodeGitDiffRead(HarnessNodeGitDiffV1),
    // V11 write/create siblings of the three node-workspace read responses
    // above: same payload shapes the node's own `WorkspaceFileWritten`/
    // `WorkspaceFileCreated`/`WorkspaceDirectoryCreated` replies carry,
    // projected the same way `NodeWorkspaceFileRead` already is.
    NodeWorkspaceFileWritten(HarnessNodeWorkspaceFileV1),
    NodeWorkspaceFileCreated(HarnessNodeWorkspaceFileV1),
    NodeWorkspaceDirectoryCreated(HarnessNodeWorkspaceDirectoryV1),
    LaunchPlans(HarnessLaunchPlanPageV1),
    TaskExecutionSpec(Option<HarnessTaskExecutionSpecV1>),
    TaskLaunchOptions(HarnessTaskLaunchOptionsV1),
    RuntimeInventory(HarnessRuntimeInventoryPageV1),
    TerminalRead(HarnessRuntimeTerminalPageV1),
    SessionSpawned(HarnessRuntimeSessionAddressV1),
    SessionInputWritten,
    SessionResized,
    SessionStopped,
    SessionControlled,
    SessionBytesWritten,
    SessionPasted,
    SessionRemoved,
    SessionResumed,
    // `PromptSession`'s ack -- same bare shape as `SessionInputWritten`
    // above, because the node's own `NodeRequest::Prompt` handler acks with
    // a bare `NodeResponse::Accepted` too. See `HarnessOperatorRequestV1::
    // PromptSession`'s own doc comment.
    SessionPrompted,
    // Unit acks for the four ACP control verbs above -- same bare-ack shape
    // as the eight session-control acks immediately above, because the node
    // itself acks all four with its own bare `NodeResponse::Accepted`.
    InteractionResolved,
    SessionModeSet,
    SessionConfigOptionSet,
    SessionModelSet,
    NativeSessionsCataloged(HarnessNativeSessionsCatalogedV1),
    NativeSessionsPaged(HarnessNativeSessionsPagedV1),
    NativeSessionPreviewed(HarnessNativeSessionPreviewedV1),
    SessionRecordPreviewed(HarnessSessionRecordPreviewedV1),
    SessionRecordResumed(HarnessSessionRecordResumedV1),
    SessionRecordUpdated(HarnessRuntimeManagedSessionV1),
    SessionRecordForgotten { record_id: String },
    ProviderSessionIndexed(HarnessRuntimeManagedSessionV1),
    NativeSessionIndexed(HarnessNativeSessionIndexedV1),
    HostDirectoriesBrowsed(HarnessHostDirectoryListingV1),
    WorkspaceRegistered(HarnessWorkspaceSnapshotV1),
    StandaloneWorkspaceCreated(HarnessWorkspaceSnapshotV1),
    WorkspaceUnregistered { workspace_id: String },
    WorktreeCreated {
        worktree: HarnessGitWorktreeSnapshotV1,
        workspace: HarnessWorkspaceSnapshotV1,
    },
    WorktreeRemoved {
        target_root: HarnessHostPathV1,
        workspace_id: Option<String>,
    },
    ContextPackExported(HarnessResolvedContextPackReceiptV1),
    ContextPackForgotten { context_id: String },
    Mutation(HarnessOperatorMutationOutcomeV1),
    ExecutionSpecMutation(HarnessOperatorMutationOutcomeV1),
    Schedule(HarnessScheduleOutcomeV1),
    TaskStarted(HarnessTaskStartOutcomeV1),
    /// Grant id and revision from `resolve_harness_mcp_grant`. No bearer
    /// credential: `HarnessReadCredential`'s `Serialize` writes the raw
    /// token, and this command must not.
    HarnessMcpGrantMinted(HarnessMcpGrantMintV1),
}

impl HarnessOperatorResponseV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        match self {
            Self::Monitor(value) => value.validate().map_err(HarnessOperatorApiError::Read),
            Self::Timeline(value) => value.validate().map_err(HarnessOperatorApiError::Read),
            Self::Tasks(value) => value.validate().map_err(HarnessOperatorApiError::Read),
            Self::Task(value) => value.validate().map_err(HarnessOperatorApiError::Read),
            Self::TaskOperations(entries) => {
                if entries.len() > usize::from(HARNESS_ENTITY_PAGE_LIMIT_MAX) {
                    return Err(HarnessOperatorApiError::InvalidOperationLedger);
                }
                for entry in entries {
                    entry.validate()?;
                }
                Ok(())
            }
            Self::Runs(value) => value.validate().map_err(HarnessOperatorApiError::Read),
            Self::Run(value) => value.validate().map_err(HarnessOperatorApiError::Read),
            Self::RunCorrelation(value) => value.validate(),
            Self::RunTransfer(value) => value.validate(),
            Self::ReverseAttribution(value) => value.validate(),
            Self::RunContextSourceObserved(value) => value.validate(),
            Self::RunWorkspaceInspected(value) => value.validate(),
            Self::RunWorkspaceFileRead(value) => value.validate(),
            Self::RunGitHistoryRead(value) => value.validate(),
            Self::RunGitDiffRead(value) => value.validate(),
            Self::NodeWorkspaceInspected(value) => value.validate(),
            Self::NodeWorkspaceFileRead(value) => value.validate(),
            Self::NodeGitHistoryRead(value) => value.validate(),
            Self::NodeGitDiffRead(value) => value.validate(),
            Self::NodeWorkspaceFileWritten(value) | Self::NodeWorkspaceFileCreated(value) => {
                value.validate()
            }
            Self::NodeWorkspaceDirectoryCreated(value) => value.validate(),
            Self::LaunchPlans(value) => value.validate(),
            Self::TaskExecutionSpec(value) => {
                if let Some(value) = value {
                    value.validate().map_err(HarnessOperatorApiError::Protocol)?;
                }
                Ok(())
            }
            Self::TaskLaunchOptions(value) => value.validate(),
            Self::RuntimeInventory(value) => value.validate(),
            Self::TerminalRead(value) => value.validate(),
            Self::SessionSpawned(value) => value.validate(),
            Self::SessionInputWritten
            | Self::SessionResized
            | Self::SessionStopped
            | Self::SessionControlled
            | Self::SessionBytesWritten
            | Self::SessionPasted
            | Self::SessionRemoved
            | Self::SessionResumed
            | Self::SessionPrompted
            | Self::InteractionResolved
            | Self::SessionModeSet
            | Self::SessionConfigOptionSet
            | Self::SessionModelSet => Ok(()),
            Self::NativeSessionsCataloged(value) => value.validate(),
            Self::NativeSessionsPaged(value) => value.validate(),
            Self::NativeSessionPreviewed(value) => value.validate(),
            Self::SessionRecordPreviewed(value) => value.validate(),
            Self::SessionRecordResumed(value) => value.validate(),
            Self::SessionRecordUpdated(value) | Self::ProviderSessionIndexed(value) => {
                value.validate()
            }
            Self::SessionRecordForgotten { record_id } => {
                if !valid_runtime_id(record_id, 128) {
                    return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
                }
                Ok(())
            }
            Self::NativeSessionIndexed(value) => value.validate(),
            Self::HostDirectoriesBrowsed(value) => value.validate(),
            Self::WorkspaceRegistered(value) | Self::StandaloneWorkspaceCreated(value) => {
                value.validate()
            }
            Self::WorkspaceUnregistered { workspace_id } => {
                if !valid_runtime_id(workspace_id, 128) {
                    return Err(HarnessOperatorApiError::InvalidResourceMutationRequest);
                }
                Ok(())
            }
            Self::WorktreeCreated { worktree, workspace } => {
                worktree.validate()?;
                workspace.validate()
            }
            Self::WorktreeRemoved { target_root, workspace_id } => {
                target_root.validate()?;
                if let Some(workspace_id) = workspace_id {
                    if !valid_runtime_id(workspace_id, 128) {
                        return Err(HarnessOperatorApiError::InvalidResourceMutationRequest);
                    }
                }
                Ok(())
            }
            Self::ContextPackExported(value) => {
                value.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::ContextPackForgotten { context_id } => {
                if !valid_runtime_id(context_id, 128) {
                    return Err(HarnessOperatorApiError::InvalidResourceMutationRequest);
                }
                Ok(())
            }
            Self::Mutation(_) | Self::ExecutionSpecMutation(_) => Ok(()),
            Self::Schedule(HarnessScheduleOutcomeV1::Idle) => Ok(()),
            Self::Schedule(HarnessScheduleOutcomeV1::Dispatch(value)) => {
                value.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::TaskStarted(value) => {
                value.validate().map_err(HarnessOperatorApiError::Protocol)
            }
            Self::HarnessMcpGrantMinted(value) => value.validate(),
        }
    }
}

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
pub struct HarnessNodeIncarnationV1(HarnessSelectorV1);

impl HarnessNodeIncarnationV1 {
    pub fn new(value: impl Into<String>) -> Result<Self, HarnessOperatorApiError> {
        let value = value.into();
        if value.len() != 32 || !value.bytes().all(is_lower_hex) {
            return Err(HarnessOperatorApiError::InvalidRunCorrelation);
        }
        HarnessSelectorV1::new(value)
            .map(Self)
            .map_err(HarnessOperatorApiError::Protocol)
    }

    pub fn as_str(&self) -> &str { self.0.as_str() }

    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.0.validate().map_err(HarnessOperatorApiError::Protocol)?;
        if self.0.as_str().len() != 32 || !self.0.as_str().bytes().all(is_lower_hex) {
            return Err(HarnessOperatorApiError::InvalidRunCorrelation);
        }
        Ok(())
    }
}

impl<'de> Deserialize<'de> for HarnessNodeIncarnationV1 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        Self::new(String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessRunWorktreeViewV1 {
    Existing,
    Managed { worktree_ref: HarnessSelectorV1 },
}

impl HarnessRunWorktreeViewV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        match self {
            Self::Existing => Ok(()),
            Self::Managed { worktree_ref } => {
                worktree_ref.validate().map_err(HarnessOperatorApiError::Protocol)
            }
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessManagedRunSessionV1 {
    pub record_id: HarnessSelectorV1,
    pub active_session: Option<HarnessRuntimeIdentityV1>,
}

impl HarnessManagedRunSessionV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.record_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        if let Some(active_session) = &self.active_session {
            active_session.validate().map_err(HarnessOperatorApiError::Protocol)?;
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessInlineRunSessionV1 {
    pub inline_ref: HarnessInlineRef,
}

impl HarnessInlineRunSessionV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.inline_ref.validate().map_err(HarnessOperatorApiError::Protocol)
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", content = "value", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessRunSessionViewV1 {
    Managed(HarnessManagedRunSessionV1),
    Inline(HarnessInlineRunSessionV1),
}

impl HarnessRunSessionViewV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        match self {
            Self::Managed(value) => value.validate(),
            Self::Inline(value) => value.validate(),
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessRunCorrelationAvailabilityV1 {
    Available,
    Dormant,
    Unavailable,
    NotObserved,
    StaleIncarnation,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRunCorrelationV1 {
    pub run_id: HarnessRunId,
    pub run_revision: HarnessRevision,
    pub task_id: HarnessTaskId,
    pub node_id: HarnessSelectorV1,
    pub node_incarnation_id: HarnessNodeIncarnationV1,
    pub workspace_id: HarnessSelectorV1,
    pub provider_profile: HarnessSelectorV1,
    pub mode: HarnessExecutionModeV1,
    pub worktree: HarnessRunWorktreeViewV1,
    pub session: HarnessRunSessionViewV1,
    pub availability: HarnessRunCorrelationAvailabilityV1,
    pub observed_at_unix_ms: Option<u64>,
}

impl HarnessRunCorrelationV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.run_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.run_revision.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.task_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.node_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.node_incarnation_id.validate()?;
        self.workspace_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.provider_profile.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.worktree.validate()?;
        self.session.validate()?;
        if matches!(&self.session, HarnessRunSessionViewV1::Inline(_))
            && self.mode != HarnessExecutionModeV1::Inline
        {
            return Err(HarnessOperatorApiError::InvalidRunCorrelation);
        }
        if matches!(self.availability, HarnessRunCorrelationAvailabilityV1::NotObserved)
            != self.observed_at_unix_ms.is_none()
            || self.observed_at_unix_ms == Some(0)
        {
            return Err(HarnessOperatorApiError::InvalidRunCorrelation);
        }
        match (&self.session, self.availability) {
            (
                HarnessRunSessionViewV1::Managed(HarnessManagedRunSessionV1 {
                    active_session: Some(_),
                    ..
                }),
                HarnessRunCorrelationAvailabilityV1::Dormant,
            )
            | (
                HarnessRunSessionViewV1::Managed(HarnessManagedRunSessionV1 {
                    active_session: None,
                    ..
                }),
                HarnessRunCorrelationAvailabilityV1::Available,
            )
            | (
                HarnessRunSessionViewV1::Inline(_),
                HarnessRunCorrelationAvailabilityV1::Available
                    | HarnessRunCorrelationAvailabilityV1::Dormant,
            ) => Err(HarnessOperatorApiError::InvalidRunCorrelation),
            _ => Ok(()),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRunDeliveryTransferV1 {
    pub delivery_ref: HarnessDeliveryRef,
    pub revision: HarnessRevision,
    pub state: HarnessDeliveryStateV1,
    pub selector: HarnessSelectorV1,
    pub bundle_id: HarnessDeliveryBundleIdV1,
    pub bundle_revision: HarnessDeliveryBundleRevisionV1,
    pub bundle_digest: HarnessDeliveryBundleDigestV1,
    pub manifest_digest: HarnessDeliveryManifestDigestV2,
    pub receipt_ref: Option<HarnessReceiptRef>,
    pub created_at_unix_ms: u64,
    pub updated_at_unix_ms: u64,
    pub staged_at_unix_ms: Option<u64>,
    pub committed_at_unix_ms: Option<u64>,
}

impl HarnessRunDeliveryTransferV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.delivery_ref.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.revision.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.selector.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.bundle_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.bundle_revision.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.bundle_digest.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.manifest_digest.validate().map_err(HarnessOperatorApiError::Protocol)?;
        if let Some(receipt_ref) = &self.receipt_ref {
            receipt_ref.validate().map_err(HarnessOperatorApiError::Protocol)?;
        }
        if !valid_transfer_timestamps(
            self.created_at_unix_ms,
            self.updated_at_unix_ms,
            [self.staged_at_unix_ms, self.committed_at_unix_ms],
        ) {
            return Err(HarnessOperatorApiError::InvalidRunTransfer);
        }
        let state_fields = match self.state {
            HarnessDeliveryStateV1::Prepared => {
                self.staged_at_unix_ms.is_none()
                    && self.committed_at_unix_ms.is_none()
                    && self.receipt_ref.is_none()
            }
            HarnessDeliveryStateV1::Staged => {
                self.staged_at_unix_ms.is_some()
                    && self.committed_at_unix_ms.is_none()
                    && self.receipt_ref.is_none()
            }
            HarnessDeliveryStateV1::Committed => {
                self.staged_at_unix_ms.is_some()
                    && self.committed_at_unix_ms.is_some()
                    && self.receipt_ref.is_some()
            }
        };
        if !state_fields
            || matches!(
                (self.staged_at_unix_ms, self.committed_at_unix_ms),
                (Some(staged), Some(committed)) if committed < staged
            )
        {
            return Err(HarnessOperatorApiError::InvalidRunTransfer);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRunContextTransferV1 {
    pub context_ref: HarnessSelectorV1,
    pub digest: String,
    pub source_message_count: u64,
    pub retained_message_count: u64,
    pub byte_len: u32,
    pub truncated: bool,
}

impl HarnessRunContextTransferV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.context_ref.validate().map_err(HarnessOperatorApiError::Protocol)?;
        let valid_digest = self.digest.strip_prefix("sha256:").is_some_and(|digest| {
            digest.len() == 64 && digest.bytes().all(is_lower_hex)
        });
        if !valid_digest
            || self.source_message_count == 0
            || self.retained_message_count == 0
            || self.retained_message_count > self.source_message_count
            || self.retained_message_count > HARNESS_CONTEXT_PACK_RETAINED_MESSAGES_MAX
            || self.byte_len == 0
            || self.byte_len > HARNESS_CONTEXT_PACK_MAX_BYTES
            || self.truncated != (self.source_message_count > self.retained_message_count)
        {
            return Err(HarnessOperatorApiError::InvalidRunTransfer);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRunContinuationTransferV1 {
    pub continuation_ref: HarnessContinuationRef,
    pub receipt_ref: HarnessReceiptRef,
    pub revision: HarnessRevision,
    pub state: HarnessContinuationStateV1,
    pub source_run_id: HarnessRunId,
    pub target_run_id: HarnessRunId,
    pub source_provider: HarnessSelectorV1,
    pub context: Option<HarnessRunContextTransferV1>,
    pub prepared_at_unix_ms: u64,
    pub exporting_at_unix_ms: Option<u64>,
    pub exported_at_unix_ms: Option<u64>,
    pub bound_at_unix_ms: Option<u64>,
    pub expired_at_unix_ms: Option<u64>,
    pub outcome_unknown_at_unix_ms: Option<u64>,
    pub outcome_unknown_reason: Option<HarnessContinuationOutcomeUnknownReasonV1>,
    pub created_at_unix_ms: u64,
    pub updated_at_unix_ms: u64,
}

impl HarnessRunContinuationTransferV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.continuation_ref.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.receipt_ref.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.revision.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.source_run_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.target_run_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.source_provider.validate().map_err(HarnessOperatorApiError::Protocol)?;
        if let Some(context) = &self.context { context.validate()?; }
        if self.source_run_id == self.target_run_id
            || self.prepared_at_unix_ms != self.created_at_unix_ms
            || !valid_transfer_timestamps(
                self.created_at_unix_ms,
                self.updated_at_unix_ms,
                [
                    Some(self.prepared_at_unix_ms),
                    self.exporting_at_unix_ms,
                    self.exported_at_unix_ms,
                    self.bound_at_unix_ms,
                    self.expired_at_unix_ms,
                    self.outcome_unknown_at_unix_ms,
                ],
            )
        {
            return Err(HarnessOperatorApiError::InvalidRunTransfer);
        }
        let ordered = self.exporting_at_unix_ms
            .is_none_or(|value| value >= self.prepared_at_unix_ms)
            && self.exported_at_unix_ms.is_none_or(|value| {
                self.exporting_at_unix_ms.is_some_and(|started| value >= started)
            })
            && self.bound_at_unix_ms.is_none_or(|value| {
                self.exported_at_unix_ms.is_some_and(|exported| value >= exported)
            })
            && self.outcome_unknown_at_unix_ms.is_none_or(|value| {
                self.exporting_at_unix_ms.is_some_and(|started| value >= started)
            })
            && self.expired_at_unix_ms
                .is_none_or(|value| value >= self.prepared_at_unix_ms);
        let state_fields = match self.state {
            HarnessContinuationStateV1::Prepared => self.context.is_none()
                && self.exporting_at_unix_ms.is_none()
                && self.exported_at_unix_ms.is_none()
                && self.bound_at_unix_ms.is_none()
                && self.expired_at_unix_ms.is_none()
                && self.outcome_unknown_at_unix_ms.is_none()
                && self.outcome_unknown_reason.is_none(),
            HarnessContinuationStateV1::Exporting => self.context.is_none()
                && self.exporting_at_unix_ms.is_some()
                && self.exported_at_unix_ms.is_none()
                && self.bound_at_unix_ms.is_none()
                && self.expired_at_unix_ms.is_none()
                && self.outcome_unknown_at_unix_ms.is_none()
                && self.outcome_unknown_reason.is_none(),
            HarnessContinuationStateV1::Exported => self.context.is_some()
                && self.exporting_at_unix_ms.is_some()
                && self.exported_at_unix_ms.is_some()
                && self.bound_at_unix_ms.is_none()
                && self.expired_at_unix_ms.is_none()
                && self.outcome_unknown_at_unix_ms.is_none()
                && self.outcome_unknown_reason.is_none(),
            HarnessContinuationStateV1::Bound => self.context.is_some()
                && self.exporting_at_unix_ms.is_some()
                && self.exported_at_unix_ms.is_some()
                && self.bound_at_unix_ms.is_some()
                && self.expired_at_unix_ms.is_none()
                && self.outcome_unknown_at_unix_ms.is_none()
                && self.outcome_unknown_reason.is_none(),
            HarnessContinuationStateV1::OutcomeUnknown => self.exporting_at_unix_ms.is_some()
                && self.exported_at_unix_ms.is_none()
                && self.bound_at_unix_ms.is_none()
                && self.expired_at_unix_ms.is_none()
                && self.outcome_unknown_at_unix_ms.is_some()
                && self.outcome_unknown_reason.is_some(),
            HarnessContinuationStateV1::Expired => self.bound_at_unix_ms.is_none()
                && self.expired_at_unix_ms.is_some()
                && self.outcome_unknown_at_unix_ms.is_none()
                && self.outcome_unknown_reason.is_none(),
        };
        if !ordered || !state_fields {
            return Err(HarnessOperatorApiError::InvalidRunTransfer);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRunTransferSummaryV1 {
    pub run_id: HarnessRunId,
    pub run_revision: HarnessRevision,
    pub delivery: Option<HarnessRunDeliveryTransferV1>,
    pub continuation: Option<HarnessRunContinuationTransferV1>,
}

impl HarnessRunTransferSummaryV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.run_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.run_revision.validate().map_err(HarnessOperatorApiError::Protocol)?;
        if let Some(delivery) = &self.delivery { delivery.validate()?; }
        if let Some(continuation) = &self.continuation {
            continuation.validate()?;
            if continuation.target_run_id != self.run_id {
                return Err(HarnessOperatorApiError::InvalidRunTransfer);
            }
        }
        Ok(())
    }

    pub fn validate_for(&self, run_id: &HarnessRunId) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        if &self.run_id != run_id {
            return Err(HarnessOperatorApiError::InvalidRunTransfer);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRunContextSourceObservationV1 {
    pub run_id: HarnessRunId,
    pub run_revision: HarnessRevision,
    pub feature_state: FeatureObservationStateV1,
    pub message_count: u64,
    pub message_count_exact: bool,
    pub completed_turn_count: Option<u64>,
    pub total_tokens: Option<u64>,
    pub observed_at_unix_ms: Option<u64>,
}

impl HarnessRunContextSourceObservationV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.run_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.run_revision.validate().map_err(HarnessOperatorApiError::Protocol)?;
        let observed = self.feature_state == FeatureObservationStateV1::Observed;
        let observed_fields_valid = self.message_count > 0
            && self.message_count_exact
            && self.observed_at_unix_ms.is_some_and(|timestamp| timestamp > 0)
            && self.completed_turn_count.map_or(true, |count| count <= self.message_count);
        let unobserved_fields_empty = self.message_count == 0
            && !self.message_count_exact
            && self.completed_turn_count.is_none()
            && self.total_tokens.is_none()
            && self.observed_at_unix_ms.is_none();
        if observed && !observed_fields_valid || !observed && !unobserved_fields_empty {
            return Err(HarnessOperatorApiError::InvalidRunContextSourceObservation);
        }
        Ok(())
    }

    pub fn validate_for(&self, run_id: &HarnessRunId) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        if &self.run_id != run_id {
            return Err(HarnessOperatorApiError::InvalidRunContextSourceObservation);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRuntimeNodeInventoryV1 {
    pub node_id: String,
    pub incarnation_id: String,
    pub observed_at_unix_ms: u64,
    pub event_sequence: u64,
    pub inventory: HarnessRuntimeInventoryV1,
}

impl HarnessRuntimeNodeInventoryV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if !valid_runtime_id(&self.node_id, 128)
            || self.incarnation_id.len() != 32
            || !self.incarnation_id.bytes().all(is_lower_hex)
            || self.observed_at_unix_ms == 0
        {
            return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
        }
        self.inventory.validate()
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRuntimeInventoryV1 {
    pub enabled_providers: Vec<String>,
    pub workspaces: BTreeMap<String, HarnessRuntimeWorkspaceV1>,
    pub workspace_count: usize,
    pub workspaces_truncated: bool,
    pub session_count: usize,
    pub sessions_truncated: bool,
    pub managed_sessions: Vec<HarnessRuntimeManagedSessionV1>,
    pub managed_session_count: usize,
    pub managed_sessions_truncated: bool,
    /// Lifetime count of managed session records the node's own retention
    /// sweep has retired (mirrors `hatchery_core::SlimNodeInventory::
    /// retired_count`). Additive and informational only, `#[serde(default)]`
    /// so a payload from before this field existed still deserializes.
    #[serde(default)]
    pub retired_count: usize,
    // Operator-visible surface: no redaction beyond what the direct-C2 TUI
    // already shows for the same node (`NodeView::launch_inventory`). Mirrors
    // `gate4agent_node_protocol::LaunchInventory` field-for-field rather than
    // reusing it: `gate4agent-node-protocol` already depends on this crate
    // (for the shared Harness MCP wire types), so the reverse edge would be
    // a cyclic package dependency. The TUI reconstructs the real node-protocol
    // type from this mirror on receipt (`client.rs::project_harness_inventory_node`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub launch_inventory: Option<HarnessRuntimeLaunchInventoryV1>,
}

/// Same liveness-first rationale and exact rank table as c2-protocol's own
/// `managed_session_liveness_rank` (`hatchery_core::SlimNodeInventory`
/// page-cut ordering): `Live` ranks first, `IdentityPending` next, `Dormant`
/// next, terminal `Unavailable` last. The producer orders a page by this
/// rank before the `MAX_C2_MANAGED_SESSIONS_PER_NODE` cut, so a page is
/// never sorted by bare `record_id` alone -- validating strict ascending
/// `record_id` order regardless of state rejected every real page (fixed
/// after `ed4fa61` regressed this: see `managed_session_liveness_rank`'s
/// own doc comment in c2-protocol for why the producer's order changed).
fn managed_session_liveness_rank(state: &HarnessRuntimeManagedStateV1) -> u8 {
    match state {
        HarnessRuntimeManagedStateV1::Live => 0,
        HarnessRuntimeManagedStateV1::IdentityPending => 1,
        HarnessRuntimeManagedStateV1::Dormant => 2,
        HarnessRuntimeManagedStateV1::Unavailable => 3,
    }
}

impl HarnessRuntimeInventoryV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        let included_sessions = self.workspaces.values()
            .map(|workspace| workspace.sessions.len())
            .sum::<usize>();
        if self.enabled_providers.len() > 64
            || self.enabled_providers.windows(2).any(|pair| pair[0] >= pair[1])
            || self.enabled_providers.iter().any(|provider| !valid_runtime_id(provider, 128))
            || self.workspaces.len() > 32
            || included_sessions > 128
            || self.managed_sessions.len() > 128
            || self.workspace_count < self.workspaces.len()
            || self.session_count < included_sessions
            || self.managed_session_count < self.managed_sessions.len()
            || self.workspaces_truncated != (self.workspace_count > self.workspaces.len())
            || self.sessions_truncated != (self.session_count > included_sessions)
            || self.managed_sessions_truncated
                != (self.managed_session_count > self.managed_sessions.len())
            || self.managed_sessions.windows(2).any(|pair| {
                let left = (managed_session_liveness_rank(&pair[0].state), pair[0].record_id.as_str());
                let right = (managed_session_liveness_rank(&pair[1].state), pair[1].record_id.as_str());
                left >= right
            })
        {
            return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
        }
        for (workspace_id, workspace) in &self.workspaces {
            if workspace_id != &workspace.workspace_id { return Err(HarnessOperatorApiError::InvalidRuntimeInventory); }
            workspace.validate()?;
        }
        for record in &self.managed_sessions { record.validate()?; }
        if let Some(launch_inventory) = &self.launch_inventory { launch_inventory.validate()?; }
        Ok(())
    }
}

/// Mirror of `gate4agent_node_protocol::ResolvedEnvironmentProfileReceipt`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRuntimeEnvironmentProfileReceiptV1 {
    pub profile_id: String,
    pub profile_revision: String,
}

impl HarnessRuntimeEnvironmentProfileReceiptV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if !valid_runtime_id(&self.profile_id, 128) || !valid_runtime_id(&self.profile_revision, 128) {
            return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
        }
        Ok(())
    }
}

/// Mirror of `gate4agent_node_protocol::SpawnProfileSummary`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRuntimeSpawnProfileSummaryV1 {
    pub id: String,
    pub revision: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub environment_profile: Option<HarnessRuntimeEnvironmentProfileReceiptV1>,
}

impl HarnessRuntimeSpawnProfileSummaryV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if !valid_runtime_id(&self.id, 128) || !valid_runtime_id(&self.revision, 128) {
            return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
        }
        if let Some(environment_profile) = &self.environment_profile {
            environment_profile.validate()?;
        }
        Ok(())
    }
}

/// Mirror of `gate4agent_node_protocol::ResolvedBundleReceipt`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRuntimeBundleReceiptV1 {
    pub id: String,
    pub revision: String,
    pub digest: String,
}

impl HarnessRuntimeBundleReceiptV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if !valid_runtime_id(&self.id, 128)
            || !valid_runtime_id(&self.revision, 128)
            || !valid_sha256_digest(&self.digest)
        {
            return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
        }
        Ok(())
    }
}

/// Mirror of `gate4agent_node_protocol::LaunchInventory`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRuntimeLaunchInventoryV1 {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub spawn_profiles: Option<Vec<HarnessRuntimeSpawnProfileSummaryV1>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bundles: Option<Vec<HarnessRuntimeBundleReceiptV1>>,
}

impl HarnessRuntimeLaunchInventoryV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if self.spawn_profiles.is_none() && self.bundles.is_none() {
            return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
        }
        if let Some(profiles) = &self.spawn_profiles {
            if profiles.len() > HARNESS_RUNTIME_SPAWN_PROFILES_MAX {
                return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
            }
            for profile in profiles { profile.validate()?; }
            if profiles.iter().enumerate().any(|(index, profile)| {
                profiles[..index].iter().any(|existing| existing.id == profile.id)
            }) {
                return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
            }
        }
        if let Some(bundles) = &self.bundles {
            if bundles.len() > HARNESS_RUNTIME_LAUNCH_BUNDLES_MAX {
                return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
            }
            for bundle in bundles { bundle.validate()?; }
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRuntimeWorkspaceV1 {
    pub workspace_id: String,
    pub display_root: String,
    pub display_root_truncated: bool,
    pub sessions: Vec<HarnessRuntimeSessionV1>,
    pub session_count: usize,
    pub sessions_truncated: bool,
}

impl HarnessRuntimeWorkspaceV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if !valid_runtime_id(&self.workspace_id, 128)
            || self.display_root.len() > 1024
            || self.display_root.chars().any(char::is_control)
            || self.sessions.len() > 128
            || self.session_count < self.sessions.len()
            || self.sessions_truncated != (self.session_count > self.sessions.len())
            || self.sessions.windows(2).any(|pair| {
                (pair[0].instance_id, pair[0].generation)
                    >= (pair[1].instance_id, pair[1].generation)
            })
        {
            return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
        }
        for session in &self.sessions { session.validate()?; }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessRuntimeTransportV1 { Pty, Pipe, Acp }

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessRuntimeSessionStatusV1 { Registered, Starting, Running, Stopping, Exited, Failed }

/// Exact mirror of `gate4agent_types::TerminalControl`: this crate has no
/// dependency on `gate4agent-types` (see the doc comment on
/// `HARNESS_SESSION_BYTES_MAX_BYTES`), so `ControlSession`'s special-key
/// payload is duplicated here as its own closed wire enum rather than
/// imported. The variant set and names are kept in lockstep by hand --
/// `hatchery-harness-service`'s `c2::map_terminal_control` is the single
/// place that converts one into the other, so a variant added to one side
/// without the other fails to compile there. No `validate()` method: unlike
/// a bounded string or byte vector, a closed enum's own deserialization is
/// already the bound -- an unrecognized variant name is rejected before this
/// type is ever constructed.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessTerminalControlV1 {
    Interrupt,
    EndOfFile,
    ControlA,
    ControlB,
    ControlE,
    ControlF,
    ControlG,
    ControlH,
    ControlI,
    ControlJ,
    ControlK,
    ControlL,
    ControlM,
    ControlN,
    ControlO,
    ControlP,
    ControlQ,
    ControlR,
    ControlS,
    ControlT,
    ControlU,
    ControlV,
    ControlW,
    ControlX,
    ControlY,
    ControlZ,
    Enter,
    LineFeed,
    Escape,
    Backspace,
    Tab,
    BackTab,
    Insert,
    Delete,
    Home,
    End,
    PageUp,
    PageDown,
    ArrowUp,
    ArrowDown,
    ArrowRight,
    ArrowLeft,
    Function1,
    Function2,
    Function3,
    Function4,
    Function5,
    Function6,
    Function7,
    Function8,
    Function9,
    Function10,
    Function11,
    Function12,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRuntimeTerminalSizeV1 {
    pub rows: u16,
    pub columns: u16,
}

/// Mirrors `gate4agent_types::FOREGROUND_PROCESS_NAME_MAX_BYTES`, the bound
/// on `PtyScreenStateV1::NotAgent`'s `observed_process`. Hand-kept in step
/// with the source constant for the same reason the enum itself is
/// hand-duplicated below.
pub const HARNESS_SCREEN_STATE_PROCESS_MAX_BYTES: usize = 512;
/// Mirrors `gate4agent_types::PTY_SCREEN_GATE_NAME_MAX_BYTES`, the bound on
/// `Failing`'s `reason`.
pub const HARNESS_SCREEN_STATE_GATE_MAX_BYTES: usize = 128;
/// Mirrors `gate4agent_types::OPERATOR_GATE_OPTIONS_MAX`.
pub const HARNESS_GATE_OPTIONS_MAX: usize = 8;
/// Mirrors `gate4agent_types::OPERATOR_GATE_OPTION_TEXT_MAX_BYTES`.
pub const HARNESS_GATE_OPTION_TEXT_MAX_BYTES: usize = 128;
/// Mirrors `gate4agent_types::OPERATOR_GATE_PATH_MAX_BYTES`.
pub const HARNESS_GATE_PATH_MAX_BYTES: usize = 32_768;

/// Exact mirror of `gate4agent_types::OperatorGateKind`.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum OperatorGateKindV1 {
    WorkspaceTrust,
    HookTrust,
    Authentication,
    VendorUpdate,
    Onboarding,
    TerminalAppearance,
    ConfigurationMigration,
}

impl OperatorGateKindV1 {
    /// Mirrors `gate4agent_types::OperatorGateKind::label`.
    pub fn label(&self) -> &'static str {
        match self {
            Self::WorkspaceTrust => "workspace trust",
            Self::HookTrust => "hook trust review",
            Self::Authentication => "authentication",
            Self::VendorUpdate => "vendor update",
            Self::Onboarding => "onboarding",
            Self::TerminalAppearance => "terminal appearance setup",
            Self::ConfigurationMigration => "configuration migration",
        }
    }
}

/// Exact mirror of `gate4agent_types::OperatorGateSubject`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum OperatorGateSubjectV1 {
    Directory { path: Option<String> },
    Hooks { count: Option<u32> },
    McpServers,
    Account,
    ApiKey,
    Appearance,
    Unknown,
}

/// Exact mirror of `gate4agent_types::OperatorGateInput`.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum OperatorGateInputV1 {
    NumberedList,
    ArrowList,
    PressEnter,
    TextEntry,
    Unknown,
}

/// Exact mirror of `gate4agent_types::OperatorGateOptionSemantics`.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum OperatorGateOptionSemanticsV1 {
    Accept,
    Decline,
    Inspect,
    Exit,
    Unknown,
}

/// Exact mirror of `gate4agent_types::OperatorGateOption`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct OperatorGateOptionV1 {
    pub text: String,
    pub semantics: OperatorGateOptionSemanticsV1,
    pub selected: bool,
}

impl OperatorGateOptionV1 {
    fn is_valid(&self) -> bool {
        valid_native_single_line(&self.text, HARNESS_GATE_OPTION_TEXT_MAX_BYTES, true)
    }
}

/// Exact mirror of `gate4agent_types::OperatorGateState`: this crate has no
/// dependency on `gate4agent-types` (see the doc comment on
/// `HarnessTerminalControlV1`), so the node's gate classification is
/// duplicated here as its own closed wire shape rather than imported.
/// `hatchery-harness-service` is the single place that maps one into the
/// other (`map_operator_gate`), so a field added to one side without the
/// other fails to compile there.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct OperatorGateStateV1 {
    pub kind: OperatorGateKindV1,
    pub subject: OperatorGateSubjectV1,
    pub input: OperatorGateInputV1,
    pub options: Vec<OperatorGateOptionV1>,
}

impl OperatorGateStateV1 {
    fn is_valid(&self) -> bool {
        let subject_valid = match &self.subject {
            OperatorGateSubjectV1::Directory { path: Some(path) } => {
                valid_native_single_line(path, HARNESS_GATE_PATH_MAX_BYTES, true)
            }
            OperatorGateSubjectV1::Directory { path: None }
            | OperatorGateSubjectV1::Hooks { .. }
            | OperatorGateSubjectV1::McpServers
            | OperatorGateSubjectV1::Account
            | OperatorGateSubjectV1::ApiKey
            | OperatorGateSubjectV1::Appearance
            | OperatorGateSubjectV1::Unknown => true,
        };
        subject_valid
            && self.options.len() <= HARNESS_GATE_OPTIONS_MAX
            && self.options.iter().all(OperatorGateOptionV1::is_valid)
    }
}

/// Exact mirror of `gate4agent_types::PtyScreenState`: this crate has no
/// dependency on `gate4agent-types` (see the doc comment on
/// `HarnessTerminalControlV1`), so the node's screen-content classification
/// is duplicated here as its own closed wire enum rather than imported.
/// `hatchery-harness-service` is the single place that maps one into the
/// other, so a variant added to one side without the other fails to compile
/// there.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum PtyScreenStateV1 {
    #[default]
    Unknown,
    NotAgent { observed_process: String },
    OperatorGate { gate: OperatorGateStateV1 },
    Failing { reason: String },
    Ready,
}

impl PtyScreenStateV1 {
    /// Refuses only the three states that carry a finding -- mirrors
    /// `gate4agent_types::PtyScreenState::admits_blind_write` exactly, so a
    /// wire consumer gets the same answer as the node and no call site
    /// open-codes its own reading. See that method for why `Unknown` admits.
    pub fn admits_blind_write(&self) -> bool {
        !matches!(
            self,
            Self::OperatorGate { .. } | Self::NotAgent { .. } | Self::Failing { .. }
        )
    }

    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        let ok = match self {
            Self::Unknown | Self::Ready => true,
            Self::NotAgent { observed_process } => {
                valid_native_single_line(observed_process, HARNESS_SCREEN_STATE_PROCESS_MAX_BYTES, true)
            }
            Self::OperatorGate { gate } => gate.is_valid(),
            Self::Failing { reason } => {
                valid_native_single_line(reason, HARNESS_SCREEN_STATE_GATE_MAX_BYTES, true)
            }
        };
        if ok { Ok(()) } else { Err(HarnessOperatorApiError::InvalidRuntimeInventory) }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRuntimeSessionV1 {
    pub instance_id: u64,
    pub generation: u64,
    pub provider: String,
    pub transport: HarnessRuntimeTransportV1,
    pub status: HarnessRuntimeSessionStatusV1,
    pub process_id: Option<u32>,
    pub terminal_size: Option<HarnessRuntimeTerminalSizeV1>,
    pub operation_pending: bool,
    pub input_pending: bool,
    /// The node's current screen classification. Always populated by every
    /// server on this wire -- `Unknown` is a real classification, not an
    /// absence marker -- so `Some` is the only value ever actually sent;
    /// `Option`-wrapped only so a decoder never has to special-case a
    /// missing key.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub screen_state: Option<PtyScreenStateV1>,
}

impl HarnessRuntimeSessionV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if self.instance_id == 0
            || self.generation == 0
            || !valid_runtime_id(&self.provider, 128)
            || self.terminal_size.is_some_and(|size| size.rows == 0 || size.columns == 0)
        {
            return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
        }
        if let Some(screen_state) = &self.screen_state {
            screen_state.validate()?;
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessRuntimeMouseProtocolEncodingV1 { Default, Utf8, Sgr }

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRuntimeTerminalFrameV1 {
    pub sequence: u64,
    pub size: HarnessRuntimeTerminalSizeV1,
    pub cursor_row: u16,
    pub cursor_column: u16,
    pub formatted: Vec<u8>,
    pub scrollback_formatted: Vec<Vec<u8>>,
    pub alternate_screen: bool,
    pub mouse_protocol_enabled: bool,
    pub mouse_protocol_encoding: HarnessRuntimeMouseProtocolEncodingV1,
    /// Unix-epoch milliseconds when the source `TerminalFrame` was
    /// materialized (`gate4agent_types::TerminalFrame::produced_at_unix_ms`,
    /// stamped once at the PTY snapshot and carried unchanged through every
    /// relay hop, including the harness's own terminal ring buffer). An
    /// operator computing an age from this must account for clock skew
    /// between the node host and its own, not just transit/queue delay.
    /// `#[serde(default)]` so an operator client built before this field
    /// existed still decodes the frame -- it just can't answer "how stale".
    #[serde(default)]
    pub produced_at_unix_ms: u64,
    /// The screen classification stamped at the instant this frame's screen
    /// was materialized. Always populated -- `Unknown` is a real
    /// classification, not an absence marker -- so `Some` is the only value
    /// ever actually sent; `Option`-wrapped only so a decoder never has to
    /// special-case a missing key.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub screen_state: Option<PtyScreenStateV1>,
    /// Mirrors `gate4agent_types::TerminalFrame::bracketed_paste` onto the
    /// operator wire unconditionally. `None` here means only "the node
    /// hasn't captured a value yet" -- the source field's own genuine
    /// absence -- never a wire-version gate.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bracketed_paste: Option<bool>,
}
// NOTE: gate4agent_types::TerminalFrame::contents (plain-text render) is
// deliberately dropped on the wire -- hatchery-tui's apply_terminal_frame
// never reads it. No dead field on the wire.

impl HarnessRuntimeTerminalFrameV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if self.size.rows == 0 || self.size.columns == 0 {
            return Err(HarnessOperatorApiError::InvalidTerminalPage);
        }
        if self.scrollback_formatted.len() > HARNESS_TERMINAL_SCROLLBACK_LINES_MAX {
            return Err(HarnessOperatorApiError::InvalidTerminalPage);
        }
        let scrollback_bytes = self.scrollback_formatted.iter()
            .map(Vec::len)
            .fold(0usize, usize::saturating_add);
        if self.formatted.len().saturating_add(scrollback_bytes) > HARNESS_TERMINAL_FRAME_MAX_BYTES {
            return Err(HarnessOperatorApiError::InvalidTerminalPage);
        }
        if let Some(screen_state) = &self.screen_state {
            screen_state.validate()?;
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRuntimeSessionAddressV1 {
    pub node_id: String,
    pub incarnation_id: String,
    pub workspace_id: String,
    pub instance_id: u64,
    pub generation: u64,
}

impl HarnessRuntimeSessionAddressV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if !valid_runtime_id(&self.node_id, 128)
            || self.incarnation_id.len() != 32
            || !self.incarnation_id.bytes().all(is_lower_hex)
            || !valid_runtime_id(&self.workspace_id, 128)
            || self.instance_id == 0
            || self.generation == 0
        {
            return Err(HarnessOperatorApiError::InvalidTerminalPage);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRuntimeTerminalPageV1 {
    pub session: HarnessRuntimeSessionAddressV1,
    pub frames: Vec<HarnessRuntimeTerminalFrameV1>,
    pub dropped: u64,
    pub transport_incomplete: bool,
    pub next_cursor: Option<u64>,
}

impl HarnessRuntimeTerminalPageV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.session.validate()?;
        if self.frames.len() > usize::from(HARNESS_TERMINAL_PAGE_LIMIT_MAX)
            || self.frames.windows(2).any(|pair| pair[0].sequence >= pair[1].sequence)
            || self.next_cursor == Some(0)
        {
            return Err(HarnessOperatorApiError::InvalidTerminalPage);
        }
        if self.next_cursor.is_some()
            && self.next_cursor != self.frames.last().map(|frame| frame.sequence)
        {
            return Err(HarnessOperatorApiError::InvalidTerminalPage);
        }
        for frame in &self.frames { frame.validate()?; }
        Ok(())
    }

    pub fn validate_for(
        &self,
        session: &HarnessRuntimeSessionAddressV1,
    ) -> Result<(), HarnessOperatorApiError> {
        self.validate()?;
        if &self.session != session {
            return Err(HarnessOperatorApiError::InvalidTerminalPage);
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessRuntimeManagedModeV1 { Pty, Inline, Acp }

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessRuntimeManagedStateV1 { IdentityPending, Live, Dormant, Unavailable }

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRuntimeSessionBindingV1 {
    pub workspace_id: String,
    pub instance_id: u64,
    pub generation: u64,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRuntimeManagedSessionV1 {
    pub record_id: String,
    pub display_name: String,
    pub display_name_truncated: bool,
    pub provider: String,
    pub mode: HarnessRuntimeManagedModeV1,
    pub state: HarnessRuntimeManagedStateV1,
    pub workspace_id: String,
    pub active_binding: Option<HarnessRuntimeSessionBindingV1>,
    pub provider_identity_present: bool,
    pub updated_at_unix_ms: u64,
    /// Count of `ObservationKindV1::ActionBlocked` observations recorded
    /// against this managed session, from the harness's own per-session
    /// observation projection -- see
    /// `hatchery_observation_engine::SessionProjection::blocked_count`.
    /// The fleet-wide instrument this exists for: one `runtime-inventory`
    /// read answers "how many sessions are stuck behind a gate right now"
    /// across every node at once.
    pub blocked_count: u64,
    /// `received_at_ms` of the most recent such observation, or `None` when
    /// `blocked_count` is zero.
    pub last_blocked_at_ms: Option<u64>,
}

impl HarnessRuntimeManagedSessionV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if !valid_runtime_id(&self.record_id, 128)
            || !valid_runtime_id(&self.provider, 128)
            || !valid_runtime_id(&self.workspace_id, 128)
            || self.display_name.is_empty()
            || self.display_name.len() > 256
            || self.display_name.chars().any(char::is_control)
            || self.updated_at_unix_ms == 0
            || self.active_binding.as_ref().is_some_and(|binding| {
                !valid_runtime_id(&binding.workspace_id, 128)
                    || binding.instance_id == 0
                    || binding.generation == 0
            })
            || (self.blocked_count == 0) != self.last_blocked_at_ms.is_none()
        {
            return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessRuntimeInventoryPageV1 {
    pub nodes: Vec<HarnessRuntimeNodeInventoryV1>,
    pub next_cursor: Option<String>,
}

impl HarnessRuntimeInventoryPageV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if self.nodes.len() > usize::from(HARNESS_RUNTIME_INVENTORY_PAGE_LIMIT_MAX)
            || self.nodes.windows(2).any(|pair| pair[0].node_id >= pair[1].node_id)
            || self.next_cursor.as_ref().is_some_and(|cursor| match self.nodes.last() {
                Some(last) => cursor != &last.node_id,
                None => true,
            })
        {
            return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
        }
        for node in &self.nodes { node.validate()?; }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessNativeSessionCatalogScopeV1 { Workspace, Unregistered }

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessNativeSessionCatalogWindowV1 { Recent, Older }

#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNativeSessionRouteV1 {
    pub node_id: String,
    pub incarnation_id: String,
    pub scope: HarnessNativeSessionCatalogScopeV1,
    pub workspace_id: Option<String>,
    pub provider: String,
}

impl HarnessNativeSessionRouteV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        let scope_valid = matches!(
            (self.scope, self.workspace_id.as_ref()),
            (HarnessNativeSessionCatalogScopeV1::Workspace, Some(_))
                | (HarnessNativeSessionCatalogScopeV1::Unregistered, None)
        );
        if !scope_valid
            || !valid_runtime_id(&self.node_id, 128)
            || self.incarnation_id.len() != 32
            || !self.incarnation_id.bytes().all(is_lower_hex)
            || !valid_runtime_id(&self.provider, 128)
            || self.workspace_id.as_deref().is_some_and(|value| {
                !valid_runtime_id(value, 128)
            })
        {
            return Err(HarnessOperatorApiError::InvalidNativeHistory);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNativeSessionSelectionV1 {
    pub route: HarnessNativeSessionRouteV1,
    pub catalog_revision: u64,
    pub recent_cutoff_unix_ms: u64,
    pub selection_id: String,
}

impl HarnessNativeSessionSelectionV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.route.validate()?;
        if self.catalog_revision == 0 || !valid_native_selection_id(&self.selection_id) {
            return Err(HarnessOperatorApiError::InvalidNativeHistory);
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessNativeSessionExternalGroupKindV1 { Project, Global }

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNativeSessionExternalGroupV1 {
    pub group_id: String,
    pub kind: HarnessNativeSessionExternalGroupKindV1,
    pub display_name: String,
}

impl HarnessNativeSessionExternalGroupV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if !valid_native_group_id(&self.group_id)
            || !valid_native_single_line(&self.display_name, 256, true)
            || matches!(self.display_name.as_str(), "." | "..")
            || self.display_name.contains('/')
            || self.display_name.contains('\\')
            || self.display_name.contains(':')
        {
            return Err(HarnessOperatorApiError::InvalidNativeHistory);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNativeSessionCatalogEntryV1 {
    pub selection_id: String,
    pub title: Option<String>,
    pub modified_at_unix_ms: Option<u64>,
    pub model: Option<String>,
    pub message_count: u64,
    pub completed_turn_count: Option<u64>,
    pub external_group: Option<HarnessNativeSessionExternalGroupV1>,
    pub record_id: Option<String>,
}

impl HarnessNativeSessionCatalogEntryV1 {
    fn validate_for_route(
        &self,
        route: &HarnessNativeSessionRouteV1,
    ) -> Result<(), HarnessOperatorApiError> {
        if !valid_native_selection_id(&self.selection_id)
            || self.title.as_ref().is_some_and(|value| {
                !valid_native_single_line(value, 512, false)
            })
            || self.model.as_ref().is_some_and(|value| {
                !valid_native_single_line(value, 512, true)
            })
            || self.record_id.as_deref().is_some_and(|value| {
                !valid_runtime_id(value, 128)
            })
        {
            return Err(HarnessOperatorApiError::InvalidNativeHistory);
        }
        if let Some(group) = &self.external_group { group.validate()?; }
        let route_fields_valid = match route.scope {
            HarnessNativeSessionCatalogScopeV1::Workspace => self.external_group.is_none(),
            HarnessNativeSessionCatalogScopeV1::Unregistered => {
                self.external_group.is_some() && self.record_id.is_none()
            }
        };
        if !route_fields_valid {
            return Err(HarnessOperatorApiError::InvalidNativeHistory);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNativeSessionCatalogSummaryV1 {
    pub catalog_revision: u64,
    pub recent_cutoff_unix_ms: u64,
    pub recent_total_count: u32,
    pub older_total_count: u32,
    pub recent_next_after_selection_id: Option<String>,
    pub recent_has_more: bool,
}

impl HarnessNativeSessionCatalogSummaryV1 {
    fn validate(&self, entry_count: usize) -> Result<(), HarnessOperatorApiError> {
        if self.catalog_revision == 0
            || self.recent_next_after_selection_id.as_deref().is_some_and(|value| {
                !valid_native_selection_id(value)
            })
            || self.recent_has_more != self.recent_next_after_selection_id.is_some()
            || usize::try_from(self.recent_total_count).map_or(true, |count| count < entry_count)
            || self.recent_has_more != (usize::try_from(self.recent_total_count)
                .unwrap_or(usize::MAX) > entry_count)
        {
            return Err(HarnessOperatorApiError::InvalidNativeHistory);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNativeSessionsCatalogedV1 {
    pub route: HarnessNativeSessionRouteV1,
    pub entries: Vec<HarnessNativeSessionCatalogEntryV1>,
    pub summary: Option<HarnessNativeSessionCatalogSummaryV1>,
}

impl HarnessNativeSessionsCatalogedV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.route.validate()?;
        validate_native_entries(&self.route, &self.entries)?;
        if let Some(summary) = &self.summary { summary.validate(self.entries.len())?; }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNativeSessionCatalogPageV1 {
    pub window: HarnessNativeSessionCatalogWindowV1,
    pub revision: u64,
    pub entries: Vec<HarnessNativeSessionCatalogEntryV1>,
    pub next_after_selection_id: Option<String>,
    pub remaining_count: u32,
    pub has_more: bool,
}

impl HarnessNativeSessionCatalogPageV1 {
    fn validate_for_route(
        &self,
        route: &HarnessNativeSessionRouteV1,
    ) -> Result<(), HarnessOperatorApiError> {
        validate_native_entries(route, &self.entries)?;
        if self.revision == 0
            || self.next_after_selection_id.as_deref().is_some_and(|value| {
                !valid_native_selection_id(value)
            })
            || self.has_more != self.next_after_selection_id.is_some()
            || self.has_more != (self.remaining_count > 0)
        {
            return Err(HarnessOperatorApiError::InvalidNativeHistory);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNativeSessionsPagedV1 {
    pub route: HarnessNativeSessionRouteV1,
    pub page: HarnessNativeSessionCatalogPageV1,
}

impl HarnessNativeSessionsPagedV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.route.validate()?;
        self.page.validate_for_route(&self.route)
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessNativeSessionPreviewRoleV1 { User, Assistant }

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNativeSessionPreviewMessageV1 {
    pub role: HarnessNativeSessionPreviewRoleV1,
    pub text: String,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNativeSessionPreviewV1 {
    pub title: Option<String>,
    pub modified_at_unix_ms: Option<u64>,
    pub model: Option<String>,
    pub message_count: u64,
    pub message_count_exact: bool,
    pub completed_turn_count: Option<u64>,
    pub total_tokens: Option<u64>,
    pub truncated: bool,
    pub messages: Vec<HarnessNativeSessionPreviewMessageV1>,
}

impl HarnessNativeSessionPreviewV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if self.title.as_ref().is_some_and(|value| {
            !valid_native_single_line(value, 512, false)
        })
            || self.model.as_ref().is_some_and(|value| {
                !valid_native_single_line(value, 512, true)
            })
            || self.messages.len()
                > usize::from(HARNESS_NATIVE_SESSION_PREVIEW_MESSAGE_LIMIT_MAX)
            || self.messages.iter().any(|message| {
                message.text.len() > HARNESS_NATIVE_SESSION_PREVIEW_TEXT_MAX_BYTES
                    || message.text.chars().any(|character| {
                        character.is_control()
                            && !matches!(character, '\n' | '\r' | '\t')
                    })
            })
        {
            return Err(HarnessOperatorApiError::InvalidNativeHistory);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNativeSessionPreviewedV1 {
    pub selection: HarnessNativeSessionSelectionV1,
    pub preview: HarnessNativeSessionPreviewV1,
}

impl HarnessNativeSessionPreviewedV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.selection.validate()?;
        self.preview.validate()
    }
}

/// `PreviewSessionRecord`'s reply: reuses `HarnessNativeSessionPreviewV1`
/// verbatim rather than defining a session-record-specific preview shape --
/// the node's own `SessionRecordPreview` and `NativeSessionPreview` are the
/// same type (`pub type NativeSessionPreview = SessionRecordPreview;` in
/// `gate4agent-node-protocol`), so the wire mirrors that identity instead of
/// inventing a duplicate.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessSessionRecordPreviewedV1 {
    pub record_id: String,
    pub preview: HarnessNativeSessionPreviewV1,
}

impl HarnessSessionRecordPreviewedV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        if !valid_runtime_id(&self.record_id, 128) {
            return Err(HarnessOperatorApiError::InvalidRuntimeInventory);
        }
        self.preview.validate()
    }
}

/// `ResumeSessionRecord`'s reply: unlike `ResumeSession` (ack-only, no
/// address -- see that request variant's doc comment), the node returns the
/// freshly spawned session's address synchronously alongside the updated
/// record, and this wire mirrors that exactly.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessSessionRecordResumedV1 {
    pub record: HarnessRuntimeManagedSessionV1,
    pub session: HarnessRuntimeSessionAddressV1,
}

impl HarnessSessionRecordResumedV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.record.validate()?;
        self.session.validate()
    }
}

/// `IndexNativeSession`'s reply: echoes the committed selection alongside
/// the resulting managed-session record, the same pairing
/// `HarnessNativeSessionPreviewedV1` uses for its own selection/payload
/// echo.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessNativeSessionIndexedV1 {
    pub selection: HarnessNativeSessionSelectionV1,
    pub record: HarnessRuntimeManagedSessionV1,
}

impl HarnessNativeSessionIndexedV1 {
    fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.selection.validate()?;
        self.record.validate()
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessOperatorMutationOutcomeV1 {
    Applied,
    Replayed,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessOperatorHostErrorV1 {
    InvalidRequest,
    Unauthorized,
    NotFound,
    Conflict,
    TooLarge,
    Deadline,
    Busy,
    Unavailable,
    // Distinct from `Deadline`: a read timing out has no side effect, so
    // "safe to retry" is implicit. A `SpawnSession` whose C2/Node round trip
    // is lost after being accepted carries no such guarantee -- the session
    // may or may not exist. This case exists to tell the operator "check the
    // runtime inventory before retrying", not "retry freely".
    OutcomeUnknown,
    // Distinct from the generic `Unavailable`/`Internal` buckets a bare
    // `NodeFailureCode::BackendOperationFailed` would otherwise collapse
    // into: the node rejected this `SpawnSession` specifically because the
    // requested provider profile does not declare the requested transport.
    // Names its own inputs -- mirrors the kernel's own
    // `KernelCommandError::UnsupportedTransport { agent_id, transport }`,
    // which this variant exists to carry through to the operator instead of
    // collapsing into a bare `Unavailable`/`Deadline` that names neither
    // (see the `spawn_session_with_deadline` commit-wait loop in
    // `hatchery-node`'s `server.rs`, which used to lose exactly this
    // reason to a blind commit-deadline poll). This is why the enum gave up
    // `Copy` -- every other variant is a bare tag, but the whole point here
    // is not making the operator already know what it asked for; it is
    // proving the host actually looked at the same request the operator
    // sent.
    UnsupportedTransport {
        agent: String,
        transport: HarnessRuntimeTransportV1,
    },
    // Distinct from the generic `Unavailable` bucket a bare `NodeFailureCode
    // ::UnsupportedCapability` used to collapse into: the node did not say
    // "busy" or "disconnected", it said this exact capability does not
    // exist for the addressed session/provider. `Unavailable` reads as
    // transient to a caller ("try again shortly"); this operation can never
    // succeed, so folding it into `Unavailable` invited a retry that was
    // guaranteed to fail the same way forever. Every mapper on this wire
    // that used to fold `NodeFailureCode::UnsupportedCapability` into
    // `Unavailable` now returns this instead.
    UnsupportedCapability,
    Internal,
    // Added alongside `hatchery-harness-light` (the P2.2 light-harness
    // extraction), riding the V11 era: distinct from `NotFound` (a request
    // scoped to an id that provably does not and never will exist under the
    // current backend, e.g. any task/run id against a backend with no task
    // kernel) and from `Internal` (an unexpected failure). `Unsupported`
    // means "this verb is recognized and well-formed, but this particular
    // operator host does not implement it" -- a light-harness host rejecting
    // the task-kernel mutation family (`SubmitIntent` and its authorized
    // siblings), the node-workspace/native-history/session-record/resource-
    // mutation families, `TerminalRead`, and `SubscribeEvents` in its first
    // slice, per the app-harness protocol contract's fail-closed principle
    // (a verb a host cannot serve gets a typed rejection, never silent
    // misbehavior or a misleading `NotFound`). Purely additive: an existing
    // host (the full harness) never returns it, and every wire version this
    // enum has ever shipped under already tolerates an unrecognized error
    // variant the same way `#[serde(rename_all = "kebab-case")]` does for
    // any other closed enum here -- see this module's round-trip tests. No
    // skew risk in practice: `hatchery-harness-light` pairs this crate
    // in-process (same build, same binary), never across a version boundary.
    Unsupported,
    // The host decoded the envelope but its declared `build_stamp` did not
    // match this side's own `BUILD_STAMP` (see `HarnessOperatorApiError::
    // BuildStampMismatch`, which this carries verbatim onto the wire so the
    // caller sees both stamps instead of the generic `InvalidRequest` every
    // other malformed-envelope shape collapses to). Reaching this variant
    // itself proves a build skew between the two loopback sides -- this
    // protocol has exactly one accepted build stamp, so a well-formed peer
    // never triggers it against a matching one.
    BuildStampMismatch { expected: String, received: String },
    // -- Everything below names its own inputs instead of collapsing into a
    // bare `Conflict`. `map_operator_service_error` (`gate4agent-harness-
    // service`'s `runtime.rs`) used to have a catch-all `_ => Conflict` arm
    // covering every one of these -- the reason a stack whose derived
    // launch-plan catalogue exceeded its page size had every `spec save`
    // refused with nothing but the word "Conflict": `InvalidTaskLaunchSelection`
    // was one of the swallowed variants. Every `HarnessServiceError` variant
    // that arm used to swallow now has its own arm there and its own named
    // variant here; the catch-all is gone. Fields whose native type lives in
    // a crate this one does not depend on (`gate4agent-node-protocol`,
    // `hatchery-harness-service`, or the foreign `HarnessEngineError` this
    // crate must not open) ride the wire as `String` -- the same treatment
    // `UnsupportedTransport::agent` above already gives an unreachable id.
    //
    // The one exception is `HarnessServiceError::Engine`, whose OWN inner
    // variants live in `hatchery-harness-engine` -- a crate under active
    // edit by another worker this fix must not open. Every non-`NotFound`
    // `HarnessEngineError` still collapses into `EngineRefused { detail }`
    // (its Debug rendering), which is strictly more than the bare `Conflict`
    // it used to get, without requiring that file's variant list.
    EngineRefused { detail: String },
    /// The one variant named explicitly in this fix's own brief: a reviewed
    /// task-launch selection failed one of `validate_current_launch_options`/
    /// `validate_current_issued_launch_options`'s checks. `why` is always one
    /// of a small set of fixed literals (see `HarnessServiceError::
    /// InvalidTaskLaunchSelection`'s doc comment); `plan_id` is `Some`
    /// whenever the failing check concerns a specific plan.
    InvalidLaunchSelection {
        task_id: HarnessTaskId,
        plan_id: Option<HarnessSelectorV1>,
        why: String,
    },
    UnsupportedCheckpointVersion { version: u16 },
    InvalidDispatchContext { reason: String },
    MutationDigestMismatch,
    DispatchFingerprintUnavailable,
    NonAtomicRunOperation,
    AcceptedSpawnProofRequired,
    InvalidAcceptedSpawnProof { reason: String },
    DeliveryAuthorityWindowClosed,
    DeliveryCompilationInvalid,
    InvalidStagedDeliveryProof { reason: String },
    AtomicDeliveryCommitRequired,
    ContinuationAuthorityWindowClosed,
    InvalidContinuationProof { reason: String },
    AtomicContinuationBindRequired,
    InvalidHarnessMcpReservation { reason: String },
    HarnessMcpGrantActorRefused {
        actor_kind: String,
        parent_run_id: Option<HarnessRunId>,
        grant_actor_run_id: HarnessRunId,
    },
    HarnessMcpGrantOperationLinkRefused {
        grant_id: SessionGrantId,
        operation_id: HarnessOperationId,
        existing_grant_id: SessionGrantId,
    },
    HarnessMcpGrantRevisionRefused {
        grant_id: SessionGrantId,
        durable_revision: HarnessRevision,
        presented_revision: HarnessRevision,
    },
    HarnessMcpGrantLinkRefused {
        grant_id: SessionGrantId,
        grant_actor_run_id: HarnessRunId,
        dispatch_actor_run_id: HarnessRunId,
    },
    HarnessMcpGrantTargetRefused {
        grant_id: SessionGrantId,
        presented_target: HarnessGrantTargetV1,
        allowed_targets: Vec<HarnessGrantTargetV1>,
    },
    HarnessMcpReplayMismatch,
    HarnessMcpProofMismatch,
    HarnessMcpArmProofReservationFieldRefused {
        field: String,
        durable: String,
        proof: String,
    },
    HarnessMcpArmProofRouteRefused {
        field: String,
        durable: String,
        route: String,
    },
    HarnessMcpArmProofBindingRefused {
        field: String,
        expected: String,
        actual: String,
    },
    HarnessMcpArmDurableLookupMissing {
        missing: String,
        operation_id: Option<HarnessOperationId>,
        reservation_id: Option<String>,
    },
    HarnessMcpArmReservationNotReadyRefused {
        reservation_id: String,
        state: String,
        armed_at_unix_ms: u64,
        updated_at_unix_ms: u64,
        expires_at_unix_ms: u64,
    },
    HarnessMcpArmRouteInvalid {
        operation_id: HarnessOperationId,
        field: String,
        value: String,
    },
    HarnessMcpLaunchPolicyRefused {
        plan_policy: String,
        reservation_present: bool,
    },
    HarnessMcpLaunchReservationNotArmedRefused {
        reservation_id: String,
        state: String,
    },
    HarnessMcpLaunchOperationRefused {
        reservation_operation_id: HarnessOperationId,
        dispatch_operation_id: HarnessOperationId,
    },
    HarnessMcpLaunchGrantRefused {
        plan_grant_id: SessionGrantId,
        plan_grant_revision: HarnessRevision,
        reservation_grant_id: SessionGrantId,
        reservation_grant_revision: HarnessRevision,
    },
    HarnessMcpSpecializedTransitionRequired,
    OperatorRequestConflict { operation_id: HarnessOperationId },
    InvalidOperatorTaskTransition {
        from: HarnessTaskStateV1,
        to: HarnessTaskStateV1,
    },
    TaskHasActiveRun,
    ExecutionSpecRevisionMismatch {
        expected: Option<HarnessRevision>,
        actual: Option<HarnessRevision>,
    },
    ExecutionSpecLaunchMismatch,
    IssuedExecutionCasMismatch {
        expected: HarnessExpectedExecutionSpecRevisionV1,
        spec: Option<HarnessRevision>,
        issuance: Option<HarnessRevision>,
    },
    TaskNotReady,
    // `TaskNotReady` answered three different questions with one word: the
    // task is not in `Ready`, a dependency of it is unfinished, or it already
    // has a live run. Only the first is something the operator can see for
    // itself with `task get`; the other two are facts the host holds and the
    // operator does not. These two carry them, for the same reason
    // `UnsupportedTransport` above carries its own inputs -- the point is not
    // making the operator already know what blocked it, it is proving the
    // host looked. `dependency_ids` lists exactly the dependencies that are
    // not `Done` yet, so an operator sees what to finish first instead of a
    // shrug.
    TaskDependenciesNotDone {
        task_id: String,
        dependency_ids: Vec<String>,
    },
    // Unreachable through today's paths -- every route back into `Ready`
    // (`operator_move_task`, `operator_retry_task`) already refuses while a
    // non-terminal run exists, and `start_task_v2` flips `Ready`->`Running`
    // atomically with run creation. Named anyway rather than folded back into
    // `TaskNotReady`, so that if a future path into `Ready` ever reopens it,
    // the refusal arrives already saying which of the three things happened.
    TaskStartBlockedByRun { task_id: String },
    SchedulerResourceExhausted,
    SchedulerInvalidGraph { reason: String },
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessReadEnvelopeV1 {
    pub build_stamp: String,
    pub credential: HarnessReadCredential,
    pub request: HarnessReadRequestV1,
}

impl HarnessReadEnvelopeV1 {
    /// Builds an envelope carrying this binary's own [`BUILD_STAMP`] -- the
    /// one place every caller in this tree (this crate's own tests,
    /// `hatchery-harness-client`) should build one from, rather than
    /// hand-filling `build_stamp` at each site.
    pub fn new(credential: HarnessReadCredential, request: HarnessReadRequestV1) -> Self {
        Self { build_stamp: BUILD_STAMP.to_string(), credential, request }
    }

    pub fn validate(&self) -> Result<(), HarnessReadApiError> {
        if self.build_stamp != BUILD_STAMP {
            return Err(HarnessReadApiError::BuildStampMismatch {
                expected: BUILD_STAMP.to_string(),
                received: self.build_stamp.clone(),
            });
        }
        validate_credential(self.credential.expose())?;
        self.request.validate()
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessReadRequestV1 {
    ContextGet,
    MonitorGet { run_id: Option<HarnessRunId> },
    TimelineRead {
        run_id: Option<HarnessRunId>,
        after_sequence: Option<u64>,
        limit: u16,
    },
    TasksList {
        after_task_id: Option<HarnessTaskId>,
        state: Option<HarnessTaskStateV1>,
        /// "What did this task spawn": restricts the page to tasks whose
        /// own `parent_task_id` equals this one -- direct children only,
        /// never deeper descendants. Additive over the original bare
        /// `{ after_task_id, state, limit }` shape, so `#[serde(default)]`
        /// keeps an older caller decoding. Composes with `state` (both
        /// filters apply together) and pages the same way via
        /// `after_task_id`.
        #[serde(default)]
        parent_task_id: Option<HarnessTaskId>,
        limit: u16,
    },
    TaskGet { task_id: HarnessTaskId },
    RunsList {
        task_id: Option<HarnessTaskId>,
        after_run_id: Option<HarnessRunId>,
        lifecycle: Option<HarnessRunLifecycleV1>,
        /// "What did this run spawn": restricts the page to runs whose own
        /// `parent_run_id` equals this one -- direct children only. Additive
        /// over the original shape (`#[serde(default)]`), composes with
        /// `task_id`/`lifecycle`, pages the same way via `after_run_id`.
        #[serde(default)]
        parent_run_id: Option<HarnessRunId>,
        limit: u16,
    },
    RunGet { run_id: HarnessRunId },
    OperationGet { operation_id: HarnessOperationId },
    /// D5, Slice D: an agent creates a task under its own subtree --
    /// `parent_task_id: None` means "under my own task"; `Some` must name
    /// the caller's own task or a strict descendant of it (checked
    /// service-side, `HarnessEngine::task_is_strict_descendant`), never a
    /// task system-wide the way the operator's own `HarnessCreateTaskRequestV1`
    /// can. Gated by `grant.task_permissions.create`. Every refusal --
    /// unreachable parent, a terminal parent, or an invalid title/body/
    /// dependencies -- travels back as `HarnessTaskCreateResultV1`'s own
    /// named variant, never a host error. This bound is the coarse
    /// wire-level ceiling only; the authoritative title/body rule is the
    /// engine's own `HarnessTaskV1::validate()`, reached when the mutation
    /// applies.
    TaskCreate {
        title: String,
        body: String,
        parent_task_id: Option<HarnessTaskId>,
    },
    /// D5, Slice D: restricted service-side to a STRICT descendant of the
    /// caller's own task -- never the task that governs the caller's own
    /// run (`HarnessTaskMoveResultV1::TaskIsOwn`). Gated by
    /// `grant.task_permissions.mutate`. `to` names the operator's own task
    /// graph (`validate_operator_move` in `hatchery-harness-service`); an
    /// illegal transition or a stale `expected_revision` is a named refusal,
    /// never a host error.
    TaskMove {
        task_id: HarnessTaskId,
        expected_revision: HarnessRevision,
        to: HarnessTaskStateV1,
    },
    /// S10: a session reports its OWN run finished -- resolved from the
    /// caller's own grant binding (`actor_run_id`), the same field every
    /// other agent verb resolves its identity from; there is no run-id
    /// argument, so a session can never finish another session's run.
    /// `done` applies `HarnessLifecycleProjectionV1::CompletedReview`
    /// (`hatchery-harness-service`), landing the task in `Review` --
    /// never `Done`, the review gate a session cannot skip. `failed`
    /// applies `HarnessLifecycleProjectionV1::Failed` and records a
    /// retryable `HarnessFailureV1`. Never gated by any grant permission
    /// (unconditional in `allowed_tool_ids`, the same way `g4a_context_get`
    /// is): every session needs the ability to report its own work
    /// finished regardless of what else its grant allows, or nothing ever
    /// closes -- the exact structural gap this verb exists to close.
    /// `summary` is accepted on the wire for backward compatibility but has
    /// no effect: it used to be posted as mail to the task's own forum,
    /// removed 2026-09-17 when the mailbox moved to its own service (see
    /// `hatchery-harness-service::read::execute_exact_binding_read`'s
    /// `RunFinish` handler). Not idempotent: a second call against an
    /// already-terminal run is refused by name as
    /// `HarnessRunFinishResultV1::AlreadyFinished`, never silently accepted.
    RunFinish {
        outcome: HarnessRunFinishOutcomeV1,
        #[serde(default)]
        summary: Option<String>,
    },
}

impl HarnessReadRequestV1 {
    pub fn validate(&self) -> Result<(), HarnessReadApiError> {
        match self {
            Self::RunFinish { summary, .. } => {
                if let Some(summary) = summary {
                    if summary.len() > HARNESS_BODY_MAX_BYTES {
                        return Err(HarnessReadApiError::InvalidText("run finish summary"));
                    }
                }
                Ok(())
            }
            Self::TaskCreate { title, body, parent_task_id } => {
                if title.is_empty()
                    || title.len() > HARNESS_TITLE_MAX_BYTES
                    || body.len() > HARNESS_BODY_MAX_BYTES
                {
                    return Err(HarnessReadApiError::InvalidTaskCreate);
                }
                if let Some(parent_task_id) = parent_task_id {
                    parent_task_id.validate().map_err(HarnessReadApiError::Protocol)?;
                }
                Ok(())
            }
            Self::TaskMove { task_id, expected_revision, .. } => {
                task_id.validate().map_err(HarnessReadApiError::Protocol)?;
                expected_revision.validate().map_err(HarnessReadApiError::Protocol)
            }
            Self::TimelineRead { run_id, after_sequence, limit } => {
                if let Some(run_id) = run_id {
                    run_id.validate().map_err(HarnessReadApiError::Protocol)?;
                }
                if after_sequence == &Some(0) {
                    return Err(HarnessReadApiError::InvalidCursor);
                }
                validate_limit(*limit, HARNESS_TIMELINE_PAGE_LIMIT_MAX)
            }
            Self::TasksList { after_task_id, parent_task_id, limit, .. } => {
                if let Some(task_id) = after_task_id {
                    task_id.validate().map_err(HarnessReadApiError::Protocol)?;
                }
                if let Some(task_id) = parent_task_id {
                    task_id.validate().map_err(HarnessReadApiError::Protocol)?;
                }
                validate_limit(*limit, HARNESS_ENTITY_PAGE_LIMIT_MAX)
            }
            Self::RunsList { task_id, after_run_id, parent_run_id, limit, .. } => {
                if let Some(task_id) = task_id {
                    task_id.validate().map_err(HarnessReadApiError::Protocol)?;
                }
                if let Some(run_id) = after_run_id {
                    run_id.validate().map_err(HarnessReadApiError::Protocol)?;
                }
                if let Some(run_id) = parent_run_id {
                    run_id.validate().map_err(HarnessReadApiError::Protocol)?;
                }
                validate_limit(*limit, HARNESS_ENTITY_PAGE_LIMIT_MAX)
            }
            Self::TaskGet { task_id } => task_id.validate().map_err(HarnessReadApiError::Protocol),
            Self::RunGet { run_id } => run_id.validate().map_err(HarnessReadApiError::Protocol),
            Self::OperationGet { operation_id } => {
                operation_id.validate().map_err(HarnessReadApiError::Protocol)
            }
            Self::MonitorGet { run_id } => {
                if let Some(run_id) = run_id {
                    run_id.validate().map_err(HarnessReadApiError::Protocol)?;
                }
                Ok(())
            }
            Self::ContextGet => Ok(()),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "status", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessReadReplyV1 {
    Ok { response: HarnessReadResponseV1 },
    Error { error: HarnessReadHostErrorV1 },
}

impl HarnessReadReplyV1 {
    pub fn validate(&self) -> Result<(), HarnessReadApiError> {
        match self {
            Self::Ok { response } => response.validate(),
            Self::Error { .. } => Ok(()),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", content = "value", rename_all = "kebab-case", deny_unknown_fields)]
pub enum HarnessReadResponseV1 {
    Context(SessionContextV1),
    Monitor(SessionMonitorV1),
    Timeline(TimelinePageV1),
    Tasks(TaskPageV1),
    Task(RedactedTaskV1),
    Runs(RunPageV1),
    Run(RedactedRunV1),
    Operation(RedactedOperationV1),
    TaskCreate(HarnessTaskCreateResultV1),
    TaskMove(HarnessTaskMoveResultV1),
    RunFinish(HarnessRunFinishResultV1),
}

impl HarnessReadResponseV1 {
    pub fn validate(&self) -> Result<(), HarnessReadApiError> {
        match self {
            Self::Context(value) => value.validate(),
            Self::Monitor(value) => value.validate(),
            Self::Timeline(value) => value.validate(),
            Self::Tasks(value) => value.validate(),
            Self::Task(value) => value.validate(),
            Self::Runs(value) => value.validate(),
            Self::Run(value) => value.validate(),
            Self::Operation(value) => value.validate(),
            Self::TaskCreate(value) => value.validate().map_err(HarnessReadApiError::Protocol),
            Self::TaskMove(value) => value.validate().map_err(HarnessReadApiError::Protocol),
            Self::RunFinish(value) => value.validate().map_err(HarnessReadApiError::Protocol),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum HarnessReadHostErrorV1 {
    InvalidRequest,
    Unauthorized,
    NotFoundOrDenied,
    TooLarge,
    Deadline,
    Internal,
    // The host decoded the envelope but its declared `build_stamp` did not
    // match this side's own `BUILD_STAMP` (see `HarnessReadApiError::
    // BuildStampMismatch`, which this carries verbatim onto the wire so the
    // caller sees both stamps instead of the generic `InvalidRequest` every
    // other malformed-envelope shape collapses to). Reaching this variant
    // itself proves a build skew between the two loopback sides -- this
    // protocol has exactly one accepted build stamp, so a well-formed peer
    // never triggers it against a matching one.
    BuildStampMismatch { expected: String, received: String },
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct SessionContextV1 {
    pub grant_id: SessionGrantId,
    pub grant_revision: HarnessRevision,
    pub actor_run: CallerRunV1,
    /// The calling run's own task, redacted the same way as `TaskGet` --
    /// present only when the grant's task scope makes it visible (mirrors
    /// `actor_run.task_id`).
    pub task: Option<RedactedTaskV1>,
    /// Every OTHER run recorded against `task` ("what previous sessions did
    /// on it"), redacted the same way as `RunGet`. Gated solely by `task`
    /// being visible -- independent of the grant's `runs` read scope, which
    /// governs the general run-browsing tools instead. Always empty when
    /// `task` is `None`; never includes runs of another task.
    pub sibling_runs: Vec<RedactedRunV1>,
    pub read_permissions: HarnessReadPermissionsV1,
    pub monitoring_visibility: HarnessMonitoringVisibilityV1,
    /// Observation, not a budget: the number of tasks parented directly
    /// under the calling run's own task right now
    /// (`HarnessEngine::task_child_count`), `0` if it has no children.
    /// Unconditional -- computed from `actor_run`'s own task the same way
    /// the retired `maximum_child_count` wire field it replaces was
    /// (independent of whether `task` above is redacted to `None`). That
    /// retired field stated a cap that was validated for range but compared
    /// against no actual child count anywhere in the service -- a session
    /// could read a limit here and still create children past it. The
    /// owner's ruling: do not cap a session's children, detect them. Nothing
    /// in this wire enforces an upper bound on this count.
    pub child_task_count: u64,
    /// Observation, not a budget: the depth of the deepest chain of tasks
    /// parented (transitively) under the calling run's own task right now
    /// (`HarnessEngine::task_subtree_depth`) -- `0` if it has no children,
    /// `1` if it has children but no grandchildren, and so on. Unconditional
    /// in the same sense as `child_task_count` above. Replaces the retired
    /// `maximum_child_depth` wire field for the same reason `child_task_
    /// count` replaces `maximum_child_count` (see that field's doc comment).
    /// Nothing in this wire enforces an upper bound on this depth.
    pub child_task_subtree_depth: u64,
    /// D5, Slice D: mirrors `grant.task_permissions.create` -- gates
    /// `g4a_task_create` in `allowed_tool_ids`.
    pub task_create: bool,
    /// D5, Slice D: mirrors `grant.task_permissions.mutate` -- gates
    /// `g4a_task_move` in `allowed_tool_ids`.
    pub task_mutate: bool,
    pub allowed_tool_ids: Vec<String>,
    pub history_message_count: Option<u64>,
    pub completed_turn_count: Option<u64>,
    pub total_tokens: Option<u64>,
}

impl SessionContextV1 {
    pub fn validate(&self) -> Result<(), HarnessReadApiError> {
        self.grant_id.validate().map_err(HarnessReadApiError::Protocol)?;
        self.grant_revision.validate().map_err(HarnessReadApiError::Protocol)?;
        self.actor_run.validate()?;
        match &self.task {
            Some(task) => {
                task.validate()?;
                if self.actor_run.task_id.as_ref() != Some(&task.task_id) {
                    return Err(HarnessReadApiError::InvalidContextTask);
                }
            }
            None if !self.sibling_runs.is_empty() => {
                return Err(HarnessReadApiError::InvalidContextTask);
            }
            None => {}
        }
        if self.sibling_runs.len() > HARNESS_LINKS_MAX
            || self.sibling_runs.windows(2).any(|pair| pair[0].run_id >= pair[1].run_id)
            || self.sibling_runs.iter().any(|run| {
                run.run_id == self.actor_run.run_id
                    || run.task_id.as_ref() != self.actor_run.task_id.as_ref()
            })
        {
            return Err(HarnessReadApiError::InvalidContextTask);
        }
        for run in &self.sibling_runs {
            run.validate()?;
        }
        self.read_permissions.validate().map_err(HarnessReadApiError::Protocol)?;
        if self.allowed_tool_ids.len() > HARNESS_READ_TOOL_IDS.len() + HARNESS_WRITE_TOOL_IDS.len()
            || self.allowed_tool_ids.windows(2).any(|pair| pair[0] >= pair[1])
            || self.allowed_tool_ids.iter().any(|id| {
                !HARNESS_READ_TOOL_IDS.contains(&id.as_str())
                    && !HARNESS_WRITE_TOOL_IDS.contains(&id.as_str())
            })
            || self.allowed_tool_ids != expected_allowed_tool_ids(self)
        {
            return Err(HarnessReadApiError::InvalidAllowedTools);
        }
        Ok(())
    }
}

fn expected_allowed_tool_ids(context: &SessionContextV1) -> Vec<String> {
    // S10: `g4a_run_finish` is unconditional, exactly like `g4a_context_get`
    // -- see `HARNESS_WRITE_TOOL_IDS`'s own doc comment for why it carries
    // no grant-permission gate at all.
    let mut tools = vec!["g4a_context_get", "g4a_run_finish"];
    if context.monitoring_visibility != HarnessMonitoringVisibilityV1::None {
        tools.push("g4a_monitor_get");
    }
    if context.monitoring_visibility == HarnessMonitoringVisibilityV1::Timeline {
        tools.push("g4a_timeline_read");
    }
    if context.read_permissions.tasks != HarnessEntityReadScopeV1::None {
        tools.extend(["g4a_tasks_get", "g4a_tasks_list"]);
    }
    if context.read_permissions.runs != HarnessEntityReadScopeV1::None {
        tools.extend(["g4a_runs_get", "g4a_runs_list"]);
    }
    if context.read_permissions.operations != HarnessEntityReadScopeV1::None {
        tools.push("g4a_operation_get");
    }
    if context.task_create {
        tools.push("g4a_task_create");
    }
    if context.task_mutate {
        tools.push("g4a_task_move");
    }
    tools.sort_unstable();
    tools.into_iter().map(str::to_owned).collect()
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct CallerRunV1 {
    pub run_id: HarnessRunId,
    pub task_id: Option<HarnessTaskId>,
    pub parent_run_id: Option<HarnessRunId>,
    pub lifecycle: HarnessRunLifecycleV1,
    pub references_redacted: bool,
}

impl CallerRunV1 {
    pub fn validate(&self) -> Result<(), HarnessReadApiError> {
        self.run_id.validate().map_err(HarnessReadApiError::Protocol)?;
        if let Some(task_id) = &self.task_id {
            task_id.validate().map_err(HarnessReadApiError::Protocol)?;
        }
        if let Some(parent_run_id) = &self.parent_run_id {
            parent_run_id.validate().map_err(HarnessReadApiError::Protocol)?;
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ProjectionAvailabilityV1 {
    Unknown,
    NotObserved,
    Current,
    Partial,
    Frozen,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ProjectionFreshnessV1 {
    Unavailable,
    Live,
    Stale,
    IncompleteAfterGap,
    LastKnown,
    ReplacedIncarnation,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum FeatureObservationStateV1 {
    Unknown,
    NotSupportedByObservedSources,
    SupportedNotObserved,
    Observed,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct MonitorFeatureStatesV1 {
    pub todo: FeatureObservationStateV1,
    pub tools: FeatureObservationStateV1,
    pub subagents: FeatureObservationStateV1,
    pub interactions: FeatureObservationStateV1,
    pub owned_processes: FeatureObservationStateV1,
    pub files: FeatureObservationStateV1,
    pub usage: FeatureObservationStateV1,
    pub history: FeatureObservationStateV1,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct SessionMonitorV1 {
    pub run_id: HarnessRunId,
    pub visibility: HarnessMonitoringVisibilityV1,
    pub availability: ProjectionAvailabilityV1,
    pub freshness: ProjectionFreshnessV1,
    pub transport_incomplete: bool,
    pub features: MonitorFeatureStatesV1,
    pub todo_total: u16,
    pub todo_completed: u16,
    pub active_tools: u16,
    pub active_subagents: u16,
    pub active_interactions: u16,
    /// Count of `ObservationKindV1::ActionBlocked` observations recorded
    /// against this session so far (`SessionProjection::blocked_count`,
    /// saturated to `u16`) -- NOT a "currently pending" count like its
    /// `active_*` siblings: unlike a tool call or an interaction, a block
    /// has no resolution step to wait on (see that observation kind's own
    /// doc comment), so there is no pending/resolved split to count
    /// separately. Mirrors the fleet-wide `HarnessRuntimeManagedSessionV1::
    /// blocked_count` this same tally already backs, just for this one run.
    /// `#[serde(default)]`: additive field on a read-wire response that is
    /// never persisted, only built fresh per read.
    #[serde(default)]
    pub active_blocks: u16,
    pub active_processes: u16,
    pub input_tokens: u64,
    pub output_tokens: u64,
    pub cache_read_tokens: u64,
    pub cache_write_tokens: u64,
    pub reasoning_tokens: u64,
    pub context_window_tokens: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub history: Option<SessionMonitorHistoryV1>,
    pub detail: Option<SessionMonitorDetailV1>,
}

impl SessionMonitorV1 {
    pub fn validate(&self) -> Result<(), HarnessReadApiError> {
        self.run_id.validate().map_err(HarnessReadApiError::Protocol)?;
        if self.todo_completed > self.todo_total {
            return Err(HarnessReadApiError::InvalidMonitorCounts);
        }
        let detail_allowed = matches!(
            self.visibility,
            HarnessMonitoringVisibilityV1::Detail | HarnessMonitoringVisibilityV1::Timeline
        );
        if self.detail.is_some() && !detail_allowed {
            return Err(HarnessReadApiError::InvalidMonitorDetail);
        }
        if matches!(self.availability, ProjectionAvailabilityV1::Unknown | ProjectionAvailabilityV1::NotObserved)
            && self.detail.is_some()
        {
            return Err(HarnessReadApiError::InvalidMonitorDetail);
        }
        if self.freshness == ProjectionFreshnessV1::IncompleteAfterGap
            && !self.transport_incomplete
        {
            return Err(HarnessReadApiError::InvalidMonitorDetail);
        }
        if let Some(detail) = &self.detail {
            detail.validate(&self.features)?;
        }
        if self.features.history != FeatureObservationStateV1::Observed
            && self.history.is_some()
        {
            return Err(HarnessReadApiError::InvalidMonitorDetail);
        }
        validate_feature_counts(self)?;
        Ok(())
    }

    pub fn validate_for(&self, run_id: &HarnessRunId) -> Result<(), HarnessReadApiError> {
        self.validate()?;
        if &self.run_id != run_id {
            return Err(HarnessReadApiError::InvalidRunState);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct SessionMonitorHistoryV1 {
    pub message_count: u64,
    pub message_count_exact: bool,
    pub completed_turn_count: Option<u64>,
    pub total_tokens: Option<u64>,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct SessionMonitorDetailV1 {
    pub todo_facts: Vec<TodoFactV1>,
    pub tool_facts: Vec<ActivityFactV1>,
    pub subagent_facts: Vec<ActivityFactV1>,
    pub interaction_facts: Vec<InteractionFactV1>,
    /// One fact per `ObservationKindV1::ActionBlocked` observation recorded
    /// against this session. `hatchery-harness-service::read::monitor_detail`
    /// derives these from the session's own bounded `timeline`
    /// (`hatchery_observation_engine::SessionProjection::timeline`), not
    /// from a dedicated correlation-tracked list: a block has no
    /// request/response pairing for that engine's projection to track, only
    /// a running `blocked_count` tally (see `SessionMonitorV1::active_blocks`).
    /// Its own list rather than folded into `interaction_facts`:
    /// `interaction_facts` is built strictly off `SessionProjection::
    /// interactions`, which `ActionBlocked` never populates, so folding it
    /// in would need widening that engine-side projection, out of this
    /// field's own scope. `#[serde(default)]` reads a monitor response
    /// captured before this field existed as empty rather than failing to
    /// deserialize -- this type is never persisted, only built fresh per
    /// read and sent over the operator/grant-bound wire.
    #[serde(default)]
    pub block_facts: Vec<BlockFactV1>,
    pub process_facts: Vec<ActivityFactV1>,
    pub file_facts: Vec<FileFactV1>,
}

impl SessionMonitorDetailV1 {
    pub fn validate(&self, features: &MonitorFeatureStatesV1) -> Result<(), HarnessReadApiError> {
        validate_monitor_facts(&self.todo_facts)?;
        validate_monitor_facts(&self.tool_facts)?;
        validate_monitor_facts(&self.subagent_facts)?;
        validate_monitor_facts(&self.interaction_facts)?;
        validate_monitor_facts(&self.block_facts)?;
        validate_monitor_facts(&self.process_facts)?;
        validate_monitor_facts(&self.file_facts)?;
        for fact in &self.todo_facts { fact.validate()?; }
        for fact in &self.tool_facts { fact.validate()?; }
        for fact in &self.subagent_facts { fact.validate()?; }
        for fact in &self.interaction_facts { fact.validate()?; }
        for fact in &self.block_facts { fact.validate()?; }
        for fact in &self.process_facts { fact.validate()?; }
        for fact in &self.file_facts { fact.validate()?; }
        if self.tool_facts.iter().any(|fact| fact.class != ActivityClassV1::Tool)
            || self.subagent_facts.iter().any(|fact| fact.class != ActivityClassV1::Subagent)
            || self.process_facts.iter().any(|fact| fact.class != ActivityClassV1::OwnedProcess)
        {
            return Err(HarnessReadApiError::InvalidMonitorDetail);
        }
        Ok(())
            .and_then(|_| require_observed_or_empty(features.todo, &self.todo_facts))
            .and_then(|_| require_observed_or_empty(features.tools, &self.tool_facts))
            .and_then(|_| require_observed_or_empty(features.subagents, &self.subagent_facts))
            .and_then(|_| require_observed_or_empty(features.interactions, &self.interaction_facts))
            .and_then(|_| require_observed_or_empty(features.owned_processes, &self.process_facts))
            .and_then(|_| require_observed_or_empty(features.files, &self.file_facts))
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct TodoFactV1 {
    pub state: TodoStateV1,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub todo_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    pub evidence: ObservationEvidenceV1,
}

impl TodoFactV1 {
    fn validate(&self) -> Result<(), HarnessReadApiError> {
        validate_optional_observation_text(
            "todo id",
            self.todo_id.as_deref(),
            HARNESS_OBSERVATION_LABEL_MAX_BYTES,
        )?;
        validate_optional_observation_text(
            "todo label",
            self.label.as_deref(),
            HARNESS_OBSERVATION_TODO_TEXT_MAX_BYTES,
        )
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum TodoStateV1 { Pending, InProgress, Completed, Unknown }

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ActivityFactV1 {
    pub class: ActivityClassV1,
    pub state: ActivityStateV1,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub correlation: Option<u16>,
    pub evidence: ObservationEvidenceV1,
}

impl ActivityFactV1 {
    fn validate(&self) -> Result<(), HarnessReadApiError> {
        validate_optional_observation_text(
            "activity label",
            self.label.as_deref(),
            HARNESS_OBSERVATION_LABEL_MAX_BYTES,
        )?;
        validate_observation_correlation(self.correlation)
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ActivityClassV1 { Tool, Subagent, OwnedProcess }

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ActivityStateV1 { Started, Active, Waiting, Completed, Failed, UnknownAfterGap }

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct InteractionFactV1 {
    pub class: InteractionClassV1,
    pub state: InteractionStateV1,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub correlation: Option<u16>,
    pub evidence: ObservationEvidenceV1,
}

impl InteractionFactV1 {
    fn validate(&self) -> Result<(), HarnessReadApiError> {
        validate_optional_observation_text(
            "interaction label",
            self.label.as_deref(),
            HARNESS_OBSERVATION_LABEL_MAX_BYTES,
        )?;
        validate_observation_correlation(self.correlation)
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum InteractionClassV1 { Attention, Approval, UserInput }

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum InteractionStateV1 { Required, Responded, Dismissed, UnknownAfterGap }

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct FileFactV1 {
    pub action: FileActionV1,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub relative_path: Option<String>,
    pub evidence: ObservationEvidenceV1,
}

impl FileFactV1 {
    fn validate(&self) -> Result<(), HarnessReadApiError> {
        if let Some(path) = self.relative_path.as_deref() {
            validate_observation_relative_path(path)?;
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum FileActionV1 { Changed }

/// One `ObservationKindV1::ActionBlocked` observation surfaced on the
/// monitor wire -- see `SessionMonitorDetailV1::block_facts`'s own doc
/// comment for why this is its own list rather than folded into
/// `interaction_facts`/`process_facts`. `label` is `<authority slug>:
/// <reason>`, truncated to `HARNESS_OBSERVATION_BLOCK_LABEL_MAX_BYTES` by
/// the producer -- never the raw, up-to-1024-byte `reason` verbatim.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct BlockFactV1 {
    pub state: BlockStateV1,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub correlation: Option<u16>,
    pub evidence: ObservationEvidenceV1,
}

impl BlockFactV1 {
    fn validate(&self) -> Result<(), HarnessReadApiError> {
        validate_optional_observation_text(
            "block label",
            self.label.as_deref(),
            HARNESS_OBSERVATION_BLOCK_LABEL_MAX_BYTES,
        )?;
        validate_observation_correlation(self.correlation)
    }
}

/// A single, always-terminal variant today (paralleling `FileActionV1::
/// Changed`) -- kept as its own enum rather than a bare unit field so a
/// future, more granular block state has somewhere to land without another
/// wire shape change.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum BlockStateV1 { Blocked }

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ObservationEvidenceV1 {
    StructuredProvider,
    ManagedHook,
    WorkspaceObservation,
    NodeLifecycle,
    History,
    PtyHint,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct TimelineEntryV1 {
    pub sequence: u64,
    pub received_at_ms: u64,
    pub category: TimelineCategoryV1,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    #[serde(default)]
    pub state: TimelineStateV1,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub correlation: Option<u16>,
    pub evidence: ObservationEvidenceV1,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct TimelinePageV1 {
    pub run_id: HarnessRunId,
    pub availability: ProjectionAvailabilityV1,
    pub freshness: ProjectionFreshnessV1,
    pub transport_incomplete: bool,
    pub entries: Vec<TimelineEntryV1>,
    pub next_cursor: Option<u64>,
}

impl TimelinePageV1 {
    pub fn validate(&self) -> Result<(), HarnessReadApiError> {
        self.run_id.validate().map_err(HarnessReadApiError::Protocol)?;
        validate_bounded(&self.entries, HARNESS_TIMELINE_PAGE_LIMIT_MAX, TimelineEntryV1::validate)?;
        if self.entries.windows(2).any(|pair| pair[0].sequence >= pair[1].sequence)
            || self.next_cursor == Some(0)
        {
            return Err(HarnessReadApiError::InvalidCursor);
        }
        if self.next_cursor.is_some()
            && self.next_cursor != self.entries.last().map(|entry| entry.sequence)
        {
            return Err(HarnessReadApiError::InvalidCursor);
        }
        Ok(())
    }

    pub fn validate_for(&self, run_id: &HarnessRunId) -> Result<(), HarnessReadApiError> {
        self.validate()?;
        if &self.run_id != run_id {
            return Err(HarnessReadApiError::InvalidRunState);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct TaskPageV1 {
    pub tasks: Vec<RedactedTaskV1>,
    pub next_cursor: Option<HarnessTaskId>,
}

impl TaskPageV1 {
    pub fn validate(&self) -> Result<(), HarnessReadApiError> {
        validate_bounded(&self.tasks, HARNESS_ENTITY_PAGE_LIMIT_MAX, RedactedTaskV1::validate)?;
        if self.tasks.windows(2).any(|pair| pair[0].task_id >= pair[1].task_id)
            || self.next_cursor.as_ref() != self.tasks.last().map(|task| &task.task_id)
                && self.next_cursor.is_some()
        {
            return Err(HarnessReadApiError::InvalidCursor);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RunPageV1 {
    pub runs: Vec<RedactedRunV1>,
    pub next_cursor: Option<HarnessRunId>,
}

impl RunPageV1 {
    pub fn validate(&self) -> Result<(), HarnessReadApiError> {
        validate_bounded(&self.runs, HARNESS_ENTITY_PAGE_LIMIT_MAX, RedactedRunV1::validate)?;
        if self.runs.windows(2).any(|pair| pair[0].run_id >= pair[1].run_id)
            || self.next_cursor.as_ref() != self.runs.last().map(|run| &run.run_id)
                && self.next_cursor.is_some()
        {
            return Err(HarnessReadApiError::InvalidCursor);
        }
        Ok(())
    }
}

impl TimelineEntryV1 {
    pub fn validate(&self) -> Result<(), HarnessReadApiError> {
        if self.sequence == 0 || self.received_at_ms == 0 {
            return Err(HarnessReadApiError::InvalidTimelineEntry);
        }
        validate_optional_observation_text(
            "timeline label",
            self.label.as_deref(),
            HARNESS_OBSERVATION_PATH_MAX_BYTES,
        )?;
        validate_observation_correlation(self.correlation)?;
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum TimelineStateV1 {
    Started,
    Active,
    Waiting,
    Required,
    Updated,
    Changed,
    Completed,
    Failed,
    Dismissed,
    Interrupted,
    Stale,
    UnknownAfterGap,
    #[default]
    Unknown,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum TimelineCategoryV1 {
    Lifecycle,
    Todo,
    Tool,
    Subagent,
    Interaction,
    Process,
    File,
    Usage,
    History,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RedactedTaskV1 {
    pub task_id: HarnessTaskId,
    pub revision: HarnessRevision,
    pub title: String,
    pub body: String,
    pub creator: TaskCreatorCategoryV1,
    pub parent_task_id: Option<HarnessTaskId>,
    pub dependency_ids: Vec<HarnessTaskId>,
    pub state: HarnessTaskStateV1,
    pub run_ids: Vec<HarnessRunId>,
    pub references_redacted: bool,
    pub result_refs: Vec<HarnessResultRef>,
    pub artifact_refs: Vec<HarnessArtifactRef>,
    pub created_at_unix_ms: u64,
    pub updated_at_unix_ms: u64,
}

impl RedactedTaskV1 {
    pub fn validate(&self) -> Result<(), HarnessReadApiError> {
        self.task_id.validate().map_err(HarnessReadApiError::Protocol)?;
        self.revision.validate().map_err(HarnessReadApiError::Protocol)?;
        validate_text("title", &self.title, HARNESS_TITLE_MAX_BYTES, false)?;
        validate_text("body", &self.body, HARNESS_BODY_MAX_BYTES, true)?;
        if let Some(parent) = &self.parent_task_id {
            parent.validate().map_err(HarnessReadApiError::Protocol)?;
        }
        validate_bounded_ids_with_max(&self.dependency_ids, HARNESS_DEPENDENCIES_MAX)?;
        validate_bounded_ids_with_max(&self.run_ids, HARNESS_LINKS_MAX)?;
        validate_bounded_ids_with_max(&self.result_refs, HARNESS_RESULTS_MAX)?;
        validate_bounded_ids_with_max(&self.artifact_refs, HARNESS_ARTIFACTS_MAX)?;
        validate_timestamps(self.created_at_unix_ms, self.updated_at_unix_ms)
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum TaskCreatorCategoryV1 { User, ParentRun }

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum RedactedWorktreeIntentV1 { Existing, Managed }

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RedactedRunIntentV1 {
    pub mode: HarnessExecutionModeV1,
    pub worktree: RedactedWorktreeIntentV1,
    pub has_delivery_bundle: bool,
    pub has_continuation: bool,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RedactedRunV1 {
    pub run_id: HarnessRunId,
    pub revision: HarnessRevision,
    pub parent_run_id: Option<HarnessRunId>,
    pub task_id: Option<HarnessTaskId>,
    pub operation_id: Option<HarnessOperationId>,
    pub intent: RedactedRunIntentV1,
    pub lifecycle: HarnessRunLifecycleV1,
    pub binding: RedactedBindingStateV1,
    pub result_disposition: Option<HarnessResultDispositionV1>,
    pub failure_category: Option<HarnessFailureCategoryV1>,
    pub context_pack: Option<HarnessResolvedContextPackReceiptV1>,
    pub git_facts: Option<HarnessRunGitFactsV1>,
    /// Opaque `hreceipt_…` ref copied from the run record. The engine
    /// requires it to equal the committed delivery receipt's `receipt_ref`.
    /// The receipt body stays on the transfer. Older payloads that omit the
    /// field deserialize as `None`.
    #[serde(default)]
    pub delivery_receipt: Option<HarnessReceiptRef>,
    pub references_redacted: bool,
    pub created_at_unix_ms: u64,
    pub updated_at_unix_ms: u64,
}

impl RedactedRunV1 {
    pub fn validate(&self) -> Result<(), HarnessReadApiError> {
        self.run_id.validate().map_err(HarnessReadApiError::Protocol)?;
        self.revision.validate().map_err(HarnessReadApiError::Protocol)?;
        if let Some(parent) = &self.parent_run_id {
            parent.validate().map_err(HarnessReadApiError::Protocol)?;
        }
        if let Some(task_id) = &self.task_id {
            task_id.validate().map_err(HarnessReadApiError::Protocol)?;
        }
        if let Some(operation_id) = &self.operation_id {
            operation_id.validate().map_err(HarnessReadApiError::Protocol)?;
        }
        if matches!(self.lifecycle, HarnessRunLifecycleV1::Running | HarnessRunLifecycleV1::Waiting)
            && self.binding == RedactedBindingStateV1::None
        {
            return Err(HarnessReadApiError::InvalidRunState);
        }
        if matches!(self.lifecycle, HarnessRunLifecycleV1::Failed) != self.failure_category.is_some() {
            return Err(HarnessReadApiError::InvalidRunState);
        }
        if let Some(pack) = &self.context_pack {
            pack.validate().map_err(HarnessReadApiError::Protocol)?;
        }
        if let Some(facts) = &self.git_facts {
            facts.validate().map_err(HarnessReadApiError::Protocol)?;
        }
        if let Some(receipt) = &self.delivery_receipt {
            receipt.validate().map_err(HarnessReadApiError::Protocol)?;
        }
        validate_timestamps(self.created_at_unix_ms, self.updated_at_unix_ms)
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum RedactedBindingStateV1 { None, ManagedDormant, ManagedActive, Inline }

/// D5, Slice D's operator-side per-task operations ledger entry
/// (`HarnessOperatorRequestV1::TaskOperations` /
/// `HarnessOperatorResponseV1::TaskOperations`), one row per
/// `HarnessEngine::operations_for_task` entry. Unlike `RedactedOperationV1`,
/// `actor` here is the RAW `HarnessActorV1` -- the operator is already
/// trusted with every other raw identity this wire exposes (session
/// records, node/workspace ids, ...), and this ledger is the audit trail
/// the plan promises ("the operator's ledger on T1 shows A's run as the
/// actor of both writes"). The agent side never gets this: `g4a_tasks_get`
/// keeps `RedactedTaskV1`'s existing category-only exposure.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HarnessOperationLedgerEntryV1 {
    pub operation_id: HarnessOperationId,
    pub created_at_unix_ms: u64,
    pub kind: HarnessOperationKindV1,
    pub actor: HarnessActorV1,
}

impl HarnessOperationLedgerEntryV1 {
    pub fn validate(&self) -> Result<(), HarnessOperatorApiError> {
        self.operation_id.validate().map_err(HarnessOperatorApiError::Protocol)?;
        self.actor.validate().map_err(HarnessOperatorApiError::Protocol)?;
        if self.created_at_unix_ms == 0 {
            return Err(HarnessOperatorApiError::InvalidOperationLedger);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RedactedOperationV1 {
    pub operation_id: HarnessOperationId,
    pub revision: HarnessRevision,
    pub kind: HarnessOperationKindV1,
    pub state: HarnessOperationStateV1,
    pub task_id: Option<HarnessTaskId>,
    pub run_id: Option<HarnessRunId>,
    pub reconciles_operation_id: Option<HarnessOperationId>,
    pub references_redacted: bool,
    pub failure_category: Option<HarnessFailureCategoryV1>,
    pub outcome_unknown_reason: Option<HarnessOutcomeUnknownReasonV1>,
    pub reconciliation_outcome: Option<HarnessReconciliationOutcomeV1>,
    pub created_at_unix_ms: u64,
    pub updated_at_unix_ms: u64,
    pub dispatched_at_unix_ms: Option<u64>,
    pub finished_at_unix_ms: Option<u64>,
}

impl RedactedOperationV1 {
    pub fn validate(&self) -> Result<(), HarnessReadApiError> {
        self.operation_id.validate().map_err(HarnessReadApiError::Protocol)?;
        self.revision.validate().map_err(HarnessReadApiError::Protocol)?;
        if let Some(task_id) = &self.task_id {
            task_id.validate().map_err(HarnessReadApiError::Protocol)?;
        }
        if let Some(run_id) = &self.run_id {
            run_id.validate().map_err(HarnessReadApiError::Protocol)?;
        }
        if let Some(operation_id) = &self.reconciles_operation_id {
            operation_id.validate().map_err(HarnessReadApiError::Protocol)?;
        }
        if (self.state == HarnessOperationStateV1::Failed) != self.failure_category.is_some()
            || (self.state == HarnessOperationStateV1::OutcomeUnknown)
                != self.outcome_unknown_reason.is_some()
            || (self.state == HarnessOperationStateV1::Reconciled)
                != self.reconciliation_outcome.is_some()
        {
            return Err(HarnessReadApiError::InvalidOperationState);
        }
        validate_timestamps(self.created_at_unix_ms, self.updated_at_unix_ms)?;
        for timestamp in [self.dispatched_at_unix_ms, self.finished_at_unix_ms].into_iter().flatten() {
            if timestamp < self.created_at_unix_ms || timestamp > self.updated_at_unix_ms {
                return Err(HarnessReadApiError::InvalidTimestamp);
            }
        }
        Ok(())
    }
}

#[derive(Debug, Error)]
pub enum HarnessReadApiError {
    #[error(
        "build stamp mismatch: this side was built from tree {expected}, the peer from \
         tree {received} -- rebuild and restart the out-of-date side (hatchery-harness \
         and its hatchery-harness-client-based peers are built from the same tree and \
         must be rolled together)"
    )]
    BuildStampMismatch { expected: String, received: String },
    #[error("harness read credential is malformed")]
    MalformedCredential,
    #[error("harness read limit is outside the supported range")]
    InvalidLimit,
    #[error("harness read cursor is invalid")]
    InvalidCursor,
    #[error("harness read context has invalid allowed tools")]
    InvalidAllowedTools,
    #[error("harness monitor counts are inconsistent")]
    InvalidMonitorCounts,
    #[error("harness monitor detail is inconsistent with visibility or availability")]
    InvalidMonitorDetail,
    #[error("harness timeline entry is invalid")]
    InvalidTimelineEntry,
    #[error("harness read collection is not canonical or bounded")]
    InvalidCollection,
    #[error("harness read timestamp is invalid")]
    InvalidTimestamp,
    #[error("harness redacted run state is invalid")]
    InvalidRunState,
    #[error("harness redacted operation state is invalid")]
    InvalidOperationState,
    #[error("harness context task or sibling runs are inconsistent with the actor run")]
    InvalidContextTask,
    #[error("harness read text field is invalid: {0}")]
    InvalidText(&'static str),
    #[error("harness task create request is invalid")]
    InvalidTaskCreate,
    #[error("harness protocol value is invalid: {0}")]
    Protocol(#[from] hatchery_harness_protocol::HarnessValidationError),
}

#[derive(Debug, Error)]
pub enum HarnessOperatorApiError {
    #[error(
        "build stamp mismatch: this side was built from tree {expected}, the peer from \
         tree {received} -- rebuild and restart the out-of-date side \
         (hatchery-harness and its hatchery-tui / hatchery-tui-light client are built \
         from the same tree and must be rolled together)"
    )]
    BuildStampMismatch { expected: String, received: String },
    #[error("harness operator credential is malformed")]
    MalformedCredential,
    #[error("harness operator limit is outside the supported range")]
    InvalidLimit,
    #[error("harness operator cursor is invalid")]
    InvalidCursor,
    #[error("harness operator request reference is malformed")]
    MalformedRequestRef,
    #[error("harness operator submission timestamp is invalid")]
    InvalidSubmittedAt,
    #[error("harness runtime inventory is invalid")]
    InvalidRuntimeInventory,
    #[error("harness run correlation is invalid")]
    InvalidRunCorrelation,
    #[error("harness run transfer summary is invalid")]
    InvalidRunTransfer,
    #[error("harness reverse attribution is invalid")]
    InvalidReverseAttribution,
    #[error("harness run context source observation is invalid")]
    InvalidRunContextSourceObservation,
    #[error("harness run workspace origin is invalid")]
    InvalidWorkspaceOrigin,
    #[error("harness repository-relative path is invalid")]
    InvalidRepositoryPath,
    #[error("harness run workspace tree is invalid")]
    InvalidWorkspaceTree,
    #[error("harness run workspace file is invalid")]
    InvalidWorkspaceFile,
    #[error("harness git status is invalid")]
    InvalidGitStatus,
    #[error("harness git summary is invalid")]
    InvalidGitSummary,
    #[error("harness git object id is invalid")]
    InvalidGitObjectId,
    #[error("harness git history is invalid")]
    InvalidGitHistory,
    #[error("harness git diff is invalid")]
    InvalidGitDiff,
    #[error("harness launch plan catalog is invalid")]
    InvalidLaunchPlans,
    #[error("harness task launch options are invalid")]
    InvalidTaskLaunchOptions,
    #[error("harness reviewed task launch selection is invalid")]
    InvalidTaskLaunchSelection,
    #[error("harness native history value is invalid")]
    InvalidNativeHistory,
    #[error("harness terminal page is invalid")]
    InvalidTerminalPage,
    #[error("harness session spawn request is invalid")]
    InvalidSessionSpawn,
    #[error("harness session control request is invalid")]
    InvalidSessionControl,
    #[error("harness agent stream content is invalid")]
    InvalidAgentStream,
    #[error("harness session record request is invalid")]
    InvalidSessionRecordRequest,
    #[error("harness host path is invalid")]
    InvalidHostPath,
    #[error("harness host directory browse request is invalid")]
    InvalidHostDirectoryBrowseRequest,
    #[error("harness workspace or worktree resource mutation request is invalid")]
    InvalidResourceMutationRequest,
    #[error("harness task operations ledger entry is invalid")]
    InvalidOperationLedger,
    #[error("harness operator response is invalid")]
    Read(#[source] HarnessReadApiError),
    #[error("harness protocol value is invalid: {0}")]
    Protocol(#[source] hatchery_harness_protocol::HarnessValidationError),
}

fn validate_credential(value: &str) -> Result<(), HarnessReadApiError> {
    if value.len() > HARNESS_READ_CREDENTIAL_MAX_BYTES || !value.starts_with(TOKEN_PREFIX) {
        return Err(HarnessReadApiError::MalformedCredential);
    }
    let (payload, proof) = value[TOKEN_PREFIX.len()..]
        .split_once('.')
        .ok_or(HarnessReadApiError::MalformedCredential)?;
    if payload.is_empty()
        || payload.len() % 2 != 0
        || proof.len() != 64
        || !payload.bytes().chain(proof.bytes()).all(|byte| byte.is_ascii_digit() || matches!(byte, b'a'..=b'f'))
    {
        return Err(HarnessReadApiError::MalformedCredential);
    }
    Ok(())
}

fn validate_operator_credential(value: &str) -> Result<(), HarnessOperatorApiError> {
    let payload = value.strip_prefix(OPERATOR_TOKEN_PREFIX)
        .ok_or(HarnessOperatorApiError::MalformedCredential)?;
    if value.len() > HARNESS_OPERATOR_CREDENTIAL_MAX_BYTES
        || payload.len() != 64
        || !payload.bytes().all(|byte| byte.is_ascii_digit() || matches!(byte, b'a'..=b'f'))
    {
        return Err(HarnessOperatorApiError::MalformedCredential);
    }
    Ok(())
}

fn validate_operator_request_ref(value: &str) -> Result<(), HarnessOperatorApiError> {
    let payload = value.strip_prefix(OPERATOR_REQUEST_REF_PREFIX)
        .ok_or(HarnessOperatorApiError::MalformedRequestRef)?;
    if payload.len() != 24
        || !payload.bytes().all(|byte| byte.is_ascii_digit() || matches!(byte, b'a'..=b'f'))
    {
        return Err(HarnessOperatorApiError::MalformedRequestRef);
    }
    Ok(())
}

fn valid_runtime_id(value: &str, maximum: usize) -> bool {
    !value.is_empty()
        && value.len() <= maximum
        && value.is_ascii()
        && value.bytes().all(|byte| {
            byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b'@' | b'+')
        })
}

fn is_lower_hex(byte: u8) -> bool {
    byte.is_ascii_digit() || matches!(byte, b'a'..=b'f')
}

fn valid_sha256_digest(value: &str) -> bool {
    value.strip_prefix("sha256:")
        .is_some_and(|digest| digest.len() == 64 && digest.bytes().all(is_lower_hex))
}

fn validate_node_workspace_route(
    node_id: &str,
    workspace_id: &str,
) -> Result<(), HarnessOperatorApiError> {
    if !valid_runtime_id(node_id, 128) || !valid_runtime_id(workspace_id, 128) {
        return Err(HarnessOperatorApiError::InvalidWorkspaceOrigin);
    }
    Ok(())
}

fn validate_repository_path(value: &str) -> Result<(), HarnessOperatorApiError> {
    if value.is_empty()
        || value.len() > HARNESS_REPOSITORY_PATH_MAX_BYTES
        || value.starts_with('/')
        || value.contains('\\')
        || value.contains(':')
        || value.chars().any(char::is_control)
        || value.split('/').any(|component| {
            component.is_empty() || matches!(component, "." | "..")
        })
    {
        return Err(HarnessOperatorApiError::InvalidRepositoryPath);
    }
    Ok(())
}

fn validate_git_object_id(value: &str) -> Result<(), HarnessOperatorApiError> {
    if !matches!(value.len(), 40 | 64) || !value.bytes().all(is_lower_hex) {
        return Err(HarnessOperatorApiError::InvalidGitObjectId);
    }
    Ok(())
}

fn validate_git_single_line(
    value: &str,
    maximum: usize,
    required: bool,
) -> Result<(), HarnessOperatorApiError> {
    if value.len() > maximum
        || required && value.is_empty()
        || value.chars().any(char::is_control)
    {
        return Err(HarnessOperatorApiError::InvalidGitSummary);
    }
    Ok(())
}

fn validate_native_session_catalog_limit(limit: u16) -> Result<(), HarnessOperatorApiError> {
    if !(1..=HARNESS_NATIVE_SESSION_CATALOG_LIMIT_MAX).contains(&limit) {
        return Err(HarnessOperatorApiError::InvalidLimit);
    }
    Ok(())
}

fn valid_native_selection_id(value: &str) -> bool {
    !value.trim().is_empty()
        && value.len() <= 1_024
        && value.bytes().all(|byte| {
            byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.')
        })
}

fn valid_native_group_id(value: &str) -> bool {
    !value.trim().is_empty()
        && value.len() <= 128
        && value.bytes().all(|byte| {
            byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.')
        })
}

fn valid_native_single_line(value: &str, maximum: usize, required: bool) -> bool {
    (!required || !value.trim().is_empty())
        && value.len() <= maximum
        && !value.chars().any(char::is_control)
}

/// Mirrors `gate4agent-node-protocol`'s own `contains_unsafe_control_bytes`:
/// every control character is unsafe except the three ACP content is allowed
/// to carry verbatim (`\n`, `\r`, `\t`), itself mirroring
/// `gate4agent_types::control::validate_text`'s own allowance, so
/// agent-stream free text (`Text`, `Thinking`, `title`, `prompt`,
/// `description`, `value_json`, `label`) and `SetSessionConfigOption`'s
/// `value_json` validate against the same rule the provider event stream
/// already applies to it.
fn contains_unsafe_control_bytes(value: &str) -> bool {
    value.chars().any(|character| {
        character.is_control() && !matches!(character, '\n' | '\r' | '\t')
    })
}

/// Structural bound for an ACP-minted identifier (`mode_id`/`option_id`/
/// `model_id`/`tool_name`/catalog entry `id`/`name`/interaction option
/// `option_id`/`name`/`kind`) -- mirrors `gate4agent-node-protocol`'s own
/// `deserialize_acp_control_id`: non-empty, bounded by
/// `HARNESS_AGENT_STREAM_ID_MAX_BYTES`, free of every control character
/// (never just the unsafe subset `contains_unsafe_control_bytes` allows --
/// an id is not free text).
fn valid_agent_stream_id(value: &str) -> bool {
    !value.is_empty()
        && value.len() <= HARNESS_AGENT_STREAM_ID_MAX_BYTES
        && !value.chars().any(char::is_control)
}

/// Structural bound for `ResolveInteraction`'s `correlation_id` and
/// `InteractionPrompt`'s own copy of it -- mirrors `gate4agent-node-
/// protocol`'s own `deserialize_acp_correlation_id`, bounded by
/// `HARNESS_OBSERVATION_LABEL_MAX_BYTES` (the same source constant,
/// `hatchery_observation_protocol::OBSERVATION_LABEL_MAX_BYTES`, that
/// bound already mirrors) since a correlation id is exactly what
/// `ObservationKindV1::ApprovalRequested`/`QuestionRequested` minted onto
/// the timeline at that bound.
fn valid_agent_stream_correlation_id(value: &str) -> bool {
    !value.is_empty()
        && value.len() <= HARNESS_OBSERVATION_LABEL_MAX_BYTES
        && !value.chars().any(char::is_control)
}

fn validate_native_entries(
    route: &HarnessNativeSessionRouteV1,
    entries: &[HarnessNativeSessionCatalogEntryV1],
) -> Result<(), HarnessOperatorApiError> {
    if entries.len() > usize::from(HARNESS_NATIVE_SESSION_CATALOG_LIMIT_MAX) {
        return Err(HarnessOperatorApiError::InvalidNativeHistory);
    }
    for (index, entry) in entries.iter().enumerate() {
        entry.validate_for_route(route)?;
        if entries[..index].iter().any(|existing| {
            existing.selection_id == entry.selection_id
                || entry.record_id.is_some() && existing.record_id == entry.record_id
        }) {
            return Err(HarnessOperatorApiError::InvalidNativeHistory);
        }
    }
    Ok(())
}

fn validate_operator_limit(limit: u16) -> Result<(), HarnessOperatorApiError> {
    if !(1..=HARNESS_ENTITY_PAGE_LIMIT_MAX).contains(&limit) {
        return Err(HarnessOperatorApiError::InvalidLimit);
    }
    Ok(())
}

fn validate_operator_timeline_limit(limit: u16) -> Result<(), HarnessOperatorApiError> {
    if !(1..=HARNESS_TIMELINE_PAGE_LIMIT_MAX).contains(&limit) {
        return Err(HarnessOperatorApiError::InvalidLimit);
    }
    Ok(())
}

fn validate_operator_terminal_limit(limit: u16) -> Result<(), HarnessOperatorApiError> {
    if !(1..=HARNESS_TERMINAL_PAGE_LIMIT_MAX).contains(&limit) {
        return Err(HarnessOperatorApiError::InvalidLimit);
    }
    Ok(())
}

fn validate_limit(limit: u16, maximum: u16) -> Result<(), HarnessReadApiError> {
    if !(1..=maximum).contains(&limit) {
        return Err(HarnessReadApiError::InvalidLimit);
    }
    Ok(())
}

fn validate_bounded<T>(
    values: &[T],
    maximum: u16,
    validate: impl Fn(&T) -> Result<(), HarnessReadApiError>,
) -> Result<(), HarnessReadApiError> {
    if values.len() > maximum as usize {
        return Err(HarnessReadApiError::InvalidCollection);
    }
    for value in values { validate(value)?; }
    Ok(())
}

fn validate_bounded_ids_with_max<T: Ord>(
    values: &[T],
    maximum: usize,
) -> Result<(), HarnessReadApiError> {
    if values.len() > maximum
        || values.windows(2).any(|pair| pair[0] >= pair[1])
    {
        return Err(HarnessReadApiError::InvalidCollection);
    }
    Ok(())
}

fn validate_monitor_facts<T>(values: &[T]) -> Result<(), HarnessReadApiError> {
    if values.len() > HARNESS_MONITOR_FACTS_MAX {
        return Err(HarnessReadApiError::InvalidCollection);
    }
    Ok(())
}

fn validate_optional_observation_text(
    field: &'static str,
    value: Option<&str>,
    maximum: usize,
) -> Result<(), HarnessReadApiError> {
    let Some(value) = value else { return Ok(()); };
    if value.is_empty() || value.len() > maximum || value.chars().any(char::is_control) {
        return Err(HarnessReadApiError::InvalidText(field));
    }
    Ok(())
}

fn validate_observation_correlation(value: Option<u16>) -> Result<(), HarnessReadApiError> {
    if value.is_some_and(|value| value == 0 || usize::from(value) > HARNESS_MONITOR_FACTS_MAX) {
        return Err(HarnessReadApiError::InvalidTimelineEntry);
    }
    Ok(())
}

fn validate_observation_relative_path(path: &str) -> Result<(), HarnessReadApiError> {
    if path.is_empty()
        || path.len() > HARNESS_OBSERVATION_PATH_MAX_BYTES
        || path.starts_with('/')
        || path.contains('\\')
        || path.contains(':')
        || path.chars().any(char::is_control)
        || path.split('/').any(|component| {
            component.is_empty() || matches!(component, "." | "..")
        })
    {
        return Err(HarnessReadApiError::InvalidText("relative path"));
    }
    Ok(())
}

fn require_observed_or_empty<T>(
    state: FeatureObservationStateV1,
    values: &[T],
) -> Result<(), HarnessReadApiError> {
    if state != FeatureObservationStateV1::Observed && !values.is_empty() {
        return Err(HarnessReadApiError::InvalidMonitorDetail);
    }
    Ok(())
}

fn validate_feature_counts(monitor: &SessionMonitorV1) -> Result<(), HarnessReadApiError> {
    if monitor.features.todo != FeatureObservationStateV1::Observed
        && (monitor.todo_total != 0 || monitor.todo_completed != 0)
        || monitor.features.tools != FeatureObservationStateV1::Observed
            && monitor.active_tools != 0
        || monitor.features.subagents != FeatureObservationStateV1::Observed
            && monitor.active_subagents != 0
        || monitor.features.interactions != FeatureObservationStateV1::Observed
            && monitor.active_interactions != 0
        || monitor.features.owned_processes != FeatureObservationStateV1::Observed
            && monitor.active_processes != 0
        || monitor.features.usage != FeatureObservationStateV1::Observed
            && (monitor.input_tokens != 0
                || monitor.output_tokens != 0
                || monitor.cache_read_tokens != 0
                || monitor.cache_write_tokens != 0
                || monitor.reasoning_tokens != 0
                || monitor.context_window_tokens.is_some())
    {
        return Err(HarnessReadApiError::InvalidMonitorDetail);
    }
    if matches!(monitor.availability, ProjectionAvailabilityV1::Unknown | ProjectionAvailabilityV1::NotObserved)
        && [
            monitor.features.todo,
            monitor.features.tools,
            monitor.features.subagents,
            monitor.features.interactions,
            monitor.features.owned_processes,
            monitor.features.files,
            monitor.features.usage,
            monitor.features.history,
        ]
        .contains(&FeatureObservationStateV1::Observed)
    {
        return Err(HarnessReadApiError::InvalidMonitorDetail);
    }
    Ok(())
}

fn validate_text(
    field: &'static str,
    value: &str,
    maximum: usize,
    allow_empty: bool,
) -> Result<(), HarnessReadApiError> {
    if value.len() > maximum || !allow_empty && value.trim().is_empty() || value.contains('\0') {
        return Err(HarnessReadApiError::InvalidText(field));
    }
    Ok(())
}

fn validate_timestamps(created: u64, updated: u64) -> Result<(), HarnessReadApiError> {
    if created == 0 || updated < created {
        return Err(HarnessReadApiError::InvalidTimestamp);
    }
    Ok(())
}

fn valid_transfer_timestamps<const N: usize>(
    created: u64,
    updated: u64,
    optional: [Option<u64>; N],
) -> bool {
    created != 0
        && updated >= created
        && optional.into_iter().flatten().all(|timestamp| {
            timestamp >= created && timestamp <= updated
        })
}

#[cfg(test)]
mod tests {
    use super::*;

    fn managed_run_correlation() -> HarnessRunCorrelationV1 {
        HarnessRunCorrelationV1 {
            run_id: HarnessRunId::new(format!("hrun_{}", "a".repeat(24))).unwrap(),
            run_revision: HarnessRevision::new(7).unwrap(),
            task_id: HarnessTaskId::new(format!("htask_{}", "b".repeat(24))).unwrap(),
            node_id: HarnessSelectorV1::new("node-a").unwrap(),
            node_incarnation_id: HarnessNodeIncarnationV1::new("07".repeat(16)).unwrap(),
            workspace_id: HarnessSelectorV1::new("workspace-a").unwrap(),
            provider_profile: HarnessSelectorV1::new("codex-default").unwrap(),
            mode: HarnessExecutionModeV1::Pty,
            worktree: HarnessRunWorktreeViewV1::Managed {
                worktree_ref: HarnessSelectorV1::new("worktree-a").unwrap(),
            },
            session: HarnessRunSessionViewV1::Managed(HarnessManagedRunSessionV1 {
                record_id: HarnessSelectorV1::new("record-a").unwrap(),
                active_session: Some(HarnessRuntimeIdentityV1 {
                    instance_id: 41,
                    generation: 3,
                }),
            }),
            availability: HarnessRunCorrelationAvailabilityV1::Available,
            observed_at_unix_ms: Some(100),
        }
    }

    fn run_transfer_summary() -> HarnessRunTransferSummaryV1 {
        let run_id = HarnessRunId::new(format!("hrun_{}", "a".repeat(24))).unwrap();
        HarnessRunTransferSummaryV1 {
            run_id: run_id.clone(),
            run_revision: HarnessRevision::new(9).unwrap(),
            delivery: Some(HarnessRunDeliveryTransferV1 {
                delivery_ref: HarnessDeliveryRef::new(format!(
                    "hdelivery_{}",
                    "b".repeat(24),
                )).unwrap(),
                revision: HarnessRevision::new(3).unwrap(),
                state: HarnessDeliveryStateV1::Committed,
                selector: HarnessSelectorV1::new("delivery-safe").unwrap(),
                bundle_id: HarnessDeliveryBundleIdV1::new("bundle-safe").unwrap(),
                bundle_revision: HarnessDeliveryBundleRevisionV1::new("revision-4").unwrap(),
                bundle_digest: HarnessDeliveryBundleDigestV1::new(format!(
                    "sha256:{}",
                    "c".repeat(64),
                )).unwrap(),
                manifest_digest: HarnessDeliveryManifestDigestV2::new(format!(
                    "sha256:{}",
                    "d".repeat(64),
                )).unwrap(),
                receipt_ref: Some(HarnessReceiptRef::new(format!(
                    "hreceipt_{}",
                    "e".repeat(24),
                )).unwrap()),
                created_at_unix_ms: 10,
                updated_at_unix_ms: 30,
                staged_at_unix_ms: Some(20),
                committed_at_unix_ms: Some(30),
            }),
            continuation: Some(HarnessRunContinuationTransferV1 {
                continuation_ref: HarnessContinuationRef::new(format!(
                    "hcontinuation_{}",
                    "f".repeat(24),
                )).unwrap(),
                receipt_ref: HarnessReceiptRef::new(format!(
                    "hreceipt_{}",
                    "0".repeat(24),
                )).unwrap(),
                revision: HarnessRevision::new(4).unwrap(),
                state: HarnessContinuationStateV1::Bound,
                source_run_id: HarnessRunId::new(format!(
                    "hrun_{}",
                    "1".repeat(24),
                )).unwrap(),
                target_run_id: run_id,
                source_provider: HarnessSelectorV1::new("claude").unwrap(),
                context: Some(HarnessRunContextTransferV1 {
                    context_ref: HarnessSelectorV1::new("context-safe").unwrap(),
                    digest: format!("sha256:{}", "2".repeat(64)),
                    source_message_count: 12,
                    retained_message_count: 8,
                    byte_len: 4096,
                    truncated: true,
                }),
                prepared_at_unix_ms: 10,
                exporting_at_unix_ms: Some(15),
                exported_at_unix_ms: Some(20),
                bound_at_unix_ms: Some(25),
                expired_at_unix_ms: None,
                outcome_unknown_at_unix_ms: None,
                outcome_unknown_reason: None,
                created_at_unix_ms: 10,
                updated_at_unix_ms: 25,
            }),
        }
    }

    fn run_workspace_origin() -> HarnessRunWorkspaceOriginV1 {
        HarnessRunWorkspaceOriginV1 {
            run_id: HarnessRunId::new(format!("hrun_{}", "a".repeat(24))).unwrap(),
            run_revision: HarnessRevision::new(7).unwrap(),
            node_id: HarnessSelectorV1::new("node-a").unwrap(),
            node_incarnation_id: HarnessNodeIncarnationV1::new("07".repeat(16)).unwrap(),
            workspace_id: HarnessSelectorV1::new("workspace-a").unwrap(),
        }
    }

    fn node_workspace_origin() -> HarnessNodeWorkspaceOriginV1 {
        HarnessNodeWorkspaceOriginV1 {
            node_id: "node-a".to_owned(),
            node_incarnation_id: "07".repeat(16),
            workspace_id: "workspace-a".to_owned(),
        }
    }

    fn reverse_attribution_workspace() -> HarnessReverseAttributionWorkspaceV1 {
        HarnessReverseAttributionWorkspaceV1 {
            node_id: HarnessSelectorV1::new("node-a").unwrap(),
            node_incarnation_id: HarnessNodeIncarnationV1::new("07".repeat(16)).unwrap(),
            workspace_id: HarnessSelectorV1::new("workspace-a").unwrap(),
        }
    }

    fn reverse_attribution_link(run_byte: char) -> HarnessReverseAttributionLinkV1 {
        HarnessReverseAttributionLinkV1 {
            task_id: HarnessTaskId::new(format!("htask_{}", "1".repeat(24))).unwrap(),
            run_id: HarnessRunId::new(format!("hrun_{}", run_byte.to_string().repeat(24)))
                .unwrap(),
            run_revision: HarnessRevision::new(3).unwrap(),
            binding: HarnessReverseAttributionBindingV1::Workspace {
                workspace: reverse_attribution_workspace(),
            },
            relation: HarnessReverseAttributionRelationV1::WorkspaceScope,
        }
    }

    fn git_commit(id_byte: char) -> HarnessGitCommitV1 {
        HarnessGitCommitV1 {
            id: HarnessGitObjectIdV1::new(id_byte.to_string().repeat(40)).unwrap(),
            parents: Vec::new(),
            subject: "Bounded subject".to_owned(),
            author_name: "Author".to_owned(),
            authored_at: "2026-08-16T10:00:00Z".to_owned(),
            committer_name: "Committer".to_owned(),
            committed_at: "2026-08-16T10:00:00Z".to_owned(),
            signature_status: HarnessGitSignatureStatusV1::NoSignature,
            signer: None,
        }
    }

    fn assert_json_has_no_forbidden_keys(encoded: &str, forbidden: &[&str]) {
        fn visit(value: &serde_json::Value, forbidden: &[&str]) {
            match value {
                serde_json::Value::Object(fields) => {
                    for (field, value) in fields {
                        assert!(
                            !forbidden.iter().any(|forbidden| field == forbidden),
                            "response exposed structural field {field}",
                        );
                        visit(value, forbidden);
                    }
                }
                serde_json::Value::Array(values) => {
                    for value in values { visit(value, forbidden); }
                }
                _ => {}
            }
        }

        let value: serde_json::Value = serde_json::from_str(encoded).unwrap();
        visit(&value, forbidden);
    }

    #[test]
    fn credential_debug_and_errors_never_expose_secret() {
        let credential = HarnessReadCredential::parse(format!("g4ah2_aa.{}", "0".repeat(64)))
            .expect("credential");
        assert_eq!(format!("{credential:?}"), "HarnessReadCredential([REDACTED])");
        assert!(!HarnessReadApiError::MalformedCredential.to_string().contains(credential.expose()));
    }

    #[test]
    fn operator_and_agent_credentials_are_strictly_separated_and_redacted() {
        let operator = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).expect("operator credential");
        let read = format!("g4ah2_aa.{}", "0".repeat(64));
        assert!(HarnessOperatorCredential::parse(&read).is_err());
        assert!(HarnessReadCredential::parse(operator.expose()).is_err());
        assert_eq!(
            format!("{operator:?}"),
            "HarnessOperatorCredential([REDACTED])",
        );
        assert!(!HarnessOperatorApiError::MalformedCredential
            .to_string().contains(operator.expose()));
    }

    #[test]
    fn operator_frames_enforce_exact_cursor_and_collection_bounds() {
        assert!(HarnessOperatorRequestV1::TasksList {
            after_task_id: None,
            state: None,
            parent_task_id: None,
            limit: HARNESS_ENTITY_PAGE_LIMIT_MAX,
        }.validate().is_ok());
        assert!(HarnessOperatorRequestV1::TasksList {
            after_task_id: None,
            state: None,
            parent_task_id: None,
            limit: HARNESS_ENTITY_PAGE_LIMIT_MAX + 1,
        }.validate().is_err());
        let run_id = HarnessRunId::new(format!("hrun_{}", "a".repeat(24))).unwrap();
        assert!(HarnessOperatorRequestV1::TimelineRead {
            run_id,
            after_sequence: Some(0),
            limit: 1,
        }.validate().is_err());
    }

    #[test]
    fn legacy_raw_schedule_variant_json_is_rejected() {
        let legacy = r#"{"kind":"schedule-ready-task","request":{}}"#;
        assert!(serde_json::from_str::<HarnessOperatorRequestV1>(legacy).is_err());
    }

    #[test]
    fn operator_intent_is_authority_free() {
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        let request = HarnessOperatorRequestV1::SubmitIntent {
            intent: HarnessOperatorIntentV1 {
                request_ref: HarnessOperatorRequestRefV1::new(format!(
                    "hireq_{}",
                    "1".repeat(24),
                )).unwrap(),
                submitted_at_unix_ms: 10,
                action: HarnessOperatorActionV1::CreateTask {
                    title: "Harness-owned identity".to_owned(),
                    body: "Typed user intent".to_owned(),
                    parent_task_id: None,
                    dependencies: Vec::new(),
                    initial_state: HarnessTaskStateV1::Backlog,
                },
            },
        };
        let encoded_request = serde_json::to_string(&request).unwrap();
        assert!(!encoded_request.contains("authority"));
        assert!(!encoded_request.contains("operation_id"));
        assert!(!encoded_request.contains("idempotency_ref"));
        assert!(!encoded_request.contains("\"task_id\":"));
        let envelope = HarnessOperatorEnvelopeV1 {
            build_stamp: BUILD_STAMP.to_string(),
            credential,
            request,
        };
        assert!(envelope.validate().is_ok());
        let decoded: HarnessOperatorEnvelopeV1 = serde_json::from_slice(
            &serde_json::to_vec(&envelope).unwrap(),
        ).unwrap();
        assert_eq!(decoded, envelope);
    }

    #[test]
    fn operator_run_correlation_is_exact() {
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        let request = HarnessOperatorRequestV1::RunCorrelationGet {
            run_id: HarnessRunId::new(format!("hrun_{}", "c".repeat(24))).unwrap(),
        };
        let envelope = HarnessOperatorEnvelopeV1 {
            build_stamp: BUILD_STAMP.to_string(),
            credential,
            request,
        };
        envelope.validate().unwrap();
        let decoded: HarnessOperatorEnvelopeV1 = serde_json::from_slice(
            &serde_json::to_vec(&envelope).unwrap(),
        ).unwrap();
        assert_eq!(decoded, envelope);
    }

    #[test]
    fn operator_run_transfer_is_exact_private() {
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        let summary = run_transfer_summary();
        summary.validate().unwrap();
        let request = HarnessOperatorRequestV1::RunTransferGet {
            run_id: summary.run_id.clone(),
        };
        HarnessOperatorEnvelopeV1 {
            build_stamp: BUILD_STAMP.to_string(),
            credential,
            request,
        }.validate().unwrap();

        let response = HarnessOperatorResponseV1::RunTransfer(summary.clone());
        response.validate().unwrap();
        let encoded = serde_json::to_string(&response).unwrap();
        let decoded: HarnessOperatorResponseV1 = serde_json::from_str(&encoded).unwrap();
        assert_eq!(decoded, response);
        for forbidden in [
            "provider_home",
            "provider_native",
            "session_id",
            "source_root",
            "component_path",
            "component_name",
            "blob_bytes",
            "prompt",
            "node_store",
            "credential",
            "auth",
            "C:\\\\",
        ] {
            assert!(!encoded.contains(forbidden), "response exposed {forbidden}");
        }

        let mut wrong_target = summary.clone();
        wrong_target.continuation.as_mut().unwrap().target_run_id =
            HarnessRunId::new(format!("hrun_{}", "9".repeat(24))).unwrap();
        assert!(matches!(
            wrong_target.validate(),
            Err(HarnessOperatorApiError::InvalidRunTransfer),
        ));

        let mut invalid_delivery = summary.clone();
        invalid_delivery.delivery.as_mut().unwrap().committed_at_unix_ms = None;
        assert!(matches!(
            invalid_delivery.validate(),
            Err(HarnessOperatorApiError::InvalidRunTransfer),
        ));

        let mut invalid_context = summary;
        invalid_context.continuation.as_mut().unwrap()
            .context.as_mut().unwrap().truncated = false;
        assert!(matches!(
            invalid_context.validate(),
            Err(HarnessOperatorApiError::InvalidRunTransfer),
        ));
    }

    fn ordinary_launch_plan(plan_id: &str, digest: char) -> HarnessLaunchPlanSummaryV1 {
        HarnessLaunchPlanSummaryV1 {
            scheduled_launch: HarnessScheduledLaunchRefV2 {
                plan: HarnessLaunchPlanRefV1 {
                    plan_id: HarnessSelectorV1::new(plan_id).unwrap(),
                    revision: HarnessRevision::new(2).unwrap(),
                    digest: HarnessRequestDigest::new(digest.to_string().repeat(64)).unwrap(),
                },
                authority: HarnessLaunchAuthorityRefV1::OrdinaryOperator,
            },
            node_id: HarnessSelectorV1::new("node-a").unwrap(),
            workspace_id: HarnessSelectorV1::new("workspace-a").unwrap(),
            worktree: HarnessWorktreeIntentV1::Managed {
                worktree_ref: HarnessSelectorV1::new("worktree-a").unwrap(),
            },
            provider_profile: HarnessSelectorV1::new("codex-default").unwrap(),
            provider_id: HarnessSelectorV1::new("codex").unwrap(),
            mode: HarnessExecutionModeV1::Pty,
        }
    }

    fn task_launch_options() -> HarnessTaskLaunchOptionsV1 {
        let task_id = HarnessTaskId::new(format!("htask_{}", "b".repeat(24))).unwrap();
        let legacy_plan = ordinary_launch_plan("plan-a", 'a');
        let plan = HarnessOrdinaryLaunchPlanOptionV1 {
            plan: legacy_plan.scheduled_launch.plan,
            node_id: legacy_plan.node_id,
            source_workspace_id: legacy_plan.workspace_id,
            provider_profile: legacy_plan.provider_profile,
            provider_id: legacy_plan.provider_id,
            mode: legacy_plan.mode,
        };
        let context = HarnessContextSourceSelectionV1 {
            source_run_id: HarnessRunId::new(format!("hrun_{}", "c".repeat(24))).unwrap(),
            source_run_revision: HarnessRevision::new(7).unwrap(),
            observed_at_unix_ms: 90,
            metadata_digest: HarnessRequestDigest::new("d".repeat(64)).unwrap(),
            node_id: HarnessSelectorV1::new("node-a").unwrap(),
            node_incarnation: HarnessSelectorV1::new("07".repeat(16)).unwrap(),
            workspace_id: HarnessSelectorV1::new("source-workspace").unwrap(),
            session_record_id: HarnessSelectorV1::new("record-a").unwrap(),
            active_session: Some(HarnessRuntimeIdentityV1 { instance_id: 41, generation: 3 }),
            message_count: 12,
            message_count_exact: true,
            completed_turn_count: Some(5),
            total_tokens: Some(4096),
            availability: HarnessContextSourceAvailabilityV1::Live,
            context_pack: None,
        };
        let delivery = HarnessDeliveryBundleSelectionV1 {
            bundle: HarnessDeliveryBundleV1 {
                selector: HarnessSelectorV1::new("review-kit").unwrap(),
                bundle_id: HarnessDeliveryBundleIdV1::new("bundle.review-kit").unwrap(),
                revision: HarnessDeliveryBundleRevisionV1::new("revision-7").unwrap(),
                digest: HarnessDeliveryBundleDigestV1::new(format!(
                    "sha256:{}",
                    "e".repeat(64),
                )).unwrap(),
                manifest_digest: HarnessDeliveryManifestDigestV2::new(format!(
                    "sha256:{}",
                    "f".repeat(64),
                )).unwrap(),
            },
            component_counts: vec![HarnessDeliveryComponentCountV1 {
                kind: HarnessDeliveryComponentKindV1::Skill,
                workspace_count: 2,
                session_count: 1,
            }],
        };
        let issuance = HarnessTaskLaunchIssuanceRefV1 {
            issuance_id: HarnessTaskLaunchIssuanceId::new(format!(
                "hissue_{}",
                "1".repeat(24),
            )).unwrap(),
            revision: HarnessRevision::new(2).unwrap(),
            digest: HarnessRequestDigest::new("2".repeat(64)).unwrap(),
        };
        HarnessTaskLaunchOptionsV1 {
            task_id: task_id.clone(),
            task_revision: HarnessRevision::new(5).unwrap(),
            policy_digest: HarnessRequestDigest::new("3".repeat(64)).unwrap(),
            plans: vec![plan],
            managed_worktree_profiles: vec![HarnessManagedWorktreeProfileOptionV1 {
                node_id: HarnessSelectorV1::new("node-a").unwrap(),
                node_incarnation: HarnessSelectorV1::new("07".repeat(16)).unwrap(),
                source_workspace_id: HarnessSelectorV1::new("workspace-a").unwrap(),
                profile_id: HarnessSelectorV1::new("review").unwrap(),
                profile_revision: HarnessSelectorV1::new("review.r7").unwrap(),
                retention: HarnessManagedWorktreeRetentionV1::RemoveWhenReleased,
                observed_at_unix_ms: 90,
            }],
            context_sources: vec![context],
            delivery_bundles: vec![delivery],
            current_issued_spec: Some(HarnessIssuedExecutionSpecSummaryV1 {
                task_id,
                execution_spec_id: HarnessExecutionSpecId::new(format!(
                    "hespec_{}",
                    "4".repeat(24),
                )).unwrap(),
                revision: HarnessRevision::new(2).unwrap(),
                launch_issuance: issuance,
                review_policy: HarnessTaskReviewPolicyV1::OperatorReview,
                created_at_unix_ms: 80,
                updated_at_unix_ms: 90,
            }),
            truncated: false,
            next_after: None,
            context_source_exclusions: vec![ContextSourceExclusionEntryV1 {
                run_id: HarnessRunId::new(format!("hrun_{}", "9".repeat(24))).unwrap(),
                exclusion: ContextSourceExclusionV1::NodeIncarnationUnknown {
                    node_id: HarnessSelectorV1::new("node-a").unwrap(),
                    node_incarnation: HarnessSelectorV1::new("07".repeat(16)).unwrap(),
                    known_incarnations: vec![HarnessSelectorV1::new("08".repeat(16)).unwrap()],
                },
            }],
        }
    }

    #[test]
    fn operator_task_launch_contract_is_exact_and_private() {
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        let options = task_launch_options();
        options.validate().unwrap();
        let task_id = options.task_id.clone();
        let authority = HarnessOperatorAuthorityV1 {
            operation_id: HarnessOperationId::new(format!("hop_{}", "5".repeat(24))).unwrap(),
            idempotency_ref: HarnessIdempotencyRef::new(format!(
                "hidem_{}",
                "6".repeat(24),
            )).unwrap(),
            actor_id: HarnessSelectorV1::new("operator").unwrap(),
            now_unix_ms: 100,
        };
        let selection = HarnessReviewedTaskLaunchSelectionV1 {
            plan: options.plans[0].clone(),
            worktree: HarnessReviewedWorktreeSelectionV1::Managed {
                profile: options.managed_worktree_profiles[0].clone(),
            },
            context_source: Some(options.context_sources[0].clone()),
            delivery: Some(options.delivery_bundles[0].clone()),
            review_policy: HarnessTaskReviewPolicyV1::OperatorReview,
        };
        let replace = HarnessOperatorRequestV1::ReplaceTaskExecutionSpecV2 {
            request: HarnessReplaceTaskExecutionSpecRequestV2 {
                authority: authority.clone(),
                task_id: task_id.clone(),
                expected_task_revision: options.task_revision,
                expected_execution_spec_revision:
                    HarnessExpectedExecutionSpecRevisionV1::Exact(HarnessRevision::new(2).unwrap()),
                selection: selection.clone(),
            },
        };
        let issuance = options.current_issued_spec.as_ref().unwrap().launch_issuance.clone();
        let start = HarnessOperatorRequestV1::StartTaskV2 {
            request: HarnessStartTaskRequestV2 {
                authority: authority.clone(),
                task_id: task_id.clone(),
                expected_task_revision: options.task_revision,
                expected_execution_spec_revision: HarnessRevision::new(2).unwrap(),
                expected_launch_issuance: issuance,
            },
        };
        for request in [
            HarnessOperatorRequestV1::TaskLaunchOptionsGet {
                task_id: task_id.clone(),
                provider: None,
                workspace: None,
                plan_id: None,
                after: None,
            },
            replace.clone(),
            start.clone(),
        ] {
            request.validate().unwrap();
        }
        HarnessOperatorEnvelopeV1 {
            build_stamp: BUILD_STAMP.to_string(),
            credential,
            request: replace.clone(),
        }.validate().unwrap();

        let replace_json = serde_json::to_string(&replace).unwrap();
        for forbidden in [
            "issuance_id",
            "policy_digest",
            "provider_home",
            "credential",
            "canonical_root",
            "display_root",
            "prompt",
        ] {
            assert!(!replace_json.contains(forbidden), "leaked {forbidden}");
        }
        let mut unknown = serde_json::to_value(start).unwrap();
        unknown["request"]["caller_issuance_digest"] = serde_json::json!("untrusted");
        assert!(serde_json::from_value::<HarnessOperatorRequestV1>(unknown).is_err());

        let intent = HarnessOperatorIntentV1 {
            request_ref: HarnessOperatorRequestRefV1::new(format!(
                "hireq_{}",
                "7".repeat(24),
            )).unwrap(),
            submitted_at_unix_ms: 100,
            action: HarnessOperatorActionV1::ReplaceTaskExecutionSpecV2 {
                task_id,
                expected_task_revision: options.task_revision,
                expected_execution_spec_revision:
                    HarnessExpectedExecutionSpecRevisionV1::Exact(HarnessRevision::new(2).unwrap()),
                selection,
            },
        };
        let intent_json = serde_json::to_string(&intent).unwrap();
        assert!(!intent_json.contains("authority"));
        let start_intent = HarnessOperatorIntentV1 {
            request_ref: HarnessOperatorRequestRefV1::new(format!(
                "hireq_{}",
                "8".repeat(24),
            )).unwrap(),
            submitted_at_unix_ms: 101,
            action: HarnessOperatorActionV1::StartTaskV2 {
                task_id: options.task_id.clone(),
                expected_task_revision: options.task_revision,
                expected_execution_spec_revision: HarnessRevision::new(2).unwrap(),
                expected_launch_issuance: options.current_issued_spec.as_ref().unwrap()
                    .launch_issuance.clone(),
            },
        };
        let start_intent_json = serde_json::to_string(&start_intent).unwrap();
        assert!(!start_intent_json.contains("authority"));
    }

    #[test]
    fn task_launch_options_are_bounded_canonical_correlated_and_redacted() {
        let options = task_launch_options();
        options.validate_for(&options.task_id).unwrap();
        let encoded = serde_json::to_string(&HarnessOperatorResponseV1::TaskLaunchOptions(
            options.clone(),
        )).unwrap();
        for forbidden in [
            "canonical_root",
            "display_root",
            "provider_session_id",
            "provider_home",
            "credential",
            "auth_token",
            "source_path",
            "payload",
        ] {
            assert!(!encoded.contains(forbidden), "leaked {forbidden}");
        }

        let mut duplicate = options.clone();
        duplicate.plans.push(duplicate.plans[0].clone());
        assert!(matches!(
            duplicate.validate(),
            Err(HarnessOperatorApiError::InvalidTaskLaunchOptions),
        ));
        let mut oversized = options.clone();
        oversized.context_sources = vec![
            options.context_sources[0].clone();
            HARNESS_TASK_LAUNCH_OPTIONS_MAX + 1
        ];
        assert!(matches!(
            oversized.validate(),
            Err(HarnessOperatorApiError::InvalidTaskLaunchOptions),
        ));
        let other_task = HarnessTaskId::new(format!("htask_{}", "8".repeat(24))).unwrap();
        assert!(matches!(
            options.validate_for(&other_task),
            Err(HarnessOperatorApiError::InvalidTaskLaunchOptions),
        ));
    }

    /// The launch-catalogue ceiling fix's read-side pagination: `provider`/
    /// `workspace`/`plan_id`/`after` on the request and `next_after` on the
    /// response are additive over the pre-pagination wire shape, so an older
    /// peer's JSON (never carrying any of them) must still decode.
    #[test]
    fn task_launch_options_paging_fields_are_additive_and_next_after_is_checked() {
        let task_id = HarnessTaskId::new(format!("htask_{}", "9".repeat(24))).unwrap();
        let legacy_request_json = serde_json::json!({
            "kind": "task-launch-options-get",
            "task_id": task_id.as_str(),
        });
        let decoded: HarnessOperatorRequestV1 =
            serde_json::from_value(legacy_request_json).unwrap();
        assert!(matches!(
            decoded,
            HarnessOperatorRequestV1::TaskLaunchOptionsGet {
                provider: None, workspace: None, plan_id: None, after: None, ..
            },
        ));

        let mut options = task_launch_options();
        let legacy_response_json = serde_json::to_value(&options).unwrap();
        assert!(legacy_response_json.get("next_after").is_some());
        let mut without_next_after = legacy_response_json.clone();
        without_next_after.as_object_mut().unwrap().remove("next_after");
        let redecoded: HarnessTaskLaunchOptionsV1 =
            serde_json::from_value(without_next_after).unwrap();
        assert_eq!(redecoded.next_after, None);
        redecoded.validate().unwrap();

        let plan_id = options.plans[0].plan.plan_id.clone();
        options.next_after = Some(plan_id.clone());
        assert!(matches!(
            options.validate(),
            Err(HarnessOperatorApiError::InvalidTaskLaunchOptions),
        ), "next_after without truncated must be refused");
        options.truncated = true;
        options.validate().unwrap();
        options.next_after = Some(HarnessSelectorV1::new("not-the-last-plan").unwrap());
        assert!(matches!(
            options.validate(),
            Err(HarnessOperatorApiError::InvalidTaskLaunchOptions),
        ), "next_after must name the last plan on the page");
    }

    /// `context_source_exclusions` is additive over the pre-instrumentation
    /// wire shape (same discipline as `next_after` above), round-trips
    /// exactly, and is bounded/ordered independently of the other four
    /// lists.
    #[test]
    fn context_source_exclusions_are_additive_bounded_and_ordered() {
        let options = task_launch_options();
        options.validate_for(&options.task_id).unwrap();
        assert_eq!(options.context_source_exclusions.len(), 1);

        let mut legacy_json = serde_json::to_value(&options).unwrap();
        legacy_json.as_object_mut().unwrap().remove("context_source_exclusions");
        let redecoded: HarnessTaskLaunchOptionsV1 = serde_json::from_value(legacy_json).unwrap();
        assert!(redecoded.context_source_exclusions.is_empty());
        redecoded.validate().unwrap();

        let entry = options.context_source_exclusions[0].clone();
        let encoded = serde_json::to_string(&entry).unwrap();
        assert!(encoded.contains("\"reason\":\"node-incarnation-unknown\""));
        let redecoded_entry: ContextSourceExclusionEntryV1 = serde_json::from_str(&encoded).unwrap();
        assert_eq!(redecoded_entry, entry);

        let mut oversized = options.clone();
        oversized.context_source_exclusions =
            vec![entry.clone(); HARNESS_CONTEXT_SOURCE_EXCLUSIONS_MAX + 1];
        assert!(matches!(
            oversized.validate(),
            Err(HarnessOperatorApiError::InvalidTaskLaunchOptions),
        ));

        let mut duplicated = options.clone();
        duplicated.context_source_exclusions.push(entry);
        assert!(matches!(
            duplicated.validate(),
            Err(HarnessOperatorApiError::InvalidTaskLaunchOptions),
        ), "context_source_exclusions must stay sorted and duplicate-free by run_id");
    }

    /// Every [`ContextSourceExclusionV1`] variant round-trips through JSON
    /// exactly and validates -- covers every field-carrying shape the
    /// enum's `#[serde(tag = "reason")]` produces, not just the one sample
    /// `task_launch_options()` carries above.
    #[test]
    fn context_source_exclusion_variants_round_trip_every_shape() {
        let samples = [
            ContextSourceExclusionV1::LifecycleNotLive {
                lifecycle: HarnessRunLifecycleV1::Cancelled,
            },
            ContextSourceExclusionV1::NoBinding,
            ContextSourceExclusionV1::NoActiveManagedSession,
            ContextSourceExclusionV1::NodeIncarnationUnknown {
                node_id: HarnessSelectorV1::new("node-a").unwrap(),
                node_incarnation: HarnessSelectorV1::new("07".repeat(16)).unwrap(),
                known_incarnations: vec![HarnessSelectorV1::new("08".repeat(16)).unwrap()],
            },
            ContextSourceExclusionV1::ManagedSessionRecordMismatch {
                record_id: HarnessSelectorV1::new("record-a").unwrap(),
                workspace_id: HarnessSelectorV1::new("workspace-a").unwrap(),
                instance_id: 7,
                generation: 3,
                node_has_records: 4,
            },
            ContextSourceExclusionV1::ManagedSessionsPageTruncated {
                record_id: HarnessSelectorV1::new("record-a").unwrap(),
                page_len: 128,
                total: 185,
            },
            ContextSourceExclusionV1::MonitorUnavailable,
            ContextSourceExclusionV1::ProjectionNotLive {
                availability: ProjectionAvailabilityV1::Partial,
                freshness: ProjectionFreshnessV1::Stale,
                transport_incomplete: true,
            },
            ContextSourceExclusionV1::DurableNodeUnknown {
                node_id: HarnessSelectorV1::new("node-a").unwrap(),
            },
        ];
        for exclusion in samples {
            exclusion.validate().unwrap();
            let encoded = serde_json::to_string(&exclusion).unwrap();
            let decoded: ContextSourceExclusionV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, exclusion);
        }

        let oversized = ContextSourceExclusionV1::NodeIncarnationUnknown {
            node_id: HarnessSelectorV1::new("node-a").unwrap(),
            node_incarnation: HarnessSelectorV1::new("07".repeat(16)).unwrap(),
            known_incarnations: vec![
                HarnessSelectorV1::new("08".repeat(16)).unwrap();
                CONTEXT_SOURCE_EXCLUSION_KNOWN_INCARNATIONS_MAX + 1
            ],
        };
        assert!(matches!(
            oversized.validate(),
            Err(HarnessOperatorApiError::InvalidTaskLaunchOptions),
        ));
    }

    #[test]
    fn operator_execution_requests_are_exact() {
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        let authority = HarnessOperatorAuthorityV1 {
            operation_id: HarnessOperationId::new(format!("hop_{}", "4".repeat(24))).unwrap(),
            idempotency_ref: HarnessIdempotencyRef::new(format!(
                "hidem_{}",
                "5".repeat(24),
            )).unwrap(),
            actor_id: HarnessSelectorV1::new("operator").unwrap(),
            now_unix_ms: 40,
        };
        let task_id = HarnessTaskId::new(format!("htask_{}", "b".repeat(24))).unwrap();
        let requests = [
            HarnessOperatorRequestV1::LaunchPlansList {
                after_plan_id: Some(HarnessSelectorV1::new("plan-a").unwrap()),
                limit: HARNESS_LAUNCH_PLAN_PAGE_LIMIT_MAX,
            },
            HarnessOperatorRequestV1::TaskExecutionSpecGet {
                task_id: task_id.clone(),
            },
            HarnessOperatorRequestV1::ReplaceTaskExecutionSpec {
                request: HarnessReplaceTaskExecutionSpecRequestV1 {
                    authority: authority.clone(),
                    task_id: task_id.clone(),
                    expected_task_revision: HarnessRevision::new(3).unwrap(),
                    expected_execution_spec_revision:
                        HarnessExpectedExecutionSpecRevisionV1::Absent,
                    spec: HarnessTaskExecutionSpecInputV1 {
                        scheduled_launch: ordinary_launch_plan("plan-a", 'c').scheduled_launch,
                        review_policy: HarnessTaskReviewPolicyV1::OperatorReview,
                    },
                },
            },
            HarnessOperatorRequestV1::StartTask {
                request: HarnessStartTaskRequestV1 {
                    authority,
                    task_id,
                    expected_task_revision: HarnessRevision::new(3).unwrap(),
                    expected_execution_spec_revision: HarnessRevision::new(1).unwrap(),
                    expected_scheduled_launch_digest: HarnessRequestDigest::new(
                        "d".repeat(64),
                    ).unwrap(),
                },
            },
        ];
        for request in requests {
            assert!(request.validate().is_ok());
            assert!(HarnessOperatorEnvelopeV1 {
                build_stamp: BUILD_STAMP.to_string(),
                credential: credential.clone(),
                request,
            }.validate().is_ok());
        }

        assert!(matches!(
            HarnessOperatorRequestV1::LaunchPlansList {
                after_plan_id: None,
                limit: HARNESS_LAUNCH_PLAN_PAGE_LIMIT_MAX + 1,
            }.validate(),
            Err(HarnessOperatorApiError::InvalidLimit),
        ));
    }

    #[test]
    fn launch_plan_page_is_bounded_canonical_and_redacted() {
        let first = ordinary_launch_plan("plan-a", 'a');
        let second = ordinary_launch_plan("plan-b", 'b');
        let page = HarnessLaunchPlanPageV1 {
            plans: vec![first.clone(), second.clone()],
            next_plan_id: Some(second.scheduled_launch.plan.plan_id.clone()),
        };
        page.validate().unwrap();
        let encoded = serde_json::to_string(&page).unwrap();
        for sentinel in [
            "delivery",
            "continuation",
            "harness_mcp",
            "spawn_spec",
            "environment",
            "provider_home",
            "credential",
            "raw_path",
        ] {
            assert!(!encoded.contains(sentinel));
        }
        assert_eq!(serde_json::from_str::<HarnessLaunchPlanPageV1>(&encoded).unwrap(), page);

        assert!(matches!(
            HarnessLaunchPlanPageV1 {
                plans: vec![second, first],
                next_plan_id: None,
            }.validate(),
            Err(HarnessOperatorApiError::InvalidLaunchPlans),
        ));
    }

    #[test]
    fn execution_spec_and_start_intents_are_authority_free() {
        let intent = HarnessOperatorIntentV1 {
            request_ref: HarnessOperatorRequestRefV1::new(format!(
                "hireq_{}",
                "1".repeat(24),
            )).unwrap(),
            submitted_at_unix_ms: 50,
            action: HarnessOperatorActionV1::StartTask {
                task_id: HarnessTaskId::new(format!("htask_{}", "2".repeat(24))).unwrap(),
                expected_task_revision: HarnessRevision::new(7).unwrap(),
                expected_execution_spec_revision: HarnessRevision::new(3).unwrap(),
                expected_scheduled_launch_digest: HarnessRequestDigest::new(
                    "c".repeat(64),
                ).unwrap(),
            },
        };
        intent.validate().unwrap();
        let request = HarnessOperatorRequestV1::SubmitIntent { intent };
        let encoded = serde_json::to_string(&request).unwrap();
        assert!(!encoded.contains("authority"));
        assert!(!encoded.contains("operation_id"));
        assert!(!encoded.contains("idempotency_ref"));

        let replace = HarnessOperatorRequestV1::SubmitIntent {
            intent: HarnessOperatorIntentV1 {
                request_ref: HarnessOperatorRequestRefV1::new(format!(
                    "hireq_{}",
                    "3".repeat(24),
                )).unwrap(),
                submitted_at_unix_ms: 51,
                action: HarnessOperatorActionV1::ReplaceTaskExecutionSpec {
                    task_id: HarnessTaskId::new(format!(
                        "htask_{}",
                        "4".repeat(24),
                    )).unwrap(),
                    expected_task_revision: HarnessRevision::new(2).unwrap(),
                    expected_execution_spec_revision:
                        HarnessExpectedExecutionSpecRevisionV1::Absent,
                    spec: HarnessTaskExecutionSpecInputV1 {
                        scheduled_launch: ordinary_launch_plan("plan-a", 'd').scheduled_launch,
                        review_policy: HarnessTaskReviewPolicyV1::OperatorReview,
                    },
                },
            },
        };
        replace.validate().unwrap();
    }

    #[test]
    fn run_correlation_round_trip_preserves_atomic_identity_and_redacts() {
        let correlation = managed_run_correlation();
        correlation.validate().unwrap();
        let response = HarnessOperatorResponseV1::RunCorrelation(correlation.clone());
        response.validate().unwrap();
        let encoded = serde_json::to_vec(&response).unwrap();
        let decoded: HarnessOperatorResponseV1 = serde_json::from_slice(&encoded).unwrap();
        assert_eq!(decoded, response);
        let text = String::from_utf8(encoded.clone()).unwrap();
        for forbidden in [
            "g4aho_",
            "credential",
            "provider_identity",
            "session_id",
            "spawn_spec",
            "environment",
            "C:\\\\",
        ] {
            assert!(!text.contains(forbidden));
        }
        assert!(text.contains("provider_profile"));
        assert!(text.contains("node_incarnation_id"));
        assert!(text.contains("active_session"));

        let mut without_generation: serde_json::Value =
            serde_json::from_slice(&encoded).unwrap();
        without_generation["value"]["session"]["value"]["active_session"]
            .as_object_mut().unwrap().remove("generation");
        assert!(serde_json::from_value::<HarnessOperatorResponseV1>(
            without_generation,
        ).is_err());
    }

    #[test]
    fn run_correlation_rejects_inconsistent_availability_and_unbounded_fields() {
        let mut correlation = managed_run_correlation();
        correlation.availability = HarnessRunCorrelationAvailabilityV1::NotObserved;
        assert!(matches!(
            correlation.validate(),
            Err(HarnessOperatorApiError::InvalidRunCorrelation),
        ));
        correlation.observed_at_unix_ms = None;
        correlation.validate().unwrap();

        correlation.availability = HarnessRunCorrelationAvailabilityV1::Dormant;
        correlation.observed_at_unix_ms = Some(101);
        assert!(matches!(
            correlation.validate(),
            Err(HarnessOperatorApiError::InvalidRunCorrelation),
        ));

        let unknown = r#"{
            "run_id":"hrun_aaaaaaaaaaaaaaaaaaaaaaaa",
            "run_revision":1,
            "task_id":"htask_bbbbbbbbbbbbbbbbbbbbbbbb",
            "node_id":"node-a",
            "node_incarnation_id":"07070707070707070707070707070707",
            "workspace_id":"workspace-a",
            "provider_profile":"codex-default",
            "mode":"inline",
            "worktree":{"kind":"existing"},
            "session":{"kind":"inline","value":{"inline_ref":"hinline_cccccccccccccccccccccccc"}},
            "availability":"not-observed",
            "observed_at_unix_ms":null,
            "raw_path":"C:\\\\private"
        }"#;
        assert!(serde_json::from_str::<HarnessRunCorrelationV1>(unknown).is_err());
        assert!(HarnessNodeIncarnationV1::new("x".repeat(129)).is_err());
        assert!(HarnessNodeIncarnationV1::new("0".repeat(31)).is_err());
        assert!(HarnessNodeIncarnationV1::new("A".repeat(32)).is_err());
    }

    #[test]
    fn runtime_inventory_request_validates() {
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        let request = HarnessOperatorRequestV1::RuntimeInventoryList {
            after_node_id: None,
            limit: HARNESS_RUNTIME_INVENTORY_PAGE_LIMIT_MAX,
        };
        assert!(HarnessOperatorEnvelopeV1 {
            build_stamp: BUILD_STAMP.to_string(),
            credential,
            request,
        }.validate().is_ok());
    }

    /// Plan item 4 (`gate4agent-blocked-action-event-2026-09-02.md` §2): the
    /// fleet-wide `blocked_count`/`last_blocked_at_ms` instrument (added
    /// under the old hand-bumped wire-pin scheme; the envelope now checks
    /// `BUILD_STAMP` for exact equality instead, so this field's own arrival
    /// is no longer asserted by a version number here) round-trips through
    /// JSON intact, and its own invariant (a count of zero implies no
    /// timestamp, and vice versa) is enforced the same way every other
    /// paired count/timestamp field in this crate is.
    #[test]
    fn managed_session_blocked_stats_round_trip() {
        let mut session = sample_managed_session("record-a");
        session.blocked_count = 2;
        session.last_blocked_at_ms = Some(2_000);
        session.validate().expect("blocked_count with a timestamp is valid");
        let encoded = serde_json::to_string(&session).unwrap();
        let decoded: HarnessRuntimeManagedSessionV1 = serde_json::from_str(&encoded).unwrap();
        assert_eq!(decoded, session);

        let mut zero_with_timestamp = sample_managed_session("record-a");
        zero_with_timestamp.last_blocked_at_ms = Some(1);
        assert!(zero_with_timestamp.validate().is_err());

        let mut counted_without_timestamp = sample_managed_session("record-a");
        counted_without_timestamp.blocked_count = 1;
        assert!(counted_without_timestamp.validate().is_err());
    }

    fn sample_runtime_inventory(
        managed_sessions: Vec<HarnessRuntimeManagedSessionV1>,
    ) -> HarnessRuntimeInventoryV1 {
        HarnessRuntimeInventoryV1 {
            enabled_providers: Vec::new(),
            workspaces: BTreeMap::new(),
            workspace_count: 0,
            workspaces_truncated: false,
            session_count: 0,
            sessions_truncated: false,
            managed_session_count: managed_sessions.len(),
            managed_sessions_truncated: false,
            managed_sessions,
            retired_count: 0,
            launch_inventory: None,

        machine: None,}
    }

    /// Regression for `ed4fa61`: the producer (`SlimNodeInventory::
    /// from_snapshot`/`from_c2_snapshot`, c2-protocol) orders a managed-
    /// session page by `managed_session_liveness_rank` (Live, IdentityPending,
    /// Dormant, Unavailable) first and `record_id` only within a rank, so a
    /// real page's `record_id`s are routinely NOT ascending overall. Before
    /// this fix `validate` demanded flat ascending `record_id` order and
    /// rejected every such page outright.
    #[test]
    fn managed_session_ordering_is_liveness_rank_then_record_id() {
        let mut live = sample_managed_session("zzz-live");
        live.state = HarnessRuntimeManagedStateV1::Live;
        let mut pending = sample_managed_session("aaa-pending");
        pending.state = HarnessRuntimeManagedStateV1::IdentityPending;
        let mut unavailable = sample_managed_session("mmm-unavailable");
        unavailable.state = HarnessRuntimeManagedStateV1::Unavailable;

        let ordered = sample_runtime_inventory(vec![
            live.clone(),
            pending,
            unavailable,
        ]);
        ordered.validate().expect(
            "liveness-rank-then-record_id order validates even though record_id is not ascending overall",
        );

        let duplicate = sample_runtime_inventory(vec![live.clone(), live.clone()]);
        assert!(duplicate.validate().is_err());

        let mut dormant = sample_managed_session("aaa-dormant");
        dormant.state = HarnessRuntimeManagedStateV1::Dormant;
        let rank_out_of_order = sample_runtime_inventory(vec![dormant, live]);
        assert!(rank_out_of_order.validate().is_err());
    }

    /// Slice R (`hatchery-node`'s session-record retention sweep):
    /// `retired_count` is additive, defaults to `0` for a pre-existing
    /// payload that never had it, and round-trips intact once a producer
    /// sets a real, nonzero value.
    #[test]
    fn runtime_inventory_retired_count_round_trips_and_defaults_to_zero() {
        let mut inventory = sample_runtime_inventory(Vec::new());
        assert_eq!(inventory.retired_count, 0);
        inventory.validate().expect("a fresh sample inventory is valid");

        inventory.retired_count = 5;
        let encoded = serde_json::to_string(&inventory).unwrap();
        assert!(encoded.contains("\"retired_count\":5"));
        let decoded: HarnessRuntimeInventoryV1 = serde_json::from_str(&encoded).unwrap();
        assert_eq!(decoded, inventory);
        decoded.validate().expect("a nonzero retired_count is still a valid inventory");
    }

    #[test]
    fn runtime_inventory_launch_inventory_round_trips_and_fails_closed() {
        let node = HarnessRuntimeNodeInventoryV1 {
            node_id: "node-a".to_owned(),
            incarnation_id: "07".repeat(16),
            observed_at_unix_ms: 1_000,
            event_sequence: 4,
            inventory: HarnessRuntimeInventoryV1 {
                enabled_providers: vec!["claude".to_owned()],
                workspaces: BTreeMap::new(),
                workspace_count: 0,
                workspaces_truncated: false,
                session_count: 0,
                sessions_truncated: false,
                managed_sessions: Vec::new(),
                managed_session_count: 0,
                managed_sessions_truncated: false,
                retired_count: 0,
                launch_inventory: Some(HarnessRuntimeLaunchInventoryV1 {
                    spawn_profiles: Some(vec![HarnessRuntimeSpawnProfileSummaryV1 {
                        id: "default".to_owned(),
                        revision: "rev-1".to_owned(),
                        environment_profile: Some(HarnessRuntimeEnvironmentProfileReceiptV1 {
                            profile_id: "env-a".to_owned(),
                            profile_revision: "env-rev-1".to_owned(),

                        machine: None,}),
                    }]),
                    bundles: Some(vec![HarnessRuntimeBundleReceiptV1 {
                        id: "bundle-a".to_owned(),
                        revision: "bundle-rev-1".to_owned(),
                        digest: format!("sha256:{}", "a".repeat(64)),
                    }]),
                }),
            },
        };
        node.validate().expect("valid launch inventory");
        let encoded = serde_json::to_string(&node).unwrap();
        let decoded: HarnessRuntimeNodeInventoryV1 = serde_json::from_str(&encoded).unwrap();
        assert_eq!(decoded, node);

        let mut missing_component = node.clone();
        missing_component.inventory.launch_inventory = Some(HarnessRuntimeLaunchInventoryV1 {
            spawn_profiles: None,
            bundles: None,
        });
        assert!(missing_component.validate().is_err());

        let mut duplicate_profile = node.clone();
        let profile = duplicate_profile.inventory.launch_inventory.as_ref().unwrap()
            .spawn_profiles.as_ref().unwrap()[0].clone();
        duplicate_profile.inventory.launch_inventory.as_mut().unwrap()
            .spawn_profiles.as_mut().unwrap().push(profile);
        assert!(duplicate_profile.validate().is_err());

        let mut bad_digest = node.clone();
        bad_digest.inventory.launch_inventory.as_mut().unwrap()
            .bundles.as_mut().unwrap()[0].digest = "not-a-digest".to_owned();
        assert!(bad_digest.validate().is_err());

        let mut no_launch_inventory = node;
        no_launch_inventory.inventory.launch_inventory = None;
        no_launch_inventory.validate().expect("absent launch inventory stays valid");
    }

    #[test]
    fn native_history_is_exact_bounded_and_excludes_sensitive_fields() {
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        let route = HarnessNativeSessionRouteV1 {
            node_id: "node-a".to_owned(),
            incarnation_id: "1".repeat(32),
            scope: HarnessNativeSessionCatalogScopeV1::Workspace,
            workspace_id: Some("workspace-a".to_owned()),
            provider: "codex".to_owned(),
        };
        let selection = HarnessNativeSessionSelectionV1 {
            route: route.clone(),
            catalog_revision: 7,
            recent_cutoff_unix_ms: 9,
            selection_id: "selection-a".to_owned(),
        };
        for request in [
            HarnessOperatorRequestV1::CatalogNativeSessions {
                route: route.clone(),
                limit: HARNESS_NATIVE_SESSION_CATALOG_LIMIT_MAX,
            },
            HarnessOperatorRequestV1::PageNativeSessions {
                route: route.clone(),
                window: HarnessNativeSessionCatalogWindowV1::Recent,
                catalog_revision: 7,
                recent_cutoff_unix_ms: 9,
                after_selection_id: Some("selection-a".to_owned()),
                limit: 1,
            },
            HarnessOperatorRequestV1::PreviewNativeSession {
                selection: selection.clone(),
                message_limit: HARNESS_NATIVE_SESSION_PREVIEW_MESSAGE_LIMIT_MAX,
            },
        ] {
            HarnessOperatorEnvelopeV1 {
                build_stamp: BUILD_STAMP.to_string(),
                credential: credential.clone(),
                request,
            }.validate().unwrap();
        }
        let response = HarnessOperatorResponseV1::NativeSessionPreviewed(
            HarnessNativeSessionPreviewedV1 {
                selection,
                preview: HarnessNativeSessionPreviewV1 {
                    title: Some("Bounded preview".to_owned()),
                    modified_at_unix_ms: Some(10),
                    model: Some("model-a".to_owned()),
                    message_count: 1,
                    message_count_exact: true,
                    completed_turn_count: Some(1),
                    total_tokens: Some(12),
                    truncated: false,
                    messages: vec![HarnessNativeSessionPreviewMessageV1 {
                        role: HarnessNativeSessionPreviewRoleV1::User,
                        text: "bounded history preview".to_owned(),
                    }],
                },
            },
        );
        response.validate().unwrap();
        let encoded = serde_json::to_string(&response).unwrap();
        for forbidden in [
            "g4aho_",
            "credential",
            "provider_identity",
            "session_id",
            "terminal",
            "cwd",
        ] {
            assert!(!encoded.contains(forbidden));
        }
    }

    #[test]
    fn unknown_request_fields_and_unbounded_limits_fail_closed() {
        let unknown = r#"{"kind":"tasks-list","limit":10,"cursor":"secret"}"#;
        assert!(serde_json::from_str::<HarnessReadRequestV1>(unknown).is_err());
        assert!(HarnessReadRequestV1::TasksList {
            after_task_id: None, state: None, parent_task_id: None, limit: 0,
        }.validate().is_err());
        assert!(HarnessReadRequestV1::TasksList {
            after_task_id: None, state: None, parent_task_id: None, limit: 65,
        }.validate().is_err());
    }

    #[test]
    fn redacted_cross_entity_references_do_not_require_hidden_ids() {
        let redacted_run = r#"{
            "run_id":"hrun_000000000000000000000001",
            "revision":1,
            "parent_run_id":null,
            "task_id":null,
            "operation_id":null,
            "intent":{"mode":"inline","worktree":"existing","has_delivery_bundle":false,"has_continuation":false},
            "lifecycle":"requested",
            "binding":"none",
            "result_disposition":null,
            "failure_category":null,
            "delivery_receipt":null,
            "references_redacted":true,
            "created_at_unix_ms":1,
            "updated_at_unix_ms":1
        }"#;
        let run: RedactedRunV1 = serde_json::from_str(redacted_run).expect("redacted run");
        run.validate().expect("valid redacted run");

        let operation_with_grant = r#"{
            "operation_id":"hop_000000000000000000000001",
            "revision":1,
            "kind":"create-task",
            "state":"prepared",
            "task_id":null,
            "run_id":null,
            "grant_id":"hgrant_000000000000000000000001",
            "reconciles_operation_id":null,
            "references_redacted":true,
            "failure_category":null,
            "outcome_unknown_reason":null,
            "reconciliation_outcome":null,
            "created_at_unix_ms":1,
            "updated_at_unix_ms":1,
            "dispatched_at_unix_ms":null,
            "finished_at_unix_ms":null
        }"#;
        assert!(serde_json::from_str::<RedactedOperationV1>(operation_with_grant).is_err());
    }

    #[test]
    fn operator_run_workspace_requests_are_exact_harness_only_round_trips() {
        let run_id = run_workspace_origin().run_id;
        let path = HarnessRepositoryPathV1::new("src/lib.rs").unwrap();
        let object_id = HarnessGitObjectIdV1::new("a".repeat(40)).unwrap();
        let requests = vec![
            HarnessOperatorRequestV1::InspectRunWorkspace {
                run_id: run_id.clone(),
            },
            HarnessOperatorRequestV1::ReadRunWorkspaceFile {
                run_id: run_id.clone(),
                path: path.clone(),
            },
            HarnessOperatorRequestV1::ReadRunGitHistory {
                run_id: run_id.clone(),
                path: Some(path.clone()),
                before: Some(object_id.clone()),
                limit: HARNESS_GIT_HISTORY_LIMIT_MAX,
            },
            HarnessOperatorRequestV1::ReadRunGitDiff {
                run_id,
                mode: HarnessGitDiffModeV1::Commit { revision: object_id },
                path: Some(path),
            },
        ];
        for request in requests {
            request.validate().expect("valid workspace request");
            let encoded = serde_json::to_string(&request).unwrap();
            for forbidden in [
                "node_id",
                "workspace_id",
                "endpoint",
                "root",
                "workspace_root",
                "worktree",
                "worktree_path",
                "environment",
                "spawn_spec",
                "provider_home",
            ] {
                assert!(!encoded.contains(forbidden), "request exposed {forbidden}");
            }
            let decoded: HarnessOperatorRequestV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, request);
        }
    }

    #[test]
    fn operator_v4_workspace_replies_round_trip_without_host_or_diagnostic_fields() {
        let origin = run_workspace_origin();
        let path = HarnessRepositoryPathV1::new("src/lib.rs").unwrap();
        let commit = git_commit('a');
        let responses = vec![
            HarnessOperatorResponseV1::RunWorkspaceInspected(
                HarnessRunWorkspaceInspectionV1 {
                    origin: origin.clone(),
                    entries: vec![HarnessWorkspaceTreeEntryV1 {
                        relative_path: path.clone(),
                        kind: HarnessWorkspaceEntryKindV1::File,
                    }],
                    tree_truncated: false,
                    git: HarnessGitSummaryV1 {
                        is_repository: true,
                        branch: Some("main".to_owned()),
                        status: vec![HarnessGitStatusEntryV1 {
                            index_status: HarnessGitStatusCodeV1::Unmodified,
                            worktree_status: HarnessGitStatusCodeV1::Modified,
                            path: path.clone(),
                            previous_path: None,
                        }],
                        recent_commits: vec![HarnessGitCommitSummaryV1 {
                            id: commit.id.clone(),
                            summary: commit.subject.clone(),
                        }],
                        truncated: false,
                    },
                    truncation: None,
                },
            ),
            HarnessOperatorResponseV1::RunWorkspaceFileRead(HarnessRunWorkspaceFileV1 {
                origin: origin.clone(),
                path: path.clone(),
                content: HarnessWorkspaceFileContentV1::Utf8 {
                    text: "fn main() {}\n".to_owned(),
                    byte_len: 13,
                },
                revision: Some(HarnessWorkspaceFileRevisionV1::new("b".repeat(64)).unwrap()),
            }),
            HarnessOperatorResponseV1::RunGitHistoryRead(HarnessRunGitHistoryPageV1 {
                origin: origin.clone(),
                path: Some(path.clone()),
                commits: vec![commit],
                next_before: None,
                truncated: false,
            }),
            HarnessOperatorResponseV1::RunGitDiffRead(HarnessRunGitDiffV1 {
                origin,
                mode: HarnessGitDiffModeV1::Working,
                path: Some(path),
                text: "diff --git a/src/lib.rs b/src/lib.rs\n".to_owned(),
                truncated: false,
            }),
        ];
        for response in responses {
            response.validate().expect("valid V4 workspace response");
            let encoded = serde_json::to_string(&response).unwrap();
            assert_json_has_no_forbidden_keys(&encoded, &[
                "root",
                "worktree",
                "endpoint",
                "diagnostic",
                "author_email",
                "committer_email",
                "environment",
                "spawn_spec",
                "provider_home",
            ]);
            let decoded: HarnessOperatorResponseV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, response);
        }
    }

    #[test]
    fn operator_v4_repository_paths_and_git_cursors_fail_closed() {
        for value in [
            "",
            ".",
            "..",
            "../secret",
            "src/../secret",
            "src//lib.rs",
            "/absolute",
            "C:/absolute",
            r"src\lib.rs",
            "src/\nlib.rs",
        ] {
            assert!(HarnessRepositoryPathV1::new(value).is_err(), "accepted {value:?}");
        }
        assert!(HarnessRepositoryPathV1::new("x".repeat(HARNESS_REPOSITORY_PATH_MAX_BYTES)).is_ok());
        assert!(HarnessRepositoryPathV1::new("x".repeat(HARNESS_REPOSITORY_PATH_MAX_BYTES + 1)).is_err());
        assert!(HarnessGitObjectIdV1::new("a".repeat(40)).is_ok());
        assert!(HarnessGitObjectIdV1::new("a".repeat(64)).is_ok());
        assert!(HarnessGitObjectIdV1::new("A".repeat(40)).is_err());
        assert!(HarnessGitObjectIdV1::new("a".repeat(39)).is_err());

        let unknown_route = format!(
            r#"{{"kind":"read-run-workspace-file","run_id":"hrun_{}","path":"src/lib.rs","workspace_id":"forbidden"}}"#,
            "a".repeat(24),
        );
        assert!(serde_json::from_str::<HarnessOperatorRequestV1>(&unknown_route).is_err());
    }

    #[test]
    fn operator_v4_workspace_payloads_enforce_exact_bounds() {
        let origin = run_workspace_origin();
        let path = HarnessRepositoryPathV1::new("src/lib.rs").unwrap();
        let at_file_limit = HarnessWorkspaceFileContentV1::Utf8 {
            text: "x".repeat(HARNESS_WORKSPACE_FILE_MAX_BYTES),
            byte_len: HARNESS_WORKSPACE_FILE_MAX_BYTES as u32,
        };
        assert!(at_file_limit.validate().is_ok());
        assert!(HarnessWorkspaceFileContentV1::Utf8 {
            text: "x".repeat(HARNESS_WORKSPACE_FILE_MAX_BYTES + 1),
            byte_len: (HARNESS_WORKSPACE_FILE_MAX_BYTES + 1) as u32,
        }.validate().is_err());
        assert!(HarnessRunGitDiffV1 {
            origin: origin.clone(),
            mode: HarnessGitDiffModeV1::Working,
            path: Some(path.clone()),
            text: "x".repeat(HARNESS_GIT_DIFF_MAX_BYTES),
            truncated: false,
        }.validate().is_ok());
        assert!(HarnessRunGitDiffV1 {
            origin: origin.clone(),
            mode: HarnessGitDiffModeV1::Working,
            path: Some(path.clone()),
            text: "x".repeat(HARNESS_GIT_DIFF_MAX_BYTES + 1),
            truncated: true,
        }.validate().is_err());
        assert!(HarnessOperatorRequestV1::ReadRunGitHistory {
            run_id: origin.run_id.clone(),
            path: Some(path.clone()),
            before: None,
            limit: HARNESS_GIT_HISTORY_LIMIT_MAX + 1,
        }.validate().is_err());

        let entries: Vec<_> = (0..HARNESS_WORKSPACE_TREE_ENTRIES_MAX)
            .map(|index| HarnessWorkspaceTreeEntryV1 {
                relative_path: HarnessRepositoryPathV1::new(format!("entry-{index:04}")).unwrap(),
                kind: HarnessWorkspaceEntryKindV1::File,
            })
            .collect();
        let empty_git = HarnessGitSummaryV1 {
            is_repository: false,
            branch: None,
            status: Vec::new(),
            recent_commits: Vec::new(),
            truncated: false,
        };
        assert!(HarnessRunWorkspaceInspectionV1 {
            origin: origin.clone(),
            entries: entries.clone(),
            tree_truncated: false,
            git: empty_git.clone(),
            truncation: None,
        }.validate().is_ok());
        let mut too_many_entries = entries;
        too_many_entries.push(HarnessWorkspaceTreeEntryV1 {
            relative_path: HarnessRepositoryPathV1::new("entry-0512").unwrap(),
            kind: HarnessWorkspaceEntryKindV1::File,
        });
        assert!(HarnessRunWorkspaceInspectionV1 {
            origin: origin.clone(),
            entries: too_many_entries,
            tree_truncated: true,
            git: empty_git,
            truncation: None,
        }.validate().is_err());

        let status: Vec<_> = (0..HARNESS_GIT_STATUS_ENTRIES_MAX)
            .map(|index| HarnessGitStatusEntryV1 {
                index_status: HarnessGitStatusCodeV1::Unmodified,
                worktree_status: HarnessGitStatusCodeV1::Modified,
                path: HarnessRepositoryPathV1::new(format!("status-{index:04}")).unwrap(),
                previous_path: None,
            })
            .collect();
        let recent_commits: Vec<_> = (0..HARNESS_GIT_RECENT_COMMITS_MAX)
            .map(|index| HarnessGitCommitSummaryV1 {
                id: HarnessGitObjectIdV1::new(format!("{index:040x}")).unwrap(),
                summary: format!("commit {index}"),
            })
            .collect();
        assert!(HarnessGitSummaryV1 {
            is_repository: true,
            branch: Some("main".to_owned()),
            status: status.clone(),
            recent_commits: recent_commits.clone(),
            truncated: false,
        }.validate().is_ok());
        let mut too_many_status = status;
        too_many_status.push(HarnessGitStatusEntryV1 {
            index_status: HarnessGitStatusCodeV1::Unmodified,
            worktree_status: HarnessGitStatusCodeV1::Modified,
            path: HarnessRepositoryPathV1::new("status-0128").unwrap(),
            previous_path: None,
        });
        assert!(HarnessGitSummaryV1 {
            is_repository: true,
            branch: Some("main".to_owned()),
            status: too_many_status,
            recent_commits: Vec::new(),
            truncated: true,
        }.validate().is_err());
        let mut too_many_recent = recent_commits;
        too_many_recent.push(HarnessGitCommitSummaryV1 {
            id: HarnessGitObjectIdV1::new(format!("{:040x}", HARNESS_GIT_RECENT_COMMITS_MAX)).unwrap(),
            summary: "one too many".to_owned(),
        });
        assert!(HarnessGitSummaryV1 {
            is_repository: true,
            branch: Some("main".to_owned()),
            status: Vec::new(),
            recent_commits: too_many_recent,
            truncated: true,
        }.validate().is_err());

        let commits: Vec<_> = (0..HARNESS_GIT_HISTORY_LIMIT_MAX)
            .map(|index| {
                let mut commit = git_commit('a');
                commit.id = HarnessGitObjectIdV1::new(format!("{index:040x}")).unwrap();
                commit
            })
            .collect();
        assert!(HarnessRunGitHistoryPageV1 {
            origin: origin.clone(),
            path: Some(path.clone()),
            commits: commits.clone(),
            next_before: None,
            truncated: false,
        }.validate().is_ok());
        let next_before = commits.last().map(|commit| commit.id.clone());
        assert!(HarnessRunGitHistoryPageV1 {
            origin: origin.clone(),
            path: Some(path.clone()),
            commits: commits.clone(),
            next_before: next_before.clone(),
            truncated: false,
        }.validate().is_ok());
        assert!(HarnessRunGitHistoryPageV1 {
            origin: origin.clone(),
            path: Some(path.clone()),
            commits: commits.clone(),
            next_before: None,
            truncated: true,
        }.validate().is_ok());
        assert!(HarnessRunGitHistoryPageV1 {
            origin: origin.clone(),
            path: Some(path.clone()),
            commits: commits.clone(),
            next_before,
            truncated: true,
        }.validate().is_ok());
        let mut too_many_commits = commits;
        let mut extra_commit = git_commit('a');
        extra_commit.id = HarnessGitObjectIdV1::new(format!("{:040x}", HARNESS_GIT_HISTORY_LIMIT_MAX)).unwrap();
        too_many_commits.push(extra_commit);
        assert!(HarnessRunGitHistoryPageV1 {
            origin,
            path: Some(path),
            commits: too_many_commits,
            next_before: None,
            truncated: false,
        }.validate().is_err());
    }

    fn monitor_with_mixed_capabilities() -> SessionMonitorV1 {
        SessionMonitorV1 {
            run_id: HarnessRunId::new("hrun_000000000000000000000001").unwrap(),
            visibility: HarnessMonitoringVisibilityV1::Detail,
            availability: ProjectionAvailabilityV1::Frozen,
            freshness: ProjectionFreshnessV1::ReplacedIncarnation,
            transport_incomplete: false,
            features: MonitorFeatureStatesV1 {
                todo: FeatureObservationStateV1::Observed,
                tools: FeatureObservationStateV1::NotSupportedByObservedSources,
                subagents: FeatureObservationStateV1::Unknown,
                interactions: FeatureObservationStateV1::SupportedNotObserved,
                owned_processes: FeatureObservationStateV1::Observed,
                files: FeatureObservationStateV1::Observed,
                usage: FeatureObservationStateV1::SupportedNotObserved,
                history: FeatureObservationStateV1::Unknown,
            },
            todo_total: 1,
            todo_completed: 0,
            active_tools: 0,
            active_subagents: 0,
            active_interactions: 0,
            active_blocks: 0,
            active_processes: 0,
            input_tokens: 0,
            output_tokens: 0,
            cache_read_tokens: 0,
            cache_write_tokens: 0,
            reasoning_tokens: 0,
            context_window_tokens: None,
            history: None,
            detail: Some(SessionMonitorDetailV1 {
                todo_facts: vec![TodoFactV1 {
                    state: TodoStateV1::Unknown,
                    todo_id: Some("todo-1".to_owned()),
                    label: Some("review result".to_owned()),
                    evidence: ObservationEvidenceV1::PtyHint,
                }],
                tool_facts: Vec::new(),
                subagent_facts: Vec::new(),
                interaction_facts: Vec::new(),
                block_facts: Vec::new(),
                process_facts: vec![ActivityFactV1 {
                    class: ActivityClassV1::OwnedProcess,
                    state: ActivityStateV1::UnknownAfterGap,
                    label: Some("compiler".to_owned()),
                    correlation: Some(1),
                    evidence: ObservationEvidenceV1::ManagedHook,
                }],
                file_facts: vec![FileFactV1 {
                    action: FileActionV1::Changed,
                    relative_path: Some("src/lib.rs".to_owned()),
                    evidence: ObservationEvidenceV1::StructuredProvider,
                }],
            }),
        }
    }

    #[test]
    fn monitor_preserves_mixed_capability_and_frozen_replaced_truth() {
        let monitor = monitor_with_mixed_capabilities();
        monitor.validate().expect("truthful mixed monitor");
        let encoded = serde_json::to_string(&monitor).expect("encode monitor");
        let decoded: SessionMonitorV1 = serde_json::from_str(&encoded).expect("decode monitor");
        assert_eq!(decoded, monitor);
        assert_eq!(decoded.availability, ProjectionAvailabilityV1::Frozen);
        assert_eq!(decoded.freshness, ProjectionFreshnessV1::ReplacedIncarnation);
        assert_eq!(decoded.features.tools, FeatureObservationStateV1::NotSupportedByObservedSources);
        let detail = decoded.detail.unwrap();
        assert_eq!(detail.todo_facts[0].state, TodoStateV1::Unknown);
        assert_eq!(detail.todo_facts[0].todo_id.as_deref(), Some("todo-1"));
        assert_eq!(detail.todo_facts[0].label.as_deref(), Some("review result"));
        assert_eq!(detail.process_facts[0].correlation, Some(1));
        assert_eq!(detail.file_facts[0].relative_path.as_deref(), Some("src/lib.rs"));
    }

    #[test]
    fn monitor_rejects_inference_and_raw_detail_sentinels() {
        let mut monitor = monitor_with_mixed_capabilities();
        monitor.detail.as_mut().unwrap().tool_facts.push(ActivityFactV1 {
            class: ActivityClassV1::Tool,
            state: ActivityStateV1::Active,
            label: Some("command".to_owned()),
            correlation: Some(1),
            evidence: ObservationEvidenceV1::StructuredProvider,
        });
        assert!(monitor.validate().is_err());

        let raw_file = r#"{"action":"changed","evidence":"managed-hook","path":"C:\\private\\prompt.txt"}"#;
        assert!(serde_json::from_str::<FileFactV1>(raw_file).is_err());
        let encoded = serde_json::to_string(&monitor_with_mixed_capabilities()).unwrap();
        for sentinel in ["private", "prompt.txt", "provider_id", "correlation_id"] {
            assert!(!encoded.contains(sentinel));
        }

        let mut partial = monitor_with_mixed_capabilities();
        partial.availability = ProjectionAvailabilityV1::Partial;
        partial.freshness = ProjectionFreshnessV1::IncompleteAfterGap;
        partial.transport_incomplete = true;
        partial.validate().expect("truthful partial gap");
    }

    #[test]
    fn timeline_structured_fields_round_trip_without_raw_correlation_shape() {
        let run_id = HarnessRunId::new("hrun_000000000000000000000001").unwrap();
        let page = TimelinePageV1 {
            run_id: run_id.clone(),
            availability: ProjectionAvailabilityV1::Current,
            freshness: ProjectionFreshnessV1::Live,
            transport_incomplete: false,
            entries: vec![TimelineEntryV1 {
                sequence: 7,
                received_at_ms: 10,
                category: TimelineCategoryV1::Tool,
                label: Some("command".to_owned()),
                state: TimelineStateV1::Completed,
                correlation: Some(1),
                evidence: ObservationEvidenceV1::StructuredProvider,
            }],
            next_cursor: None,
        };
        page.validate_for(&run_id).unwrap();
        let encoded = serde_json::to_string(&page).unwrap();
        assert!(encoded.contains("\"label\":\"command\""));
        assert!(encoded.contains("\"state\":\"completed\""));
        assert!(encoded.contains("\"correlation\":1"));
        assert!(!encoded.contains("correlation_id"));
        assert_eq!(serde_json::from_str::<TimelinePageV1>(&encoded).unwrap(), page);

        let old_entry: TimelineEntryV1 = serde_json::from_str(
            r#"{"sequence":1,"received_at_ms":2,"category":"usage","evidence":"managed-hook"}"#,
        ).unwrap();
        assert_eq!(old_entry.state, TimelineStateV1::Unknown);
        assert_eq!(old_entry.correlation, None);
        old_entry.validate().unwrap();
    }

    #[test]
    fn monitor_history_summary_matches_categorical_feature_state() {
        let mut monitor = monitor_with_mixed_capabilities();
        monitor.features.history = FeatureObservationStateV1::Observed;
        monitor.history = Some(SessionMonitorHistoryV1 {
            message_count: 12,
            message_count_exact: true,
            completed_turn_count: Some(3),
            total_tokens: Some(800),
        });
        monitor.validate().unwrap();
        let encoded = serde_json::to_string(&monitor).unwrap();
        let decoded: SessionMonitorV1 = serde_json::from_str(&encoded).unwrap();
        assert_eq!(decoded.history, monitor.history);

        monitor.history = None;
        monitor.validate().expect("redacted observed history remains valid");
        monitor.features.history = FeatureObservationStateV1::NotSupportedByObservedSources;
        monitor.history = Some(SessionMonitorHistoryV1 {
            message_count: 1,
            message_count_exact: false,
            completed_turn_count: None,
            total_tokens: None,
        });
        assert!(monitor.validate().is_err());
    }

    #[test]
    fn structured_file_fact_rejects_absolute_or_traversing_paths() {
        for relative_path in ["C:/private.txt", "/private.txt", "src/../private.txt", "src\\private.txt"] {
            let fact = FileFactV1 {
                action: FileActionV1::Changed,
                relative_path: Some(relative_path.to_owned()),
                evidence: ObservationEvidenceV1::WorkspaceObservation,
            };
            assert!(fact.validate().is_err(), "accepted {relative_path}");
        }
    }

    #[test]
    fn operator_reverse_attribution_is_exact_and_private() {
        let subject = HarnessReverseAttributionSubjectV1::FileScope {
            workspace: reverse_attribution_workspace(),
            relative_path: HarnessRepositoryPathV1::new("src/lib.rs").unwrap(),
        };
        let request = HarnessOperatorRequestV1::ReverseAttributionGet {
            subject: subject.clone(),
        };
        request.validate().unwrap();
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        HarnessOperatorEnvelopeV1 {
            build_stamp: BUILD_STAMP.to_string(),
            credential,
            request,
        }.validate().unwrap();

        let response = HarnessReverseAttributionV1 {
            subject,
            outcome: HarnessReverseAttributionOutcomeV1::Attributed,
            links: vec![reverse_attribution_link('2')],
        };
        response.validate().unwrap();
        let encoded = serde_json::to_value(
            HarnessOperatorResponseV1::ReverseAttribution(response),
        ).unwrap();
        assert_eq!(
            encoded,
            serde_json::json!({
                "kind": "reverse-attribution",
                "value": {
                    "subject": {
                        "kind": "file-scope",
                        "workspace": {
                            "node_id": "node-a",
                            "node_incarnation_id": "07070707070707070707070707070707",
                            "workspace_id": "workspace-a"
                        },
                        "relative_path": "src/lib.rs"
                    },
                    "outcome": "attributed",
                    "links": [{
                        "task_id": "htask_111111111111111111111111",
                        "run_id": "hrun_222222222222222222222222",
                        "run_revision": 3,
                        "binding": {
                            "kind": "workspace",
                            "workspace": {
                                "node_id": "node-a",
                                "node_incarnation_id": "07070707070707070707070707070707",
                                "workspace_id": "workspace-a"
                            }
                        },
                        "relation": "workspace-scope"
                    }]
                }
            }),
        );
        let encoded = encoded.to_string();
        for forbidden in [
            "produced-by",
            "modified-by",
            "provider_session",
            "provider_profile",
            "provider_home",
            "credential",
            "auth",
            "canonical_root",
            "display_root",
            "source_path",
        ] {
            assert!(!encoded.contains(forbidden), "leaked {forbidden}");
        }

        let mut unknown = serde_json::to_value(HarnessOperatorRequestV1::ReverseAttributionGet {
            subject: HarnessReverseAttributionSubjectV1::Workspace {
                workspace: reverse_attribution_workspace(),
            },
        }).unwrap();
        unknown["subject"]["provider_session_id"] = serde_json::json!("forbidden");
        assert!(serde_json::from_value::<HarnessOperatorRequestV1>(unknown).is_err());
    }

    #[test]
    fn reverse_attribution_is_bounded_canonical_and_exactly_correlated() {
        let subject = HarnessReverseAttributionSubjectV1::FileScope {
            workspace: reverse_attribution_workspace(),
            relative_path: HarnessRepositoryPathV1::new("src/lib.rs").unwrap(),
        };
        let first = reverse_attribution_link('2');
        let second = reverse_attribution_link('3');
        let exact = HarnessReverseAttributionV1 {
            subject: subject.clone(),
            outcome: HarnessReverseAttributionOutcomeV1::Attributed,
            links: vec![first.clone(), second.clone()],
        };
        exact.validate_for(&subject).unwrap();

        let mut noncanonical = exact.clone();
        noncanonical.links.reverse();
        assert!(matches!(
            noncanonical.validate(),
            Err(HarnessOperatorApiError::InvalidReverseAttribution),
        ));
        let duplicate = HarnessReverseAttributionV1 {
            links: vec![first.clone(), first],
            ..exact.clone()
        };
        assert!(duplicate.validate().is_err());
        let over_limit = HarnessReverseAttributionV1 {
            links: (0..=HARNESS_REVERSE_ATTRIBUTION_LINKS_MAX)
                .map(|index| HarnessReverseAttributionLinkV1 {
                    task_id: HarnessTaskId::new(format!("htask_{index:024x}")).unwrap(),
                    ..reverse_attribution_link('4')
                })
                .collect(),
            ..exact.clone()
        };
        assert!(over_limit.validate().is_err());

        let unattributed = HarnessReverseAttributionV1 {
            subject: subject.clone(),
            outcome: HarnessReverseAttributionOutcomeV1::Unattributed,
            links: Vec::new(),
        };
        unattributed.validate().unwrap();
        let attributed_empty = HarnessReverseAttributionV1 {
            outcome: HarnessReverseAttributionOutcomeV1::Attributed,
            ..unattributed.clone()
        };
        assert!(attributed_empty.validate().is_err());
        let unattributed_linked = HarnessReverseAttributionV1 {
            outcome: HarnessReverseAttributionOutcomeV1::Unattributed,
            links: vec![second],
            ..exact.clone()
        };
        assert!(unattributed_linked.validate().is_err());

        let other_subject = HarnessReverseAttributionSubjectV1::FileScope {
            workspace: reverse_attribution_workspace(),
            relative_path: HarnessRepositoryPathV1::new("src/other.rs").unwrap(),
        };
        assert!(matches!(
            exact.validate_for(&other_subject),
            Err(HarnessOperatorApiError::InvalidReverseAttribution),
        ));
    }

    #[test]
    fn reverse_attribution_relations_reject_false_file_and_binding_claims() {
        let file_subject = HarnessReverseAttributionSubjectV1::FileScope {
            workspace: reverse_attribution_workspace(),
            relative_path: HarnessRepositoryPathV1::new("src/lib.rs").unwrap(),
        };
        let mut wrong_relation = reverse_attribution_link('2');
        wrong_relation.relation = HarnessReverseAttributionRelationV1::WorkspaceBinding;
        assert!(HarnessReverseAttributionV1 {
            subject: file_subject,
            outcome: HarnessReverseAttributionOutcomeV1::Attributed,
            links: vec![wrong_relation],
        }.validate().is_err());

        let managed_subject = HarnessReverseAttributionSubjectV1::ManagedRecord {
            workspace: reverse_attribution_workspace(),
            record_id: HarnessSelectorV1::new("record-a").unwrap(),
        };
        let mismatched_record = HarnessReverseAttributionLinkV1 {
            task_id: HarnessTaskId::new(format!("htask_{}", "1".repeat(24))).unwrap(),
            run_id: HarnessRunId::new(format!("hrun_{}", "2".repeat(24))).unwrap(),
            run_revision: HarnessRevision::new(3).unwrap(),
            binding: HarnessReverseAttributionBindingV1::ManagedRecord {
                workspace: reverse_attribution_workspace(),
                record_id: HarnessSelectorV1::new("record-b").unwrap(),
                active_instance_id: Some(41),
                active_generation: Some(3),
            },
            relation: HarnessReverseAttributionRelationV1::ManagedRecordBinding,
        };
        assert!(HarnessReverseAttributionV1 {
            subject: managed_subject,
            outcome: HarnessReverseAttributionOutcomeV1::Attributed,
            links: vec![mismatched_record],
        }.validate().is_err());

        let invalid_active_pair = HarnessReverseAttributionBindingV1::ManagedRecord {
            workspace: reverse_attribution_workspace(),
            record_id: HarnessSelectorV1::new("record-a").unwrap(),
            active_instance_id: Some(41),
            active_generation: None,
        };
        assert!(invalid_active_pair.validate().is_err());
    }

    #[test]
    fn operator_node_workspace_requests_are_exact_round_trips() {
        let node_id = node_workspace_origin().node_id;
        let workspace_id = node_workspace_origin().workspace_id;
        let path = HarnessRepositoryPathV1::new("src/lib.rs").unwrap();
        let object_id = HarnessGitObjectIdV1::new("a".repeat(40)).unwrap();
        let requests = vec![
            HarnessOperatorRequestV1::InspectNodeWorkspace {
                node_id: node_id.clone(),
                workspace_id: workspace_id.clone(),
            },
            HarnessOperatorRequestV1::ReadNodeWorkspaceFile {
                node_id: node_id.clone(),
                workspace_id: workspace_id.clone(),
                path: path.clone(),
            },
            HarnessOperatorRequestV1::ReadNodeGitHistory {
                node_id: node_id.clone(),
                workspace_id: workspace_id.clone(),
                path: Some(path.clone()),
                before: Some(object_id.clone()),
                limit: HARNESS_GIT_HISTORY_LIMIT_MAX,
            },
            HarnessOperatorRequestV1::ReadNodeGitDiff {
                node_id: node_id.clone(),
                workspace_id: workspace_id.clone(),
                mode: HarnessGitDiffModeV1::Commit { revision: object_id },
                path: Some(path),
            },
        ];
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        for request in requests {
            request.validate().expect("valid node-workspace request");
            let encoded = serde_json::to_string(&request).unwrap();
            for forbidden in ["run_id", "endpoint", "root", "worktree", "environment"] {
                assert!(!encoded.contains(forbidden), "request exposed {forbidden}");
            }
            let decoded: HarnessOperatorRequestV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, request);
            HarnessOperatorEnvelopeV1 {
                build_stamp: BUILD_STAMP.to_string(),
                credential: credential.clone(),
                request,
            }.validate().unwrap();
        }

        assert!(HarnessOperatorRequestV1::InspectNodeWorkspace {
            node_id: String::new(),
            workspace_id,
        }.validate().is_err());
        assert!(HarnessOperatorRequestV1::InspectNodeWorkspace {
            node_id,
            workspace_id: String::new(),
        }.validate().is_err());
    }

    #[test]
    fn operator_v9_node_workspace_replies_round_trip_without_run_or_diagnostic_fields() {
        let origin = node_workspace_origin();
        let path = HarnessRepositoryPathV1::new("src/lib.rs").unwrap();
        let commit = git_commit('a');
        let responses = vec![
            HarnessOperatorResponseV1::NodeWorkspaceInspected(
                HarnessNodeWorkspaceInspectionV1 {
                    origin: origin.clone(),
                    entries: vec![HarnessWorkspaceTreeEntryV1 {
                        relative_path: path.clone(),
                        kind: HarnessWorkspaceEntryKindV1::File,
                    }],
                    tree_truncated: false,
                    git: HarnessGitSummaryV1 {
                        is_repository: true,
                        branch: Some("main".to_owned()),
                        status: vec![HarnessGitStatusEntryV1 {
                            index_status: HarnessGitStatusCodeV1::Unmodified,
                            worktree_status: HarnessGitStatusCodeV1::Modified,
                            path: path.clone(),
                            previous_path: None,
                        }],
                        recent_commits: vec![HarnessGitCommitSummaryV1 {
                            id: commit.id.clone(),
                            summary: commit.subject.clone(),
                        }],
                        truncated: false,
                    },
                    truncation: None,
                },
            ),
            HarnessOperatorResponseV1::NodeWorkspaceFileRead(HarnessNodeWorkspaceFileV1 {
                origin: origin.clone(),
                path: path.clone(),
                content: HarnessWorkspaceFileContentV1::Utf8 {
                    text: "fn main() {}\n".to_owned(),
                    byte_len: 13,
                },
                revision: Some(HarnessWorkspaceFileRevisionV1::new("b".repeat(64)).unwrap()),
            }),
            HarnessOperatorResponseV1::NodeGitHistoryRead(HarnessNodeGitHistoryPageV1 {
                origin: origin.clone(),
                path: Some(path.clone()),
                commits: vec![commit],
                next_before: None,
                truncated: false,
            }),
            HarnessOperatorResponseV1::NodeGitDiffRead(HarnessNodeGitDiffV1 {
                origin,
                mode: HarnessGitDiffModeV1::Working,
                path: Some(path),
                text: "diff --git a/src/lib.rs b/src/lib.rs\n".to_owned(),
                truncated: false,
            }),
        ];
        for response in responses {
            response.validate().expect("valid V9 node-workspace response");
            let encoded = serde_json::to_string(&response).unwrap();
            assert_json_has_no_forbidden_keys(&encoded, &[
                "run_id",
                "run_revision",
                "root",
                "worktree",
                "endpoint",
                "diagnostic",
                "author_email",
                "committer_email",
                "environment",
            ]);
            let decoded: HarnessOperatorResponseV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, response);
        }

        let mismatched_workspace = HarnessNodeWorkspaceOriginV1 {
            workspace_id: "other-workspace".to_owned(),
            ..node_workspace_origin()
        };
        assert!(mismatched_workspace.validate_for("node-a", "workspace-a").is_err());
        mismatched_workspace.validate_for("node-a", "other-workspace").unwrap();
    }

    #[test]
    fn operator_node_workspace_write_verbs_are_exact_round_trips() {
        let node_id = node_workspace_origin().node_id;
        let workspace_id = node_workspace_origin().workspace_id;
        let path = HarnessRepositoryPathV1::new("src/lib.rs").unwrap();
        let revision = HarnessWorkspaceFileRevisionV1::new("b".repeat(64)).unwrap();
        let requests = vec![
            HarnessOperatorRequestV1::WriteNodeWorkspaceFile {
                node_id: node_id.clone(),
                workspace_id: workspace_id.clone(),
                path: path.clone(),
                content: "fn main() {}\n".to_owned(),
                expected_revision: revision.clone(),
            },
            HarnessOperatorRequestV1::CreateNodeWorkspaceFile {
                node_id: node_id.clone(),
                workspace_id: workspace_id.clone(),
                path: path.clone(),
            },
            HarnessOperatorRequestV1::CreateNodeWorkspaceDirectory {
                node_id: node_id.clone(),
                workspace_id: workspace_id.clone(),
                path: path.clone(),
            },
        ];
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        for request in requests {
            request.validate().expect("valid node-workspace write request");
            let encoded = serde_json::to_string(&request).unwrap();
            for forbidden in ["run_id", "endpoint", "root", "worktree", "environment"] {
                assert!(!encoded.contains(forbidden), "request exposed {forbidden}");
            }
            let decoded: HarnessOperatorRequestV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, request);
            HarnessOperatorEnvelopeV1 {
                build_stamp: BUILD_STAMP.to_string(),
                credential: credential.clone(),
                request,
            }.validate().unwrap();
        }

        let origin = node_workspace_origin();
        let responses = vec![
            HarnessOperatorResponseV1::NodeWorkspaceFileWritten(HarnessNodeWorkspaceFileV1 {
                origin: origin.clone(),
                path: path.clone(),
                content: HarnessWorkspaceFileContentV1::Utf8 {
                    text: "fn main() {}\n".to_owned(),
                    byte_len: 13,
                },
                revision: Some(revision.clone()),
            }),
            HarnessOperatorResponseV1::NodeWorkspaceFileCreated(HarnessNodeWorkspaceFileV1 {
                origin: origin.clone(),
                path: path.clone(),
                content: HarnessWorkspaceFileContentV1::Utf8 {
                    text: String::new(),
                    byte_len: 0,
                },
                revision: Some(revision.clone()),
            }),
            HarnessOperatorResponseV1::NodeWorkspaceDirectoryCreated(
                HarnessNodeWorkspaceDirectoryV1 {
                    origin: origin.clone(),
                    entry: HarnessWorkspaceTreeEntryV1 {
                        relative_path: path.clone(),
                        kind: HarnessWorkspaceEntryKindV1::Directory,
                    },
                },
            ),
        ];
        for response in responses {
            response.validate().expect("valid V11 node-workspace write response");
            let encoded = serde_json::to_string(&response).unwrap();
            assert_json_has_no_forbidden_keys(&encoded, &[
                "run_id",
                "run_revision",
                "root",
                "worktree",
                "endpoint",
                "diagnostic",
                "environment",
            ]);
            let decoded: HarnessOperatorResponseV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, response);
        }

        let wrong_kind_directory = HarnessNodeWorkspaceDirectoryV1 {
            origin,
            entry: HarnessWorkspaceTreeEntryV1 {
                relative_path: path,
                kind: HarnessWorkspaceEntryKindV1::File,
            },
        };
        assert!(wrong_kind_directory.validate().is_err());
    }

    #[test]
    fn node_workspace_write_v11_requests_reject_malformed_fields() {
        let node_id = node_workspace_origin().node_id;
        let workspace_id = node_workspace_origin().workspace_id;
        let path = HarnessRepositoryPathV1::new("src/lib.rs").unwrap();
        let revision = HarnessWorkspaceFileRevisionV1::new("b".repeat(64)).unwrap();

        let oversized_content = HarnessOperatorRequestV1::WriteNodeWorkspaceFile {
            node_id: node_id.clone(),
            workspace_id: workspace_id.clone(),
            path: path.clone(),
            content: "x".repeat(HARNESS_WORKSPACE_FILE_MAX_BYTES + 1),
            expected_revision: revision.clone(),
        };
        assert!(matches!(
            oversized_content.validate(),
            Err(HarnessOperatorApiError::InvalidWorkspaceFile),
        ));
        let bounded_content = HarnessOperatorRequestV1::WriteNodeWorkspaceFile {
            node_id: node_id.clone(),
            workspace_id: workspace_id.clone(),
            path: path.clone(),
            content: "x".repeat(HARNESS_WORKSPACE_FILE_MAX_BYTES),
            expected_revision: revision.clone(),
        };
        bounded_content.validate().unwrap();

        assert!(HarnessWorkspaceFileRevisionV1::new("not-a-sha256-digest").is_err());
        assert!(HarnessWorkspaceFileRevisionV1::new("A".repeat(64)).is_err());

        let empty_node_write = HarnessOperatorRequestV1::WriteNodeWorkspaceFile {
            node_id: String::new(),
            workspace_id: workspace_id.clone(),
            path: path.clone(),
            content: "ok".to_owned(),
            expected_revision: revision,
        };
        assert!(empty_node_write.validate().is_err());

        let empty_workspace_create_file = HarnessOperatorRequestV1::CreateNodeWorkspaceFile {
            node_id: node_id.clone(),
            workspace_id: String::new(),
            path: path.clone(),
        };
        assert!(empty_workspace_create_file.validate().is_err());

        let empty_node_create_directory = HarnessOperatorRequestV1::CreateNodeWorkspaceDirectory {
            node_id: String::new(),
            workspace_id,
            path,
        };
        assert!(empty_node_create_directory.validate().is_err());
    }

    #[test]
    fn operator_context_source_observation_is_exact_and_private() {
        let run_id = HarnessRunId::new(format!("hrun_{}", "8".repeat(24))).unwrap();
        let request = HarnessOperatorRequestV1::ObserveRunContextSource {
            run_id: run_id.clone(),
        };
        request.validate().unwrap();
        let request_json = serde_json::to_value(&request).unwrap();
        assert_eq!(
            request_json,
            serde_json::json!({
                "kind": "observe-run-context-source",
                "run_id": run_id,
            }),
        );
        for forbidden in ["authority", "operation_id", "idempotency_ref", "task_id"] {
            assert!(!request_json.to_string().contains(forbidden), "leaked {forbidden}");
        }
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        HarnessOperatorEnvelopeV1 {
            build_stamp: BUILD_STAMP.to_string(),
            credential,
            request,
        }.validate().unwrap();

        let response = HarnessRunContextSourceObservationV1 {
            run_id: HarnessRunId::new(format!("hrun_{}", "8".repeat(24))).unwrap(),
            run_revision: HarnessRevision::new(9).unwrap(),
            feature_state: FeatureObservationStateV1::Observed,
            message_count: 17,
            message_count_exact: true,
            completed_turn_count: Some(6),
            total_tokens: Some(4_096),
            observed_at_unix_ms: Some(1_000),
        };
        response.validate().unwrap();
        let response_json = serde_json::to_value(
            HarnessOperatorResponseV1::RunContextSourceObserved(response),
        ).unwrap();
        assert_eq!(
            response_json,
            serde_json::json!({
                "kind": "run-context-source-observed",
                "value": {
                    "run_id": format!("hrun_{}", "8".repeat(24)),
                    "run_revision": 9,
                    "feature_state": "observed",
                    "message_count": 17,
                    "message_count_exact": true,
                    "completed_turn_count": 6,
                    "total_tokens": 4096,
                    "observed_at_unix_ms": 1000,
                }
            }),
        );
        let response_json = response_json.to_string();
        for forbidden in [
            "transcript",
            "message_text",
            "provider_session",
            "provider_profile",
            "provider_home",
            "credential",
            "auth",
            "path",
            "model",
        ] {
            assert!(!response_json.contains(forbidden), "leaked {forbidden}");
        }

        let mut unknown = request_json;
        unknown["provider_session_id"] = serde_json::json!("forbidden");
        assert!(serde_json::from_value::<HarnessOperatorRequestV1>(unknown).is_err());
    }

    #[test]
    fn context_source_observation_rejects_inexact_or_mismatched_eligibility_claims() {
        let run_id = HarnessRunId::new(format!("hrun_{}", "8".repeat(24))).unwrap();
        let observed = HarnessRunContextSourceObservationV1 {
            run_id: run_id.clone(),
            run_revision: HarnessRevision::new(9).unwrap(),
            feature_state: FeatureObservationStateV1::Observed,
            message_count: 17,
            message_count_exact: true,
            completed_turn_count: Some(6),
            total_tokens: Some(4_096),
            observed_at_unix_ms: Some(1_000),
        };
        observed.validate_for(&run_id).unwrap();

        let mut inexact = observed.clone();
        inexact.message_count_exact = false;
        assert!(inexact.validate().is_err());
        let mut empty = observed.clone();
        empty.message_count = 0;
        assert!(empty.validate().is_err());
        let mut impossible_turns = observed.clone();
        impossible_turns.completed_turn_count = Some(18);
        assert!(impossible_turns.validate().is_err());
        let unobserved = HarnessRunContextSourceObservationV1 {
            feature_state: FeatureObservationStateV1::SupportedNotObserved,
            message_count: 0,
            message_count_exact: false,
            completed_turn_count: None,
            total_tokens: None,
            observed_at_unix_ms: None,
            ..observed.clone()
        };
        unobserved.validate().unwrap();
        let mut leaked_count = unobserved;
        leaked_count.total_tokens = Some(1);
        assert!(leaked_count.validate().is_err());

        let other_run_id = HarnessRunId::new(format!("hrun_{}", "9".repeat(24))).unwrap();
        assert!(matches!(
            observed.validate_for(&other_run_id),
            Err(HarnessOperatorApiError::InvalidRunContextSourceObservation),
        ));
    }

    fn session_address(instance_id: u64, generation: u64) -> HarnessRuntimeSessionAddressV1 {
        HarnessRuntimeSessionAddressV1 {
            node_id: "node-a".to_owned(),
            incarnation_id: "07".repeat(16),
            workspace_id: "workspace-a".to_owned(),
            instance_id,
            generation,
        }
    }

    fn sample_agent_stream_chunk(source_sequence: u64) -> HarnessAgentStreamChunkV1 {
        HarnessAgentStreamChunkV1 {
            source_sequence,
            kind: HarnessAgentStreamChunkKindV1::Text {
                text: format!("chunk-{source_sequence}"),
                is_delta: true,
            },
        }
    }

    fn sample_terminal_frame(sequence: u64) -> HarnessRuntimeTerminalFrameV1 {
        HarnessRuntimeTerminalFrameV1 {
            sequence,
            size: HarnessRuntimeTerminalSizeV1 { rows: 24, columns: 80 },
            cursor_row: 0,
            cursor_column: 0,
            formatted: format!("frame-{sequence}").into_bytes(),
            scrollback_formatted: Vec::new(),
            alternate_screen: false,
            mouse_protocol_enabled: false,
            mouse_protocol_encoding: HarnessRuntimeMouseProtocolEncodingV1::Default,
            produced_at_unix_ms: 1_000,
            screen_state: None,
            bracketed_paste: None,
        }
    }

    fn sample_runtime_session(instance_id: u64, generation: u64) -> HarnessRuntimeSessionV1 {
        HarnessRuntimeSessionV1 {
            instance_id,
            generation,
            provider: "codex".to_owned(),
            transport: HarnessRuntimeTransportV1::Pty,
            status: HarnessRuntimeSessionStatusV1::Running,
            process_id: Some(1234),
            terminal_size: Some(HarnessRuntimeTerminalSizeV1 { rows: 24, columns: 80 }),
            operation_pending: false,
            input_pending: false,
            screen_state: None,
        }
    }

    /// A fully-populated `OperatorGateStateV1` -- one accepted option, one
    /// declined, matching the shape `gate4agent-shell-native`'s numbered-list
    /// parser actually produces (see that crate's own tests for the parser
    /// itself; this crate only owns the wire mirror and its bounds).
    fn sample_operator_gate_state() -> OperatorGateStateV1 {
        OperatorGateStateV1 {
            kind: OperatorGateKindV1::HookTrust,
            subject: OperatorGateSubjectV1::Hooks { count: Some(6) },
            input: OperatorGateInputV1::NumberedList,
            options: vec![
                OperatorGateOptionV1 {
                    text: "Trust all and continue".to_owned(),
                    semantics: OperatorGateOptionSemanticsV1::Accept,
                    selected: false,
                },
                OperatorGateOptionV1 {
                    text: "Continue without trusting".to_owned(),
                    semantics: OperatorGateOptionSemanticsV1::Decline,
                    selected: true,
                },
            ],
        }
    }

    /// Every `PtyScreenStateV1` variant, for the round-trip and validation
    /// sweeps below -- kept as one list so a variant added to the enum
    /// without a matching addition here is caught by a stale sweep rather
    /// than silently under-covered.
    fn every_screen_state_variant() -> Vec<PtyScreenStateV1> {
        vec![
            PtyScreenStateV1::Unknown,
            PtyScreenStateV1::NotAgent { observed_process: "installer.exe".to_owned() },
            PtyScreenStateV1::OperatorGate { gate: sample_operator_gate_state() },
            PtyScreenStateV1::Failing { reason: "crash-loop".to_owned() },
            PtyScreenStateV1::Ready,
        ]
    }

    /// The literal proof the `deny_unknown_fields` hazard is closed: a
    /// pre-V13 reply (`screen_state: None`) must omit the KEY entirely, not
    /// serialize it as `null` -- an older client's struct has no such field
    /// at all, so a `null` value would still trip `deny_unknown_fields`.
    #[test]
    fn a_terminal_frame_with_no_screen_state_serializes_without_the_key_at_all() {
        let frame = sample_terminal_frame(1);
        assert!(frame.screen_state.is_none());
        let value = serde_json::to_value(&frame).unwrap();
        assert!(
            value.as_object().unwrap().get("screen_state").is_none(),
            "expected no screen_state key in {value}",
        );
    }

    /// Sibling of the above for `HarnessRuntimeSessionV1` -- the session
    /// inventory's own reply carries the identical hazard and the identical
    /// fix.
    #[test]
    fn a_runtime_session_with_no_screen_state_serializes_without_the_key_at_all() {
        let session = sample_runtime_session(1, 1);
        assert!(session.screen_state.is_none());
        let value = serde_json::to_value(&session).unwrap();
        assert!(
            value.as_object().unwrap().get("screen_state").is_none(),
            "expected no screen_state key in {value}",
        );
    }

    /// `Some(..)` round-trips byte-exact for every variant, on both wire
    /// types that carry it.
    #[test]
    fn some_screen_state_round_trips_exactly_for_every_variant_on_both_carriers() {
        for variant in every_screen_state_variant() {
            let mut frame = sample_terminal_frame(1);
            frame.screen_state = Some(variant.clone());
            let decoded_frame: HarnessRuntimeTerminalFrameV1 = serde_json::from_slice(
                &serde_json::to_vec(&frame).unwrap(),
            ).unwrap();
            assert_eq!(decoded_frame, frame);

            let mut session = sample_runtime_session(1, 1);
            session.screen_state = Some(variant.clone());
            let decoded_session: HarnessRuntimeSessionV1 = serde_json::from_slice(
                &serde_json::to_vec(&session).unwrap(),
            ).unwrap();
            assert_eq!(decoded_session, session);
        }
    }

    /// `validate()` rejects an oversized carried string inside a present
    /// `screen_state`, on both carriers -- the bound is enforced regardless
    /// of which wire type happens to hold the value.
    #[test]
    fn validate_rejects_an_oversized_string_inside_a_present_screen_state() {
        let oversized_process = "x".repeat(HARNESS_SCREEN_STATE_PROCESS_MAX_BYTES + 1);
        let oversized_gate = "x".repeat(HARNESS_SCREEN_STATE_GATE_MAX_BYTES + 1);
        let cases = [
            PtyScreenStateV1::NotAgent { observed_process: oversized_process },
            PtyScreenStateV1::Failing { reason: oversized_gate },
        ];
        for case in cases {
            let mut frame = sample_terminal_frame(1);
            frame.screen_state = Some(case.clone());
            assert!(
                matches!(frame.validate(), Err(HarnessOperatorApiError::InvalidRuntimeInventory)),
                "expected frame validate() to reject {case:?}",
            );

            let mut session = sample_runtime_session(1, 1);
            session.screen_state = Some(case.clone());
            assert!(
                matches!(session.validate(), Err(HarnessOperatorApiError::InvalidRuntimeInventory)),
                "expected session validate() to reject {case:?}",
            );
        }
    }

    /// Sibling of the sweep above for `OperatorGateStateV1`'s own nested
    /// bounds -- an oversized `Directory` path and an oversized option
    /// label are each, on their own, enough to fail `validate()`, on both
    /// carriers.
    #[test]
    fn validate_rejects_an_oversized_field_nested_inside_an_operator_gate() {
        let oversized_path = "x".repeat(HARNESS_GATE_PATH_MAX_BYTES + 1);
        let oversized_option_text = "x".repeat(HARNESS_GATE_OPTION_TEXT_MAX_BYTES + 1);
        let cases = [
            PtyScreenStateV1::OperatorGate {
                gate: OperatorGateStateV1 {
                    subject: OperatorGateSubjectV1::Directory { path: Some(oversized_path) },
                    ..sample_operator_gate_state()
                },
            },
            PtyScreenStateV1::OperatorGate {
                gate: OperatorGateStateV1 {
                    options: vec![OperatorGateOptionV1 {
                        text: oversized_option_text,
                        semantics: OperatorGateOptionSemanticsV1::Accept,
                        selected: false,
                    }],
                    ..sample_operator_gate_state()
                },
            },
        ];
        for case in cases {
            let mut frame = sample_terminal_frame(1);
            frame.screen_state = Some(case.clone());
            assert!(
                matches!(frame.validate(), Err(HarnessOperatorApiError::InvalidRuntimeInventory)),
                "expected frame validate() to reject {case:?}",
            );

            let mut session = sample_runtime_session(1, 1);
            session.screen_state = Some(case.clone());
            assert!(
                matches!(session.validate(), Err(HarnessOperatorApiError::InvalidRuntimeInventory)),
                "expected session validate() to reject {case:?}",
            );
        }
    }

    #[test]
    fn operator_session_verbs_are_exact_round_trips() {
        let session = session_address(41, 3);
        let requests = vec![
            HarnessOperatorRequestV1::SpawnSession {
                node_id: "node-a".to_owned(),
                workspace_id: "workspace-a".to_owned(),
                provider: "claude".to_owned(),
                provider_profile: "claude-default".to_owned(),
                mode: HarnessExecutionModeV1::Pty,
                terminal_size: HarnessRuntimeTerminalSizeV1 { rows: 40, columns: 120 },
                approval_level: None,
            },
            HarnessOperatorRequestV1::WriteSessionInput {
                session: session.clone(),
                text: "echo hi\n".to_owned(),
            },
            HarnessOperatorRequestV1::ResizeSession {
                session: session.clone(),
                terminal_size: HarnessRuntimeTerminalSizeV1 { rows: 50, columns: 160 },
            },
            HarnessOperatorRequestV1::StopSession {
                session: session.clone(),
                force: true,
            },
        ];
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        for request in requests {
            request.validate().expect("valid session verb request");
            let encoded = serde_json::to_string(&request).unwrap();
            let decoded: HarnessOperatorRequestV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, request);
            HarnessOperatorEnvelopeV1 {
                build_stamp: BUILD_STAMP.to_string(),
                credential: credential.clone(),
                request,
            }.validate().unwrap();
        }

        let responses = vec![
            HarnessOperatorResponseV1::SessionSpawned(session.clone()),
            HarnessOperatorResponseV1::SessionInputWritten,
            HarnessOperatorResponseV1::SessionResized,
            HarnessOperatorResponseV1::SessionStopped,
        ];
        for response in responses {
            response.validate().expect("valid V10 session verb response");
            let encoded = serde_json::to_string(&response).unwrap();
            let decoded: HarnessOperatorResponseV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, response);
        }
    }

    /// `PromptSession`'s own round trip, kept separate from the V10 session-
    /// verb test above: it landed on the wire under a later version and
    /// carries its own PTY-refusal doctrine (see its doc comment), not just
    /// another `WriteSessionInput`-shaped verb.
    #[test]
    fn operator_prompt_session_is_exact_round_trip() {
        let session = session_address(41, 3);
        let request = HarnessOperatorRequestV1::PromptSession {
            session: session.clone(),
            text: "please continue".to_owned(),
        };
        request.validate().expect("valid prompt-session request");
        let encoded = serde_json::to_string(&request).unwrap();
        let decoded: HarnessOperatorRequestV1 = serde_json::from_str(&encoded).unwrap();
        assert_eq!(decoded, request);
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        HarnessOperatorEnvelopeV1 {
            build_stamp: BUILD_STAMP.to_string(),
            credential,
            request,
        }.validate().unwrap();

        let response = HarnessOperatorResponseV1::SessionPrompted;
        response.validate().expect("valid prompt-session response");
        let encoded = serde_json::to_string(&response).unwrap();
        let decoded: HarnessOperatorResponseV1 = serde_json::from_str(&encoded).unwrap();
        assert_eq!(decoded, response);
    }

    #[test]
    fn session_spawn_and_control_requests_reject_malformed_fields() {
        let session = session_address(41, 3);
        let valid_spawn = HarnessOperatorRequestV1::SpawnSession {
            node_id: "node-a".to_owned(),
            workspace_id: "workspace-a".to_owned(),
            provider: "claude".to_owned(),
            provider_profile: "claude-default".to_owned(),
            mode: HarnessExecutionModeV1::Pty,
            terminal_size: HarnessRuntimeTerminalSizeV1 { rows: 40, columns: 120 },
            approval_level: None,
        };
        valid_spawn.validate().unwrap();

        let mut empty_node = valid_spawn.clone();
        if let HarnessOperatorRequestV1::SpawnSession { node_id, .. } = &mut empty_node {
            *node_id = String::new();
        }
        assert!(matches!(
            empty_node.validate(),
            Err(HarnessOperatorApiError::InvalidWorkspaceOrigin),
        ));

        let mut bad_provider = valid_spawn.clone();
        if let HarnessOperatorRequestV1::SpawnSession { provider, .. } = &mut bad_provider {
            *provider = "not a provider id!".to_owned();
        }
        assert!(matches!(
            bad_provider.validate(),
            Err(HarnessOperatorApiError::InvalidSessionSpawn),
        ));

        let mut zero_rows = valid_spawn.clone();
        if let HarnessOperatorRequestV1::SpawnSession { terminal_size, .. } = &mut zero_rows {
            terminal_size.rows = 0;
        }
        assert!(matches!(
            zero_rows.validate(),
            Err(HarnessOperatorApiError::InvalidSessionSpawn),
        ));

        let oversized_input = HarnessOperatorRequestV1::WriteSessionInput {
            session: session.clone(),
            text: "x".repeat(HARNESS_SESSION_INPUT_MAX_BYTES + 1),
        };
        assert!(matches!(
            oversized_input.validate(),
            Err(HarnessOperatorApiError::InvalidSessionControl),
        ));
        let bounded_input = HarnessOperatorRequestV1::WriteSessionInput {
            session: session.clone(),
            text: "x".repeat(HARNESS_SESSION_INPUT_MAX_BYTES),
        };
        bounded_input.validate().unwrap();

        let oversized_prompt = HarnessOperatorRequestV1::PromptSession {
            session: session.clone(),
            text: "x".repeat(HARNESS_SESSION_INPUT_MAX_BYTES + 1),
        };
        assert!(matches!(
            oversized_prompt.validate(),
            Err(HarnessOperatorApiError::InvalidSessionControl),
        ));
        let bounded_prompt = HarnessOperatorRequestV1::PromptSession {
            session: session.clone(),
            text: "x".repeat(HARNESS_SESSION_INPUT_MAX_BYTES),
        };
        bounded_prompt.validate().unwrap();

        let zero_resize = HarnessOperatorRequestV1::ResizeSession {
            session: session.clone(),
            terminal_size: HarnessRuntimeTerminalSizeV1 { rows: 24, columns: 0 },
        };
        assert!(matches!(
            zero_resize.validate(),
            Err(HarnessOperatorApiError::InvalidSessionControl),
        ));

        let mut malformed_session = session.clone();
        malformed_session.instance_id = 0;
        let bad_stop = HarnessOperatorRequestV1::StopSession {
            session: malformed_session,
            force: false,
        };
        assert!(bad_stop.validate().is_err());
    }

    #[test]
    fn operator_session_control_verbs_are_exact_round_trips() {
        let session = session_address(41, 3);
        let requests = vec![
            HarnessOperatorRequestV1::ControlSession {
                session: session.clone(),
                control: HarnessTerminalControlV1::Enter,
            },
            HarnessOperatorRequestV1::WriteSessionBytes {
                session: session.clone(),
                bytes: vec![0x1b, b'[', b'A'],
            },
            HarnessOperatorRequestV1::PasteSession {
                session: session.clone(),
                text: "pasted text\n".to_owned(),
            },
            HarnessOperatorRequestV1::RemoveSession {
                session: session.clone(),
            },
            HarnessOperatorRequestV1::ResumeSession {
                session: session.clone(),
                terminal_size: HarnessRuntimeTerminalSizeV1 { rows: 24, columns: 80 },
            },
        ];
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        for request in requests {
            request.validate().expect("valid session control verb request");
            let encoded = serde_json::to_string(&request).unwrap();
            let decoded: HarnessOperatorRequestV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, request);
            HarnessOperatorEnvelopeV1 {
                build_stamp: BUILD_STAMP.to_string(),
                credential: credential.clone(),
                request,
            }.validate().unwrap();
        }

        let responses = vec![
            HarnessOperatorResponseV1::SessionControlled,
            HarnessOperatorResponseV1::SessionBytesWritten,
            HarnessOperatorResponseV1::SessionPasted,
            HarnessOperatorResponseV1::SessionRemoved,
            HarnessOperatorResponseV1::SessionResumed,
        ];
        for response in responses {
            response.validate().expect("valid V11 session control verb response");
            let encoded = serde_json::to_string(&response).unwrap();
            let decoded: HarnessOperatorResponseV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, response);
        }
    }

    #[test]
    fn session_control_v11_requests_reject_malformed_fields() {
        let session = session_address(41, 3);

        let oversized_bytes = HarnessOperatorRequestV1::WriteSessionBytes {
            session: session.clone(),
            bytes: vec![0u8; HARNESS_SESSION_BYTES_MAX_BYTES + 1],
        };
        assert!(matches!(
            oversized_bytes.validate(),
            Err(HarnessOperatorApiError::InvalidSessionControl),
        ));
        let empty_bytes = HarnessOperatorRequestV1::WriteSessionBytes {
            session: session.clone(),
            bytes: Vec::new(),
        };
        assert!(matches!(
            empty_bytes.validate(),
            Err(HarnessOperatorApiError::InvalidSessionControl),
        ));
        let bounded_bytes = HarnessOperatorRequestV1::WriteSessionBytes {
            session: session.clone(),
            bytes: vec![0u8; HARNESS_SESSION_BYTES_MAX_BYTES],
        };
        bounded_bytes.validate().unwrap();

        let oversized_paste = HarnessOperatorRequestV1::PasteSession {
            session: session.clone(),
            text: "x".repeat(HARNESS_SESSION_INPUT_MAX_BYTES + 1),
        };
        assert!(matches!(
            oversized_paste.validate(),
            Err(HarnessOperatorApiError::InvalidSessionControl),
        ));
        let bounded_paste = HarnessOperatorRequestV1::PasteSession {
            session: session.clone(),
            text: "x".repeat(HARNESS_SESSION_INPUT_MAX_BYTES),
        };
        bounded_paste.validate().unwrap();

        let zero_resume_size = HarnessOperatorRequestV1::ResumeSession {
            session: session.clone(),
            terminal_size: HarnessRuntimeTerminalSizeV1 { rows: 0, columns: 80 },
        };
        assert!(matches!(
            zero_resume_size.validate(),
            Err(HarnessOperatorApiError::InvalidSessionControl),
        ));

        let mut malformed_session = session.clone();
        malformed_session.instance_id = 0;
        let bad_remove = HarnessOperatorRequestV1::RemoveSession {
            session: malformed_session.clone(),
        };
        assert!(bad_remove.validate().is_err());
        let bad_control = HarnessOperatorRequestV1::ControlSession {
            session: malformed_session,
            control: HarnessTerminalControlV1::Interrupt,
        };
        assert!(bad_control.validate().is_err());
    }

    fn sample_native_session_selection() -> HarnessNativeSessionSelectionV1 {
        HarnessNativeSessionSelectionV1 {
            route: HarnessNativeSessionRouteV1 {
                node_id: "node-a".to_owned(),
                incarnation_id: "1".repeat(32),
                scope: HarnessNativeSessionCatalogScopeV1::Workspace,
                workspace_id: Some("workspace-a".to_owned()),
                provider: "codex".to_owned(),
            },
            catalog_revision: 7,
            recent_cutoff_unix_ms: 9,
            selection_id: "selection-a".to_owned(),
        }
    }

    fn sample_managed_session(record_id: &str) -> HarnessRuntimeManagedSessionV1 {
        HarnessRuntimeManagedSessionV1 {
            record_id: record_id.to_owned(),
            display_name: "Session display name".to_owned(),
            display_name_truncated: false,
            provider: "codex".to_owned(),
            mode: HarnessRuntimeManagedModeV1::Pty,
            state: HarnessRuntimeManagedStateV1::Dormant,
            workspace_id: "workspace-a".to_owned(),
            active_binding: None,
            provider_identity_present: true,
            updated_at_unix_ms: 10,
            blocked_count: 0,
            last_blocked_at_ms: None,
        }
    }

    #[test]
    fn operator_session_record_family_requests_are_exact_round_trips() {
        let requests = vec![
            HarnessOperatorRequestV1::PreviewSessionRecord {
                node_id: "node-a".to_owned(),
                record_id: "record-a".to_owned(),
                message_limit: HARNESS_NATIVE_SESSION_PREVIEW_MESSAGE_LIMIT_MAX,
            },
            HarnessOperatorRequestV1::ResumeSessionRecord {
                node_id: "node-a".to_owned(),
                record_id: "record-a".to_owned(),
                terminal_size: HarnessRuntimeTerminalSizeV1 { rows: 24, columns: 80 },
                initial_prompt: Some("continue".to_owned()),
            },
            HarnessOperatorRequestV1::RenameSessionRecord {
                node_id: "node-a".to_owned(),
                record_id: "record-a".to_owned(),
                display_name: "Renamed session".to_owned(),
            },
            HarnessOperatorRequestV1::SetSessionTask {
                node_id: "node-a".to_owned(),
                record_id: "record-a".to_owned(),
                expected_revision: 3,
                target: HarnessSessionTaskTargetV1::Existing {
                    task_id: format!("task-{}", "a".repeat(24)),
                },
            },
            HarnessOperatorRequestV1::ForgetSessionRecord {
                node_id: "node-a".to_owned(),
                record_id: "record-a".to_owned(),
            },
            HarnessOperatorRequestV1::IndexProviderSession {
                node_id: "node-a".to_owned(),
                workspace_id: "workspace-a".to_owned(),
                provider: "codex".to_owned(),
                identity: HarnessProviderSessionIdentityV1 {
                    key: HarnessProviderSessionKeyV1::SessionId,
                    id: "session-123".to_owned(),
                    transcript_path: Some("/tmp/transcript.json".to_owned()),
                },
                display_name: "Indexed session".to_owned(),
            },
            HarnessOperatorRequestV1::IndexNativeSession {
                selection: sample_native_session_selection(),
                display_name: "Native indexed".to_owned(),
            },
        ];
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        for request in requests {
            request.validate().expect("valid session-record family request");
            let encoded = serde_json::to_string(&request).unwrap();
            let decoded: HarnessOperatorRequestV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, request);
            HarnessOperatorEnvelopeV1 {
                build_stamp: BUILD_STAMP.to_string(),
                credential: credential.clone(),
                request,
            }.validate().unwrap();
        }
    }

    #[test]
    fn operator_session_record_family_responses_are_exact_round_trips() {
        let record = sample_managed_session("record-a");
        let responses = vec![
            HarnessOperatorResponseV1::SessionRecordPreviewed(HarnessSessionRecordPreviewedV1 {
                record_id: "record-a".to_owned(),
                preview: HarnessNativeSessionPreviewV1 {
                    title: Some("Preview title".to_owned()),
                    modified_at_unix_ms: Some(10),
                    model: Some("gpt".to_owned()),
                    message_count: 1,
                    message_count_exact: true,
                    completed_turn_count: Some(1),
                    total_tokens: Some(100),
                    truncated: false,
                    messages: vec![HarnessNativeSessionPreviewMessageV1 {
                        role: HarnessNativeSessionPreviewRoleV1::User,
                        text: "hello".to_owned(),
                    }],
                },
            }),
            HarnessOperatorResponseV1::SessionRecordResumed(HarnessSessionRecordResumedV1 {
                record: record.clone(),
                session: session_address(41, 3),
            }),
            HarnessOperatorResponseV1::SessionRecordUpdated(record.clone()),
            HarnessOperatorResponseV1::SessionRecordForgotten { record_id: "record-a".to_owned() },
            HarnessOperatorResponseV1::ProviderSessionIndexed(record.clone()),
            HarnessOperatorResponseV1::NativeSessionIndexed(HarnessNativeSessionIndexedV1 {
                selection: sample_native_session_selection(),
                record,
            }),
        ];
        for response in responses {
            response.validate().expect("valid session-record family response");
            let encoded = serde_json::to_string(&response).unwrap();
            let decoded: HarnessOperatorResponseV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, response);
        }
    }

    #[test]
    fn session_record_family_requests_reject_malformed_fields() {
        let bad_node_id = HarnessOperatorRequestV1::PreviewSessionRecord {
            node_id: String::new(),
            record_id: "record-a".to_owned(),
            message_limit: 1,
        };
        assert!(matches!(
            bad_node_id.validate(),
            Err(HarnessOperatorApiError::InvalidSessionRecordRequest),
        ));

        let bad_limit = HarnessOperatorRequestV1::PreviewSessionRecord {
            node_id: "node-a".to_owned(),
            record_id: "record-a".to_owned(),
            message_limit: 0,
        };
        assert!(matches!(bad_limit.validate(), Err(HarnessOperatorApiError::InvalidLimit)));

        let empty_display_name = HarnessOperatorRequestV1::RenameSessionRecord {
            node_id: "node-a".to_owned(),
            record_id: "record-a".to_owned(),
            display_name: String::new(),
        };
        assert!(matches!(
            empty_display_name.validate(),
            Err(HarnessOperatorApiError::InvalidSessionRecordRequest),
        ));

        let oversized_display_name = HarnessOperatorRequestV1::RenameSessionRecord {
            node_id: "node-a".to_owned(),
            record_id: "record-a".to_owned(),
            display_name: "x".repeat(HARNESS_SESSION_RECORD_DISPLAY_NAME_MAX_BYTES + 1),
        };
        assert!(matches!(
            oversized_display_name.validate(),
            Err(HarnessOperatorApiError::InvalidSessionRecordRequest),
        ));

        let bounded_display_name = HarnessOperatorRequestV1::RenameSessionRecord {
            node_id: "node-a".to_owned(),
            record_id: "record-a".to_owned(),
            display_name: "x".repeat(HARNESS_SESSION_RECORD_DISPLAY_NAME_MAX_BYTES),
        };
        bounded_display_name.validate().unwrap();

        let zero_resume_size = HarnessOperatorRequestV1::ResumeSessionRecord {
            node_id: "node-a".to_owned(),
            record_id: "record-a".to_owned(),
            terminal_size: HarnessRuntimeTerminalSizeV1 { rows: 0, columns: 80 },
            initial_prompt: None,
        };
        assert!(matches!(
            zero_resume_size.validate(),
            Err(HarnessOperatorApiError::InvalidSessionRecordRequest),
        ));

        let empty_initial_prompt = HarnessOperatorRequestV1::ResumeSessionRecord {
            node_id: "node-a".to_owned(),
            record_id: "record-a".to_owned(),
            terminal_size: HarnessRuntimeTerminalSizeV1 { rows: 24, columns: 80 },
            initial_prompt: Some(String::new()),
        };
        assert!(matches!(
            empty_initial_prompt.validate(),
            Err(HarnessOperatorApiError::InvalidSessionRecordRequest),
        ));

        let bad_task_id = HarnessOperatorRequestV1::SetSessionTask {
            node_id: "node-a".to_owned(),
            record_id: "record-a".to_owned(),
            expected_revision: 1,
            target: HarnessSessionTaskTargetV1::Existing { task_id: String::new() },
        };
        assert!(matches!(
            bad_task_id.validate(),
            Err(HarnessOperatorApiError::InvalidSessionRecordRequest),
        ));

        let valid_clear_task = HarnessOperatorRequestV1::SetSessionTask {
            node_id: "node-a".to_owned(),
            record_id: "record-a".to_owned(),
            expected_revision: 1,
            target: HarnessSessionTaskTargetV1::Clear,
        };
        valid_clear_task.validate().unwrap();

        let bad_identity_id = HarnessOperatorRequestV1::IndexProviderSession {
            node_id: "node-a".to_owned(),
            workspace_id: "workspace-a".to_owned(),
            provider: "codex".to_owned(),
            identity: HarnessProviderSessionIdentityV1 {
                key: HarnessProviderSessionKeyV1::SessionId,
                id: String::new(),
                transcript_path: None,
            },
            display_name: "Indexed session".to_owned(),
        };
        assert!(matches!(
            bad_identity_id.validate(),
            Err(HarnessOperatorApiError::InvalidSessionRecordRequest),
        ));

        let mut malformed_selection = sample_native_session_selection();
        malformed_selection.catalog_revision = 0;
        let bad_index_native = HarnessOperatorRequestV1::IndexNativeSession {
            selection: malformed_selection.clone(),
            display_name: "Native indexed".to_owned(),
        };
        assert!(bad_index_native.validate().is_err());
        malformed_selection.catalog_revision = 7;
        HarnessOperatorRequestV1::IndexNativeSession {
            selection: malformed_selection,
            display_name: "Native indexed".to_owned(),
        }.validate().unwrap();
    }

    #[test]
    fn session_record_family_responses_reject_malformed_fields() {
        let mut malformed_record = sample_managed_session("record-a");
        malformed_record.record_id = String::new();
        assert!(
            HarnessOperatorResponseV1::SessionRecordUpdated(malformed_record.clone())
                .validate()
                .is_err()
        );
        assert!(
            HarnessOperatorResponseV1::ProviderSessionIndexed(malformed_record.clone())
                .validate()
                .is_err()
        );
        assert!(
            HarnessOperatorResponseV1::SessionRecordResumed(HarnessSessionRecordResumedV1 {
                record: malformed_record,
                session: session_address(41, 3),
            }).validate().is_err()
        );

        assert!(
            HarnessOperatorResponseV1::SessionRecordForgotten { record_id: String::new() }
                .validate()
                .is_err()
        );

        let mut malformed_session = session_address(41, 3);
        malformed_session.instance_id = 0;
        assert!(
            HarnessOperatorResponseV1::SessionRecordResumed(HarnessSessionRecordResumedV1 {
                record: sample_managed_session("record-a"),
                session: malformed_session,
            }).validate().is_err()
        );

        let mut malformed_selection = sample_native_session_selection();
        malformed_selection.selection_id = String::new();
        assert!(
            HarnessOperatorResponseV1::NativeSessionIndexed(HarnessNativeSessionIndexedV1 {
                selection: malformed_selection,
                record: sample_managed_session("record-a"),
            }).validate().is_err()
        );
    }

    fn host_path(value: &str) -> HarnessHostPathV1 {
        HarnessHostPathV1::new(value).unwrap()
    }

    fn sample_workspace_snapshot(workspace_id: &str) -> HarnessWorkspaceSnapshotV1 {
        HarnessWorkspaceSnapshotV1 {
            workspace_id: workspace_id.to_owned(),
            canonical_root: host_path(r"C:\fixtures\workspace"),
            worktree_service_mode: Some(HarnessWorktreeServiceModeV1::Manual),
        }
    }

    fn sample_git_worktree_snapshot() -> HarnessGitWorktreeSnapshotV1 {
        HarnessGitWorktreeSnapshotV1 {
            path: host_path(r"C:\fixtures\worktree"),
            head: "a".repeat(40),
            branch: Some("feature/resource-mutation".to_owned()),
            is_bare: false,
            is_main: false,
            locked: false,
            prunable: false,
            workspace_id: Some("worktree-workspace".to_owned()),
        }
    }

    #[test]
    fn operator_resource_mutation_family_requests_are_exact_round_trips() {
        let requests = vec![
            HarnessOperatorRequestV1::BrowseHostDirectories {
                node_id: "node-a".to_owned(),
                directory: Some(host_path(r"C:\fixtures")),
                after: Some(host_path(r"C:\fixtures\a")),
            },
            HarnessOperatorRequestV1::RegisterWorkspace {
                node_id: "node-a".to_owned(),
                workspace_id: "workspace-b".to_owned(),
                root: host_path(r"C:\fixtures\workspace-b"),
            },
            HarnessOperatorRequestV1::UnregisterWorkspace {
                node_id: "node-a".to_owned(),
                workspace_id: "workspace-b".to_owned(),
            },
            HarnessOperatorRequestV1::CreateStandaloneWorkspace {
                node_id: "node-a".to_owned(),
                workspace_id: "workspace-c".to_owned(),
                root: host_path(r"C:\fixtures\workspace-c"),
                initial_branch: Some("main".to_owned()),
            },
            HarnessOperatorRequestV1::CreateWorktree {
                node_id: "node-a".to_owned(),
                source_workspace_id: "workspace-a".to_owned(),
                workspace_id: "worktree-workspace".to_owned(),
                target_root: host_path(r"C:\fixtures\worktree"),
                branch: "feature/resource-mutation".to_owned(),
                base: Some("main".to_owned()),
            },
            HarnessOperatorRequestV1::RemoveWorktree {
                node_id: "node-a".to_owned(),
                source_workspace_id: "workspace-a".to_owned(),
                target_root: host_path(r"C:\fixtures\worktree"),
            },
            HarnessOperatorRequestV1::ExportContextPack { session: session_address(41, 3) },
            HarnessOperatorRequestV1::ForgetContextPack {
                node_id: "node-a".to_owned(),
                context_id: HarnessSelectorV1::new("context-a").unwrap(),
            },
        ];
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "b".repeat(64),
        )).unwrap();
        for request in requests {
            request.validate().expect("valid resource-mutation family request");
            let encoded = serde_json::to_string(&request).unwrap();
            let decoded: HarnessOperatorRequestV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, request);
            HarnessOperatorEnvelopeV1 {
                build_stamp: BUILD_STAMP.to_string(),
                credential: credential.clone(),
                request,
            }.validate().unwrap();
        }
    }

    #[test]
    fn operator_resource_mutation_family_responses_are_exact_round_trips() {
        let responses = vec![
            HarnessOperatorResponseV1::HostDirectoriesBrowsed(HarnessHostDirectoryListingV1 {
                directory: Some(host_path(r"C:\fixtures")),
                parent: Some(host_path(r"C:\")),
                entries: vec![HarnessHostDirectoryEntryV1 {
                    path: host_path(r"C:\fixtures\workspace"),
                    display_name: "workspace".to_owned(),
                    is_link: false,
                }],
                next_after: Some(host_path(r"C:\fixtures\workspace")),
                incomplete: true,
            }),
            HarnessOperatorResponseV1::WorkspaceRegistered(sample_workspace_snapshot("workspace-b")),
            HarnessOperatorResponseV1::StandaloneWorkspaceCreated(
                sample_workspace_snapshot("workspace-c"),
            ),
            HarnessOperatorResponseV1::WorkspaceUnregistered {
                workspace_id: "workspace-b".to_owned(),
            },
            HarnessOperatorResponseV1::WorktreeCreated {
                worktree: sample_git_worktree_snapshot(),
                workspace: sample_workspace_snapshot("worktree-workspace"),
            },
            HarnessOperatorResponseV1::WorktreeRemoved {
                target_root: host_path(r"C:\fixtures\worktree"),
                workspace_id: Some("worktree-workspace".to_owned()),
            },
            HarnessOperatorResponseV1::ContextPackExported(HarnessResolvedContextPackReceiptV1 {
                id: HarnessSelectorV1::new("context-a").unwrap(),
                digest: "sha256:".to_owned() + &"a".repeat(64),
                lineage: HarnessContextPackLineageV1 {
                    source_node_id: HarnessSelectorV1::new("node-a").unwrap(),
                    source_workspace_id: HarnessSelectorV1::new("workspace-a").unwrap(),
                    source_instance_id: 41,
                    source_generation: 3,
                    source_provider: HarnessSelectorV1::new("codex").unwrap(),
                },
                source_message_count: 4,
                retained_message_count: 4,
                byte_len: 128,
                truncated: false,
            }),
            HarnessOperatorResponseV1::ContextPackForgotten { context_id: "context-a".to_owned() },
        ];
        for response in responses {
            response.validate().expect("valid resource-mutation family response");
            let encoded = serde_json::to_string(&response).unwrap();
            let decoded: HarnessOperatorResponseV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, response);
        }
    }

    #[test]
    fn resource_mutation_family_requests_reject_malformed_fields() {
        let bad_node_id = HarnessOperatorRequestV1::BrowseHostDirectories {
            node_id: String::new(),
            directory: None,
            after: None,
        };
        assert!(matches!(
            bad_node_id.validate(),
            Err(HarnessOperatorApiError::InvalidHostDirectoryBrowseRequest),
        ));

        assert!(matches!(
            HarnessHostPathV1::new(""),
            Err(HarnessOperatorApiError::InvalidHostPath),
        ));
        assert!(matches!(
            HarnessHostPathV1::new("x".repeat(HARNESS_HOST_PATH_MAX_BYTES + 1)),
            Err(HarnessOperatorApiError::InvalidHostPath),
        ));
        HarnessHostPathV1::new("x".repeat(HARNESS_HOST_PATH_MAX_BYTES)).unwrap();

        let bad_workspace_id = HarnessOperatorRequestV1::RegisterWorkspace {
            node_id: "node-a".to_owned(),
            workspace_id: String::new(),
            root: host_path(r"C:\fixtures\workspace-b"),
        };
        assert!(matches!(
            bad_workspace_id.validate(),
            Err(HarnessOperatorApiError::InvalidResourceMutationRequest),
        ));

        let empty_branch = HarnessOperatorRequestV1::CreateWorktree {
            node_id: "node-a".to_owned(),
            source_workspace_id: "workspace-a".to_owned(),
            workspace_id: "worktree-workspace".to_owned(),
            target_root: host_path(r"C:\fixtures\worktree"),
            branch: String::new(),
            base: None,
        };
        assert!(matches!(
            empty_branch.validate(),
            Err(HarnessOperatorApiError::InvalidResourceMutationRequest),
        ));

        let control_char_base = HarnessOperatorRequestV1::CreateWorktree {
            node_id: "node-a".to_owned(),
            source_workspace_id: "workspace-a".to_owned(),
            workspace_id: "worktree-workspace".to_owned(),
            target_root: host_path(r"C:\fixtures\worktree"),
            branch: "feature/resource-mutation".to_owned(),
            base: Some("main\u{0007}".to_owned()),
        };
        assert!(matches!(
            control_char_base.validate(),
            Err(HarnessOperatorApiError::InvalidResourceMutationRequest),
        ));

        let mut malformed_session = session_address(41, 3);
        malformed_session.instance_id = 0;
        assert!(
            HarnessOperatorRequestV1::ExportContextPack { session: malformed_session }
                .validate()
                .is_err()
        );

        let bad_context_id = HarnessOperatorRequestV1::ForgetContextPack {
            node_id: "node-a".to_owned(),
            context_id: HarnessSelectorV1::new("context-a").unwrap(),
        };
        bad_context_id.validate().unwrap();
        let empty_forget_node_id = HarnessOperatorRequestV1::ForgetContextPack {
            node_id: String::new(),
            context_id: HarnessSelectorV1::new("context-a").unwrap(),
        };
        assert!(matches!(
            empty_forget_node_id.validate(),
            Err(HarnessOperatorApiError::InvalidResourceMutationRequest),
        ));
    }

    #[test]
    fn resource_mutation_family_responses_reject_malformed_fields() {
        assert!(
            HarnessOperatorResponseV1::WorkspaceUnregistered { workspace_id: String::new() }
                .validate()
                .is_err()
        );

        let mut malformed_workspace = sample_workspace_snapshot("workspace-b");
        malformed_workspace.workspace_id = String::new();
        assert!(
            HarnessOperatorResponseV1::WorkspaceRegistered(malformed_workspace)
                .validate()
                .is_err()
        );

        let mut malformed_worktree = sample_git_worktree_snapshot();
        malformed_worktree.head = String::new();
        assert!(
            HarnessOperatorResponseV1::WorktreeCreated {
                worktree: malformed_worktree,
                workspace: sample_workspace_snapshot("worktree-workspace"),
            }.validate().is_err()
        );

        assert!(
            HarnessOperatorResponseV1::WorktreeRemoved {
                target_root: host_path(r"C:\fixtures\worktree"),
                workspace_id: Some(String::new()),
            }.validate().is_err()
        );

        assert!(
            HarnessOperatorResponseV1::ContextPackForgotten { context_id: String::new() }
                .validate()
                .is_err()
        );
    }

    fn sample_redacted_task(id_byte: char) -> RedactedTaskV1 {
        RedactedTaskV1 {
            task_id: HarnessTaskId::new(format!("htask_{}", id_byte.to_string().repeat(24)))
                .unwrap(),
            revision: HarnessRevision::new(1).unwrap(),
            title: "Task title".to_owned(),
            body: String::new(),
            creator: TaskCreatorCategoryV1::User,
            parent_task_id: None,
            dependency_ids: Vec::new(),
            state: HarnessTaskStateV1::Backlog,
            run_ids: Vec::new(),
            references_redacted: false,
            result_refs: Vec::new(),
            artifact_refs: Vec::new(),
            created_at_unix_ms: 10,
            updated_at_unix_ms: 10,
        }
    }

    fn sample_redacted_run(id_byte: char) -> RedactedRunV1 {
        RedactedRunV1 {
            run_id: HarnessRunId::new(format!("hrun_{}", id_byte.to_string().repeat(24)))
                .unwrap(),
            revision: HarnessRevision::new(1).unwrap(),
            parent_run_id: None,
            task_id: None,
            operation_id: None,
            intent: RedactedRunIntentV1 {
                mode: HarnessExecutionModeV1::Pty,
                worktree: RedactedWorktreeIntentV1::Existing,
                has_delivery_bundle: false,
                has_continuation: false,
            },
            lifecycle: HarnessRunLifecycleV1::Requested,
            binding: RedactedBindingStateV1::None,
            result_disposition: None,
            failure_category: None,
            context_pack: None,
            git_facts: None,
            delivery_receipt: None,
            references_redacted: false,
            created_at_unix_ms: 10,
            updated_at_unix_ms: 10,
        }
    }

    fn simple_node_inventory(node_id: &str) -> HarnessRuntimeNodeInventoryV1 {
        HarnessRuntimeNodeInventoryV1 {
            node_id: node_id.to_owned(),
            incarnation_id: "07".repeat(16),
            observed_at_unix_ms: 1_000,
            event_sequence: 1,
            inventory: HarnessRuntimeInventoryV1 {
                enabled_providers: Vec::new(),
                workspaces: BTreeMap::new(),
                workspace_count: 0,
                workspaces_truncated: false,
                session_count: 0,
                sessions_truncated: false,
                managed_sessions: Vec::new(),
                managed_session_count: 0,
                managed_sessions_truncated: false,
                retired_count: 0,
                launch_inventory: None,

            machine: None,},
        }
    }

    #[test]
    fn operator_subscribe_events_is_exact_round_trip() {
        let request = HarnessOperatorRequestV1::SubscribeEvents {};
        request.validate().expect("valid subscribe request");
        let encoded = serde_json::to_string(&request).unwrap();
        let decoded: HarnessOperatorRequestV1 = serde_json::from_str(&encoded).unwrap();
        assert_eq!(decoded, request);

        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        HarnessOperatorEnvelopeV1 {
            build_stamp: BUILD_STAMP.to_string(),
            credential,
            request,
        }.validate().unwrap();
    }

    #[test]
    fn operator_subscribe_terminal_is_exact_round_trip() {
        let request = HarnessOperatorRequestV1::SubscribeTerminal {
            sessions: vec![session_address(1, 1), session_address(2, 1)],
        };
        request.validate().expect("valid subscribe-terminal request");
        let encoded = serde_json::to_string(&request).unwrap();
        let decoded: HarnessOperatorRequestV1 = serde_json::from_str(&encoded).unwrap();
        assert_eq!(decoded, request);

        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        HarnessOperatorEnvelopeV1 {
            build_stamp: BUILD_STAMP.to_string(),
            credential,
            request,
        }.validate().unwrap();
    }

    /// The single accepted build stamp: a foreign stamp is rejected, and the
    /// error names both the expected and the received stamp -- the
    /// diagnostic a silent binary-skew incident previously had neither of.
    #[test]
    fn envelope_with_a_foreign_build_stamp_is_rejected_naming_both_values() {
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        let foreign_stamp = "f".repeat(40);
        let envelope = HarnessOperatorEnvelopeV1 {
            build_stamp: foreign_stamp.clone(),
            credential,
            request: HarnessOperatorRequestV1::SubscribeEvents {},
        };
        let error = envelope
            .validate()
            .expect_err("a foreign build stamp must be rejected");
        assert!(matches!(
            &error,
            HarnessOperatorApiError::BuildStampMismatch { expected, received }
                if expected == BUILD_STAMP && received == &foreign_stamp,
        ));
        let message = error.to_string();
        assert!(message.contains(BUILD_STAMP));
        assert!(message.contains(&foreign_stamp));
    }

    #[test]
    fn subscribe_terminal_rejects_empty_oversized_and_duplicate_session_lists() {
        assert!(matches!(
            HarnessOperatorRequestV1::SubscribeTerminal { sessions: Vec::new() }.validate(),
            Err(HarnessOperatorApiError::InvalidTerminalPage),
        ));

        let oversized = (0..=HARNESS_TERMINAL_SUBSCRIPTION_SESSIONS_MAX as u64)
            .map(|instance_id| session_address(instance_id + 1, 1))
            .collect::<Vec<_>>();
        assert!(matches!(
            HarnessOperatorRequestV1::SubscribeTerminal { sessions: oversized }.validate(),
            Err(HarnessOperatorApiError::InvalidTerminalPage),
        ));

        let duplicated = vec![session_address(1, 1), session_address(1, 1)];
        assert!(matches!(
            HarnessOperatorRequestV1::SubscribeTerminal { sessions: duplicated }.validate(),
            Err(HarnessOperatorApiError::InvalidTerminalPage),
        ));

        let mut malformed = session_address(1, 1);
        malformed.node_id = String::new();
        assert!(HarnessOperatorRequestV1::SubscribeTerminal { sessions: vec![malformed] }
            .validate()
            .is_err());
    }

    #[test]
    fn operator_event_variants_are_exact_round_trips() {
        let task = sample_redacted_task('a');
        let run = sample_redacted_run('b');
        let node = simple_node_inventory("node-a");
        let events = vec![
            HarnessOperatorEventV1::SnapshotBaseline {
                sequence: 0,
                tasks: vec![task.clone()],
                runs: vec![run.clone()],
                nodes: vec![node.clone()],
            },
            HarnessOperatorEventV1::TaskChanged { sequence: 1, task: task.clone() },
            HarnessOperatorEventV1::RunChanged { sequence: 2, run: run.clone() },
            HarnessOperatorEventV1::RuntimeInventoryChanged { sequence: 3, node: node.clone() },
            HarnessOperatorEventV1::RuntimeInventoryRemoved {
                sequence: 4,
                node_id: "node-a".to_owned(),
            },
            HarnessOperatorEventV1::Lagged { sequence: 5 },
        ];
        for event in events {
            event.validate().expect("valid operator event");
            let encoded = serde_json::to_string(&event).unwrap();
            let decoded: HarnessOperatorEventV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, event);
        }
    }

    #[test]
    fn operator_terminal_event_variants_are_exact_round_trips() {
        let events = vec![
            HarnessOperatorTerminalEventV1::TerminalFrame {
                sequence: 0,
                session: session_address(1, 1),
                frame: sample_terminal_frame(1),
                coalesced_since_last: 0,
            },
            HarnessOperatorTerminalEventV1::TerminalFrame {
                sequence: 1,
                session: session_address(1, 1),
                frame: sample_terminal_frame(2),
                coalesced_since_last: 3,
            },
            HarnessOperatorTerminalEventV1::Ping { sequence: 2 },
        ];
        for event in events {
            event.validate().expect("valid operator terminal event");
            let encoded = serde_json::to_string(&event).unwrap();
            let decoded: HarnessOperatorTerminalEventV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, event);
        }
    }

    #[test]
    fn operator_terminal_event_validate_rejects_a_malformed_frame_or_session() {
        let mut bad_session = session_address(1, 1);
        bad_session.node_id = String::new();
        let bad_session_event = HarnessOperatorTerminalEventV1::TerminalFrame {
            sequence: 0,
            session: bad_session,
            frame: sample_terminal_frame(1),
            coalesced_since_last: 0,
        };
        assert!(matches!(
            bad_session_event.validate(),
            Err(HarnessOperatorApiError::InvalidTerminalPage),
        ));

        let mut zero_size_frame = sample_terminal_frame(1);
        zero_size_frame.size.rows = 0;
        let bad_frame_event = HarnessOperatorTerminalEventV1::TerminalFrame {
            sequence: 0,
            session: session_address(1, 1),
            frame: zero_size_frame,
            coalesced_since_last: 0,
        };
        assert!(matches!(
            bad_frame_event.validate(),
            Err(HarnessOperatorApiError::InvalidTerminalPage),
        ));
    }

    #[test]
    fn operator_event_validate_rejects_malformed_frames() {
        let task_a = sample_redacted_task('a');
        let task_b = sample_redacted_task('b');
        let out_of_order = HarnessOperatorEventV1::SnapshotBaseline {
            sequence: 0,
            tasks: vec![task_b, task_a],
            runs: Vec::new(),
            nodes: Vec::new(),
        };
        assert!(out_of_order.validate().is_err());

        let bad_node_id = HarnessOperatorEventV1::RuntimeInventoryRemoved {
            sequence: 0,
            node_id: String::new(),
        };
        assert!(matches!(
            bad_node_id.validate(),
            Err(HarnessOperatorApiError::InvalidRuntimeInventory),
        ));

        let mut malformed_task = sample_redacted_task('c');
        malformed_task.title = "\0".to_owned();
        let bad_task = HarnessOperatorEventV1::TaskChanged { sequence: 0, task: malformed_task };
        assert!(bad_task.validate().is_err());
    }

    /// `HarnessOperatorHostErrorV1::Unsupported` (added alongside
    /// `hatchery-harness-light`, see its own doc comment): a purely
    /// additive unit variant, so this only needs to prove it round-trips --
    /// on its own, and wrapped in the `HarnessOperatorReplyV1::Error` shape
    /// every operator host error actually rides on the wire.
    #[test]
    fn operator_host_error_unsupported_round_trips() {
        let error = HarnessOperatorHostErrorV1::Unsupported;
        let encoded = serde_json::to_string(&error).unwrap();
        assert_eq!(encoded, "\"unsupported\"");
        assert_eq!(serde_json::from_str::<HarnessOperatorHostErrorV1>(&encoded).unwrap(), error);

        let reply = HarnessOperatorReplyV1::Error { error };
        assert!(reply.validate().is_ok());
        let encoded_reply = serde_json::to_vec(&reply).unwrap();
        let decoded_reply: HarnessOperatorReplyV1 = serde_json::from_slice(&encoded_reply).unwrap();
        assert_eq!(decoded_reply, reply);
    }

    #[test]
    fn operator_acp_control_verbs_are_exact_round_trips() {
        let session = session_address(7, 2);
        let requests = vec![
            HarnessOperatorRequestV1::ResolveInteraction {
                session: session.clone(),
                correlation_id: "a".repeat(HARNESS_OBSERVATION_LABEL_MAX_BYTES),
                response: HarnessProviderInteractionResponseV1::ApproveOnce,
            },
            HarnessOperatorRequestV1::ResolveInteraction {
                session: session.clone(),
                correlation_id: "correlation-1".to_owned(),
                response: HarnessProviderInteractionResponseV1::Deny,
            },
            HarnessOperatorRequestV1::ResolveInteraction {
                session: session.clone(),
                correlation_id: "correlation-2".to_owned(),
                response: HarnessProviderInteractionResponseV1::Answer {
                    text: "yes, proceed".to_owned(),
                },
            },
            HarnessOperatorRequestV1::SetSessionMode {
                session: session.clone(),
                mode_id: "plan".to_owned(),
            },
            HarnessOperatorRequestV1::SetSessionConfigOption {
                session: session.clone(),
                option_id: "reasoning-effort".to_owned(),
                value_json: "\"high\"".to_owned(),
            },
            HarnessOperatorRequestV1::SetSessionModel {
                session: session.clone(),
                model_id: "grok-4".to_owned(),
            },
        ];
        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        for request in requests {
            request.validate().expect("valid ACP control verb request");
            let encoded = serde_json::to_string(&request).unwrap();
            let decoded: HarnessOperatorRequestV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, request);
            HarnessOperatorEnvelopeV1 {
                build_stamp: BUILD_STAMP.to_string(),
                credential: credential.clone(),
                request,
            }.validate().unwrap();
        }

        let responses = vec![
            HarnessOperatorResponseV1::InteractionResolved,
            HarnessOperatorResponseV1::SessionModeSet,
            HarnessOperatorResponseV1::SessionConfigOptionSet,
            HarnessOperatorResponseV1::SessionModelSet,
        ];
        for response in responses {
            response.validate().expect("valid ACP control verb response");
            let encoded = serde_json::to_string(&response).unwrap();
            let decoded: HarnessOperatorResponseV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, response);
        }
    }

    #[test]
    fn acp_control_requests_reject_malformed_fields() {
        let session = session_address(7, 2);

        let empty_correlation = HarnessOperatorRequestV1::ResolveInteraction {
            session: session.clone(),
            correlation_id: String::new(),
            response: HarnessProviderInteractionResponseV1::ApproveOnce,
        };
        assert!(matches!(
            empty_correlation.validate(),
            Err(HarnessOperatorApiError::InvalidSessionControl),
        ));

        let oversized_correlation = HarnessOperatorRequestV1::ResolveInteraction {
            session: session.clone(),
            correlation_id: "a".repeat(HARNESS_OBSERVATION_LABEL_MAX_BYTES + 1),
            response: HarnessProviderInteractionResponseV1::Deny,
        };
        assert!(matches!(
            oversized_correlation.validate(),
            Err(HarnessOperatorApiError::InvalidSessionControl),
        ));

        let oversized_answer = HarnessOperatorRequestV1::ResolveInteraction {
            session: session.clone(),
            correlation_id: "correlation-1".to_owned(),
            response: HarnessProviderInteractionResponseV1::Answer {
                text: "x".repeat(HARNESS_ACP_INTERACTION_RESPONSE_MAX_BYTES + 1),
            },
        };
        assert!(matches!(
            oversized_answer.validate(),
            Err(HarnessOperatorApiError::InvalidSessionControl),
        ));

        let empty_mode_id = HarnessOperatorRequestV1::SetSessionMode {
            session: session.clone(),
            mode_id: String::new(),
        };
        assert!(matches!(
            empty_mode_id.validate(),
            Err(HarnessOperatorApiError::InvalidSessionControl),
        ));

        let control_char_option_id = HarnessOperatorRequestV1::SetSessionConfigOption {
            session: session.clone(),
            option_id: "bad\u{0007}id".to_owned(),
            value_json: "true".to_owned(),
        };
        assert!(matches!(
            control_char_option_id.validate(),
            Err(HarnessOperatorApiError::InvalidSessionControl),
        ));

        let empty_value_json = HarnessOperatorRequestV1::SetSessionConfigOption {
            session: session.clone(),
            option_id: "reasoning-effort".to_owned(),
            value_json: String::new(),
        };
        assert!(matches!(
            empty_value_json.validate(),
            Err(HarnessOperatorApiError::InvalidSessionControl),
        ));

        let oversized_model_id = HarnessOperatorRequestV1::SetSessionModel {
            session: session.clone(),
            model_id: "x".repeat(HARNESS_AGENT_STREAM_ID_MAX_BYTES + 1),
        };
        assert!(matches!(
            oversized_model_id.validate(),
            Err(HarnessOperatorApiError::InvalidSessionControl),
        ));

        let mut malformed_session = session.clone();
        malformed_session.instance_id = 0;
        let bad_session = HarnessOperatorRequestV1::SetSessionModel {
            session: malformed_session,
            model_id: "grok-4".to_owned(),
        };
        assert!(bad_session.validate().is_err());
    }

    #[test]
    fn operator_subscribe_agent_stream_is_exact_round_trip() {
        let request = HarnessOperatorRequestV1::SubscribeAgentStream {
            sessions: vec![session_address(1, 1), session_address(2, 1)],
        };
        request.validate().expect("valid subscribe-agent-stream request");
        let encoded = serde_json::to_string(&request).unwrap();
        let decoded: HarnessOperatorRequestV1 = serde_json::from_str(&encoded).unwrap();
        assert_eq!(decoded, request);

        let credential = HarnessOperatorCredential::parse(format!(
            "g4aho_{}",
            "a".repeat(64),
        )).unwrap();
        HarnessOperatorEnvelopeV1 {
            build_stamp: BUILD_STAMP.to_string(),
            credential,
            request,
        }.validate().unwrap();
    }

    #[test]
    fn subscribe_agent_stream_rejects_empty_oversized_and_duplicate_session_lists() {
        assert!(matches!(
            HarnessOperatorRequestV1::SubscribeAgentStream { sessions: Vec::new() }.validate(),
            Err(HarnessOperatorApiError::InvalidAgentStream),
        ));

        let oversized = (0..=HARNESS_AGENT_STREAM_SUBSCRIPTION_SESSIONS_MAX as u64)
            .map(|instance_id| session_address(instance_id + 1, 1))
            .collect::<Vec<_>>();
        assert!(matches!(
            HarnessOperatorRequestV1::SubscribeAgentStream { sessions: oversized }.validate(),
            Err(HarnessOperatorApiError::InvalidAgentStream),
        ));

        let duplicated = vec![session_address(1, 1), session_address(1, 1)];
        assert!(matches!(
            HarnessOperatorRequestV1::SubscribeAgentStream { sessions: duplicated }.validate(),
            Err(HarnessOperatorApiError::InvalidAgentStream),
        ));

        let mut malformed = session_address(1, 1);
        malformed.node_id = String::new();
        assert!(HarnessOperatorRequestV1::SubscribeAgentStream { sessions: vec![malformed] }
            .validate()
            .is_err());
    }

    #[test]
    fn operator_agent_event_variants_are_exact_round_trips() {
        let session = session_address(1, 1);
        let chunks = vec![
            HarnessAgentStreamChunkKindV1::Text { text: "hello".to_owned(), is_delta: false },
            HarnessAgentStreamChunkKindV1::Thinking { text: "considering options".to_owned() },
            HarnessAgentStreamChunkKindV1::InteractionPrompt {
                correlation_id: "correlation-1".to_owned(),
                interaction_kind: HarnessProviderInteractionKindV1::Approval,
                tool_name: "execute".to_owned(),
                title: Some("Run command?".to_owned()),
                prompt: "rm -rf /tmp/scratch".to_owned(),
                options: vec![HarnessAgentStreamInteractionOptionV1 {
                    option_id: "allow".to_owned(),
                    name: "Allow".to_owned(),
                    kind: "allow_once".to_owned(),
                }],
            },
            HarnessAgentStreamChunkKindV1::ModeCatalog {
                current: Some("plan".to_owned()),
                available: vec![HarnessAgentStreamNamedIdV1 {
                    id: "plan".to_owned(),
                    name: "Plan".to_owned(),
                    description: None,
                }],
            },
            HarnessAgentStreamChunkKindV1::ConfigOptions {
                options: vec![HarnessProviderConfigOptionV1 {
                    id: "reasoning-effort".to_owned(),
                    name: "Reasoning effort".to_owned(),
                    description: Some("How hard to think".to_owned()),
                    category: Some("model".to_owned()),
                    kind: HarnessProviderConfigOptionKindV1::Select,
                    value_json: "\"high\"".to_owned(),
                    choices: vec![HarnessProviderConfigChoiceV1 {
                        value_json: "\"high\"".to_owned(),
                        label: Some("High".to_owned()),
                    }],
                }],
            },
            HarnessAgentStreamChunkKindV1::ModelCatalog {
                current: Some("grok-4".to_owned()),
                available: vec![HarnessAgentStreamNamedIdV1 {
                    id: "grok-4".to_owned(),
                    name: "Grok 4".to_owned(),
                    description: None,
                }],
            },
        ];

        let mut events: Vec<HarnessOperatorAgentEventV1> = chunks
            .into_iter()
            .enumerate()
            .map(|(index, kind)| HarnessOperatorAgentEventV1::AgentChunk {
                sequence: index as u64,
                session: session.clone(),
                chunk: HarnessAgentStreamChunkV1 { source_sequence: index as u64, kind },
                published_at_ms: 1_000 + index as u64,
            })
            .collect();
        events.push(HarnessOperatorAgentEventV1::Lagged {
            sequence: 100,
            session: session.clone(),
            dropped: 3,
        });
        events.push(HarnessOperatorAgentEventV1::ReplayBoundary {
            session: session.clone(),
            replayed: 6,
            dropped_before_replay: 44,
        });
        events.push(HarnessOperatorAgentEventV1::Ping { sequence: 101 });

        for event in events {
            event.validate().expect("valid operator agent event");
            let encoded = serde_json::to_string(&event).unwrap();
            let decoded: HarnessOperatorAgentEventV1 = serde_json::from_str(&encoded).unwrap();
            assert_eq!(decoded, event);
        }
    }

    /// `TasksList`/`RunsList` gain a parent filter ("what did this task/run
    /// spawn") additive over the pre-existing shape via `#[serde(default)]`:
    /// an old-shape request with no `parent_task_id`/`parent_run_id` key
    /// still decodes (defaulting to `None`), and a set value round-trips.
    /// A malformed id can never reach `parent_task_id`/
    /// `parent_run_id` in the first place -- `HarnessTaskId::new`/
    /// `HarnessRunId::new` are the validation, refusing a malformed string
    /// before a request carrying one could ever be built (the CLI's own
    /// rejection of a malformed `--parent`/`--parent-run` flag, tested in
    /// `hatchery-harnessctl`, goes through this exact constructor).
    #[test]
    fn parent_filters_default_on_old_shape_and_round_trip() {
        let task_id = HarnessTaskId::new(format!("htask_{}", "a".repeat(24))).unwrap();
        let tasks_request = HarnessReadRequestV1::TasksList {
            after_task_id: None,
            state: None,
            parent_task_id: Some(task_id.clone()),
            limit: 10,
        };
        tasks_request.validate().unwrap();
        let encoded_tasks = serde_json::to_string(&tasks_request).unwrap();
        assert_eq!(
            serde_json::from_str::<HarnessReadRequestV1>(&encoded_tasks).unwrap(),
            tasks_request,
        );

        let old_shape_tasks = serde_json::json!({
            "kind": "tasks-list",
            "after_task_id": null,
            "state": null,
            "limit": 10,
        });
        let decoded_tasks: HarnessReadRequestV1 = serde_json::from_value(old_shape_tasks).unwrap();
        match &decoded_tasks {
            HarnessReadRequestV1::TasksList { parent_task_id, .. } => assert!(parent_task_id.is_none()),
            other => panic!("expected TasksList, got {other:?}"),
        }
        decoded_tasks.validate().unwrap();
        assert!(HarnessTaskId::new("not-a-task-id").is_err());

        let run_id = HarnessRunId::new(format!("hrun_{}", "b".repeat(24))).unwrap();
        let runs_request = HarnessReadRequestV1::RunsList {
            task_id: None,
            after_run_id: None,
            lifecycle: None,
            parent_run_id: Some(run_id.clone()),
            limit: 10,
        };
        runs_request.validate().unwrap();
        let encoded_runs = serde_json::to_string(&runs_request).unwrap();
        assert_eq!(
            serde_json::from_str::<HarnessReadRequestV1>(&encoded_runs).unwrap(),
            runs_request,
        );

        let old_shape_runs = serde_json::json!({
            "kind": "runs-list",
            "task_id": null,
            "after_run_id": null,
            "lifecycle": null,
            "limit": 10,
        });
        let decoded_runs: HarnessReadRequestV1 = serde_json::from_value(old_shape_runs).unwrap();
        match &decoded_runs {
            HarnessReadRequestV1::RunsList { parent_run_id, .. } => assert!(parent_run_id.is_none()),
            other => panic!("expected RunsList, got {other:?}"),
        }
        decoded_runs.validate().unwrap();
        assert!(HarnessRunId::new("not-a-run-id").is_err());
    }

    #[test]
    fn operator_agent_event_validate_rejects_malformed_chunks_or_sessions() {
        let session = session_address(1, 1);

        let mut bad_session = session.clone();
        bad_session.node_id = String::new();
        let bad_session_event = HarnessOperatorAgentEventV1::AgentChunk {
            sequence: 0,
            session: bad_session,
            chunk: sample_agent_stream_chunk(0),
            published_at_ms: 1_000,
        };
        assert!(bad_session_event.validate().is_err());

        let oversized_text_chunk = HarnessAgentStreamChunkV1 {
            source_sequence: 0,
            kind: HarnessAgentStreamChunkKindV1::Text {
                text: "x".repeat(HARNESS_AGENT_STREAM_TEXT_MAX_BYTES + 1),
                is_delta: false,
            },
        };
        let bad_chunk_event = HarnessOperatorAgentEventV1::AgentChunk {
            sequence: 0,
            session: session.clone(),
            chunk: oversized_text_chunk,
            published_at_ms: 1_000,
        };
        assert!(matches!(
            bad_chunk_event.validate(),
            Err(HarnessOperatorApiError::InvalidAgentStream),
        ));

        let empty_correlation_prompt = HarnessAgentStreamChunkV1 {
            source_sequence: 0,
            kind: HarnessAgentStreamChunkKindV1::InteractionPrompt {
                correlation_id: String::new(),
                interaction_kind: HarnessProviderInteractionKindV1::Question,
                tool_name: "ask".to_owned(),
                title: None,
                prompt: "What next?".to_owned(),
                options: Vec::new(),
            },
        };
        let bad_prompt_event = HarnessOperatorAgentEventV1::AgentChunk {
            sequence: 0,
            session: session.clone(),
            chunk: empty_correlation_prompt,
            published_at_ms: 1_000,
        };
        assert!(matches!(
            bad_prompt_event.validate(),
            Err(HarnessOperatorApiError::InvalidAgentStream),
        ));
    }

    /// D5, Slice D: `g4a_task_create`'s wire request round-trips and bounds
    /// title/body at this coarse wire-level ceiling -- the authoritative
    /// rule (trim/control-character checks) is the engine's own
    /// `HarnessTaskV1::validate`, reached at apply time, not this validate().
    #[test]
    fn task_create_request_round_trips_and_bounds_title_and_body() {
        let request = HarnessReadRequestV1::TaskCreate {
            title: "Investigate the flaky retry".to_owned(),
            body: "See the timeline for the failing run.".to_owned(),
            parent_task_id: Some(HarnessTaskId::new(format!("htask_{}", "a".repeat(24))).unwrap()),
        };
        request.validate().unwrap();
        let encoded = serde_json::to_string(&request).unwrap();
        assert_eq!(serde_json::from_str::<HarnessReadRequestV1>(&encoded).unwrap(), request);

        let no_parent = HarnessReadRequestV1::TaskCreate {
            title: "Child under my own task".to_owned(),
            body: String::new(),
            parent_task_id: None,
        };
        no_parent.validate().unwrap();

        let empty_title = HarnessReadRequestV1::TaskCreate {
            title: String::new(),
            body: String::new(),
            parent_task_id: None,
        };
        assert!(matches!(empty_title.validate(), Err(HarnessReadApiError::InvalidTaskCreate)));

        let oversized_title = HarnessReadRequestV1::TaskCreate {
            title: "x".repeat(HARNESS_TITLE_MAX_BYTES + 1),
            body: String::new(),
            parent_task_id: None,
        };
        assert!(matches!(oversized_title.validate(), Err(HarnessReadApiError::InvalidTaskCreate)));

        let oversized_body = HarnessReadRequestV1::TaskCreate {
            title: "ok".to_owned(),
            body: "x".repeat(HARNESS_BODY_MAX_BYTES + 1),
            parent_task_id: None,
        };
        assert!(matches!(oversized_body.validate(), Err(HarnessReadApiError::InvalidTaskCreate)));
    }

    /// D5, Slice D: `g4a_task_move`'s wire request round-trips.
    #[test]
    fn task_move_request_round_trips() {
        let request = HarnessReadRequestV1::TaskMove {
            task_id: HarnessTaskId::new(format!("htask_{}", "b".repeat(24))).unwrap(),
            expected_revision: HarnessRevision::new(3).unwrap(),
            to: HarnessTaskStateV1::Ready,
        };
        request.validate().unwrap();
        let encoded = serde_json::to_string(&request).unwrap();
        assert_eq!(serde_json::from_str::<HarnessReadRequestV1>(&encoded).unwrap(), request);
    }

    /// S10: `g4a_run_finish`'s wire request round-trips, `summary` defaults
    /// to `None` for a caller that omits it, and an oversized summary is a
    /// named validation failure (`HARNESS_BODY_MAX_BYTES`, the same bound
    /// task bodies enforce).
    #[test]
    fn run_finish_request_round_trips_and_bounds_summary() {
        let request = HarnessReadRequestV1::RunFinish {
            outcome: HarnessRunFinishOutcomeV1::Done,
            summary: Some("handed off cleanly".to_owned()),
        };
        request.validate().unwrap();
        let encoded = serde_json::to_string(&request).unwrap();
        assert_eq!(serde_json::from_str::<HarnessReadRequestV1>(&encoded).unwrap(), request);

        let without_summary: HarnessReadRequestV1 = serde_json::from_value(serde_json::json!({
            "kind": "run-finish",
            "outcome": "failed",
        })).unwrap();
        assert_eq!(
            without_summary,
            HarnessReadRequestV1::RunFinish { outcome: HarnessRunFinishOutcomeV1::Failed, summary: None },
        );
        without_summary.validate().unwrap();

        let oversized_summary = HarnessReadRequestV1::RunFinish {
            outcome: HarnessRunFinishOutcomeV1::Done,
            summary: Some("x".repeat(HARNESS_BODY_MAX_BYTES + 1)),
        };
        assert!(matches!(
            oversized_summary.validate(),
            Err(HarnessReadApiError::InvalidText("run finish summary")),
        ));
    }

    /// S10: `HarnessRunFinishResultV1` round-trips through the read response
    /// wire (`HarnessReadResponseV1::RunFinish`).
    #[test]
    fn run_finish_result_round_trips_through_the_response_wire() {
        let finished = HarnessReadResponseV1::RunFinish(HarnessRunFinishResultV1::Finished {
            run_id: HarnessRunId::new(format!("hrun_{}", "d".repeat(24))).unwrap(),
            task_id: HarnessTaskId::new(format!("htask_{}", "d".repeat(24))).unwrap(),
            result: HarnessRunFinishOutcomeV1::Done,
        });
        finished.validate().unwrap();
        let encoded = serde_json::to_string(&finished).unwrap();
        assert_eq!(serde_json::from_str::<HarnessReadResponseV1>(&encoded).unwrap(), finished);

        let already_finished = HarnessReadResponseV1::RunFinish(HarnessRunFinishResultV1::AlreadyFinished {
            run_id: HarnessRunId::new(format!("hrun_{}", "d".repeat(24))).unwrap(),
            lifecycle: HarnessRunLifecycleV1::Failed,
        });
        already_finished.validate().unwrap();
    }

    /// D5, Slice D: every `HarnessTaskCreateResultV1`/`HarnessTaskMoveResultV1`
    /// variant round-trips through the read response wire
    /// (`HarnessReadResponseV1::TaskCreate`/`TaskMove`).
    #[test]
    fn task_create_and_move_results_round_trip_every_variant() {
        let created = HarnessReadResponseV1::TaskCreate(HarnessTaskCreateResultV1::Created {
            task_id: HarnessTaskId::new(format!("htask_{}", "c".repeat(24))).unwrap(),
            revision: HarnessRevision::new(1).unwrap(),
        });
        created.validate().unwrap();
        let encoded = serde_json::to_string(&created).unwrap();
        assert_eq!(serde_json::from_str::<HarnessReadResponseV1>(&encoded).unwrap(), created);

        let parent_outside = HarnessReadResponseV1::TaskCreate(
            HarnessTaskCreateResultV1::ParentOutsideOwnSubtree {
                parent_task_id: HarnessTaskId::new(format!("htask_{}", "d".repeat(24))).unwrap(),
                own_task_id: HarnessTaskId::new(format!("htask_{}", "e".repeat(24))).unwrap(),
            },
        );
        parent_outside.validate().unwrap();

        let parent_terminal = HarnessReadResponseV1::TaskCreate(HarnessTaskCreateResultV1::ParentTerminal {
            parent_task_id: HarnessTaskId::new(format!("htask_{}", "d".repeat(24))).unwrap(),
            state: HarnessTaskStateV1::Done,
        });
        parent_terminal.validate().unwrap();

        let title_invalid = HarnessReadResponseV1::TaskCreate(HarnessTaskCreateResultV1::TitleInvalid {
            why: "task title is empty, unbounded, padded, or contains control characters".to_owned(),
        });
        title_invalid.validate().unwrap();

        let body_invalid = HarnessReadResponseV1::TaskCreate(HarnessTaskCreateResultV1::BodyInvalid {
            why: "task body is unbounded".to_owned(),
        });
        body_invalid.validate().unwrap();

        let dependencies_invalid = HarnessReadResponseV1::TaskCreate(
            HarnessTaskCreateResultV1::DependenciesInvalid { why: "dependencies must not self-link".to_owned() },
        );
        dependencies_invalid.validate().unwrap();

        let moved = HarnessReadResponseV1::TaskMove(HarnessTaskMoveResultV1::Moved {
            task_id: HarnessTaskId::new(format!("htask_{}", "f".repeat(24))).unwrap(),
            revision: HarnessRevision::new(2).unwrap(),
            from: HarnessTaskStateV1::Backlog,
            to: HarnessTaskStateV1::Ready,
        });
        moved.validate().unwrap();
        let encoded = serde_json::to_string(&moved).unwrap();
        assert_eq!(serde_json::from_str::<HarnessReadResponseV1>(&encoded).unwrap(), moved);

        let task_is_own = HarnessReadResponseV1::TaskMove(HarnessTaskMoveResultV1::TaskIsOwn {
            task_id: HarnessTaskId::new(format!("htask_{}", "f".repeat(24))).unwrap(),
        });
        task_is_own.validate().unwrap();

        let outside_subtree = HarnessReadResponseV1::TaskMove(HarnessTaskMoveResultV1::TaskOutsideOwnSubtree {
            task_id: HarnessTaskId::new(format!("htask_{}", "f".repeat(24))).unwrap(),
            own_task_id: HarnessTaskId::new(format!("htask_{}", "0".repeat(24))).unwrap(),
        });
        outside_subtree.validate().unwrap();

        let illegal_transition = HarnessReadResponseV1::TaskMove(HarnessTaskMoveResultV1::IllegalTransition {
            task_id: HarnessTaskId::new(format!("htask_{}", "f".repeat(24))).unwrap(),
            from: HarnessTaskStateV1::Done,
            to: HarnessTaskStateV1::Ready,
        });
        illegal_transition.validate().unwrap();

        let revision_conflict = HarnessReadResponseV1::TaskMove(HarnessTaskMoveResultV1::RevisionConflict {
            task_id: HarnessTaskId::new(format!("htask_{}", "f".repeat(24))).unwrap(),
            expected: HarnessRevision::new(2).unwrap(),
            current: HarnessRevision::new(3).unwrap(),
        });
        revision_conflict.validate().unwrap();
    }

    fn base_session_context() -> SessionContextV1 {
        SessionContextV1 {
            grant_id: SessionGrantId::new(format!("hgrant_{}", "a".repeat(24))).unwrap(),
            grant_revision: HarnessRevision::new(1).unwrap(),
            actor_run: CallerRunV1 {
                run_id: HarnessRunId::new(format!("hrun_{}", "a".repeat(24))).unwrap(),
                task_id: None,
                parent_run_id: None,
                lifecycle: HarnessRunLifecycleV1::Running,
                references_redacted: false,
            },
            task: None,
            sibling_runs: Vec::new(),
            read_permissions: HarnessReadPermissionsV1 {
                tasks: HarnessEntityReadScopeV1::None,
                runs: HarnessEntityReadScopeV1::None,
                operations: HarnessEntityReadScopeV1::None,
            },
            monitoring_visibility: HarnessMonitoringVisibilityV1::None,
            child_task_count: 0,
            child_task_subtree_depth: 0,
            task_create: false,
            task_mutate: false,
            allowed_tool_ids: vec!["g4a_context_get".to_owned(), "g4a_run_finish".to_owned()],
            history_message_count: None,
            completed_turn_count: None,
            total_tokens: None,
        }
    }

    /// D5, Slice D: `expected_allowed_tool_ids` derives `g4a_task_create`/
    /// `g4a_task_move` from `task_permissions.create`/`.mutate` -- create-only
    /// advertises `g4a_task_create` alone, and a grant cannot claim a tool it
    /// was not actually granted (`InvalidAllowedTools`).
    #[test]
    fn expected_allowed_tool_ids_derives_task_tools_from_task_permissions() {
        let base = base_session_context();
        base.validate().unwrap();

        let create_only = SessionContextV1 {
            task_create: true,
            allowed_tool_ids: vec![
                "g4a_context_get".to_owned(),
                "g4a_run_finish".to_owned(),
                "g4a_task_create".to_owned(),
            ],
            ..base.clone()
        };
        create_only.validate().unwrap();

        let both = SessionContextV1 {
            task_create: true,
            task_mutate: true,
            allowed_tool_ids: vec![
                "g4a_context_get".to_owned(),
                "g4a_run_finish".to_owned(),
                "g4a_task_create".to_owned(),
                "g4a_task_move".to_owned(),
            ],
            ..base.clone()
        };
        both.validate().unwrap();

        let create_only_claims_move = SessionContextV1 {
            task_create: true,
            allowed_tool_ids: vec![
                "g4a_context_get".to_owned(),
                "g4a_task_create".to_owned(),
                "g4a_task_move".to_owned(),
            ],
            ..base
        };
        assert!(matches!(
            create_only_claims_move.validate(),
            Err(HarnessReadApiError::InvalidAllowedTools),
        ));
    }

    /// The removed `maximum_child_count`/`maximum_child_depth` wire fields
    /// stated a cap that was checked for range but never compared against an
    /// actual child count anywhere -- this proves their replacements
    /// (`child_task_count`/`child_task_subtree_depth`) carry whatever the
    /// engine observed, with no ceiling of their own: a value far past the
    /// old `HARNESS_CHILD_COUNT_MAX`/`HARNESS_CHILD_DEPTH_MAX` bounds still
    /// validates and round-trips over the wire under the new field names.
    #[test]
    fn child_task_observations_carry_unbounded_values() {
        let base = base_session_context();
        let many_children = SessionContextV1 {
            child_task_count: u64::from(HARNESS_CHILD_COUNT_MAX) * 1_000,
            child_task_subtree_depth: u64::from(HARNESS_CHILD_DEPTH_MAX) * 1_000,
            ..base
        };
        many_children.validate().unwrap();
        let encoded = serde_json::to_value(&many_children).unwrap();
        assert_eq!(
            encoded.get("child_task_count").and_then(serde_json::Value::as_u64),
            Some(u64::from(HARNESS_CHILD_COUNT_MAX) * 1_000),
        );
        assert_eq!(
            encoded.get("child_task_subtree_depth").and_then(serde_json::Value::as_u64),
            Some(u64::from(HARNESS_CHILD_DEPTH_MAX) * 1_000),
        );
        assert!(encoded.get("maximum_child_count").is_none());
        assert!(encoded.get("maximum_child_depth").is_none());
        let decoded: SessionContextV1 = serde_json::from_value(encoded).unwrap();
        assert_eq!(decoded, many_children);
    }
}