argonauts-app 0.1.0

Argonauts Code's app controller and run host: the conversation, routing, choosing Captain or crew, and the run host each run has.
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
use std::collections::{BTreeMap, HashMap};

use argonauts_core::display_names::DisplayNames;
use argonauts_core::events::approval::{ApprovalEvent, ApprovalOpened, ApprovalPacket};
use argonauts_core::events::run::{
    RunLifecycle, RunMode, RunNamed, RunOutcome, RunStarted, WaitReason,
};
use argonauts_core::events::session::{CliWorkflowRun, SessionEvent};
use argonauts_core::events::task::MergeEvent;
use argonauts_core::events::workflow::{AgentLeftOut, StepEnd, WorkflowDeclared, WorkflowEvent};
use argonauts_core::events::{RunEvent, RunEventKind};
use argonauts_core::hidden_git::{MergedInto, PUT_BACK};
use argonauts_core::names::{ApprovalId, ModelRef, RoleName, SessionKey, WorkflowId};
use argonauts_core::steps::intake::InstructionKind;
use argonauts_core::views::{
    AgentRow, AgentView, InnerAgent, NestedWorkflowRow, PhaseCount, PhaseRow, RowNote, RowStatus,
    StatusKind, StatusLine, Tone, WorkflowRow, WorkflowState, WorkflowView, agents_counted,
    cli_workflow_run_id, is_workflow_agent, shape_known, writes_its_phases,
};
use argonauts_core::workflow::{StepRef, Workflow};
use argonauts_core::{AgentEvent, DiffStat, RunId, Spend};
use time::OffsetDateTime;

use super::sessions::{RunFacts, SessionFacts, add_spend};

#[cfg(test)]
mod crew_tests;
mod inner;
mod own;
#[cfg(test)]
mod own_tests;
mod pull_request;
pub(crate) use pull_request::followed_heading;
mod reported;
mod tries;

use reported::StepWorkflow;

/// Two levels inside the run are the most a run nests; a deeper chain can only come from a
/// damaged log.
const DEEPEST: usize = 2;

/// How a run that ended on a pull request reads under its header.
const PULL_REQUEST_OPEN: &str = "A pull request is open";

/// The phases one later step of a run adds after its workflow's own, read from the run's events.
type LaterPhases = fn(&[RunEvent]) -> Vec<PhaseRow>;

/// Every later step that appends phases, in order: the follow-up of a pull request the run
/// opened. Each adds none until its step has run.
const APPENDED: &[LaterPhases] = &[pull_request::phases];

/// The run's own workflow when `workflow` is `None`, else that nested one, or a workflow an
/// agent's own CLI runs. Before the main agent picks the run's own workflow,
/// `None` gives the one that runs first, Understand; a run that switched workflows, as a goal's
/// rounds or Captain at the plan approval do, shows the newest, as the engine follows it. `None`
/// when the events hold no such workflow.
pub(crate) fn workflow_view(
    events: &[RunEvent],
    run: &RunId,
    workflow: Option<&WorkflowId>,
) -> Option<WorkflowView> {
    view_from(&WorkflowLog::of(events), events, run, workflow)
}

/// Whether the run's own workflow is still being written: it started, and its phases aren't
/// known yet, so there's no view of it to open.
pub(crate) fn being_written(events: &[RunEvent]) -> bool {
    let log = WorkflowLog::of(events);
    log.newest_top()
        .and_then(|id| log.workflows.get(id))
        .is_some_and(|facts| !log.shape_known(facts))
}

/// One agent of a workflow its CLI runs itself, as the CLI last reported it. `None` when the
/// events hold no such workflow or agent.
pub(crate) fn inner_agent_view(
    events: &[RunEvent],
    run: &RunId,
    agent: &InnerAgent,
) -> Option<AgentView> {
    WorkflowLog::of(events).inner_agent_view(run, agent)
}

/// The view of `workflow` as `log` has the run.
fn view_from(
    log: &WorkflowLog,
    events: &[RunEvent],
    run: &RunId,
    workflow: Option<&WorkflowId>,
) -> Option<WorkflowView> {
    if let Some(id) = workflow
        && let Some(mut view) = log.agent_view(run, id)
    {
        // A workflow its CLI runs can have agents on goals too, as the run's own has.
        view.goals = super::goal_feed::agent_goals(events, &view.phases);
        return Some(view);
    }
    let id = match workflow {
        Some(id) => id.clone(),
        None => log.newest_top().or_else(|| log.order.first())?.clone(),
    };
    let facts = log.workflows.get(&id)?;
    // Nothing of a workflow still being written is drawn.
    if log.is_top(facts) && !log.shape_known(facts) {
        return None;
    }
    // The top of the log, the run's own workflow or Understand ahead of it, goes by the run's
    // name.
    let top = log.is_top(facts);
    let built = log.build(&id, 0)?;
    // The run's own workflow goes by the run's name, the one its panel row shows, and never
    // by the template it started from.
    let templates = [
        log.in_use.as_deref(),
        log.declared.get(&id).map(|declared| declared.name.as_str()),
        log.named
            .as_ref()
            .and_then(|named| named.based_on.as_deref()),
    ];
    let reported = log
        .reported_name(&id)
        .filter(|(name, _)| argonauts_core::text::names_the_run(name, &templates));
    let (name, description) = match (&log.named, top) {
        // A workflow its script writes names itself in its first report, as what it does.
        (_, true) if reported.is_some() => reported.clone().unwrap_or_default(),
        (Some(named), true) => (named.name.clone(), named.description.clone()),
        _ => match (log.reported_name(&id), log.declared.get(&id)) {
            // A workflow one of its steps runs says what it is better than its own phases do.
            (Some(reported), _) => reported,
            (None, Some(declared)) => (declared.name.clone(), declared.description.clone()),
            (None, None) => (
                facts.definition.name.clone(),
                facts.definition.description.clone(),
            ),
        },
    };
    // `orchestrator workflow`, after the run's own name: the workflow it started from, else
    // the one in use, as the run's Workflow item in the conversation says. Neither is said of
    // a run that goes by that name.
    let kind = |known: Option<&str>| {
        known
            .map(str::trim)
            .filter(|kind| !kind.is_empty() && *kind != name.trim())
            .map(argonauts_core::text::workflow_kind)
    };
    let started_from = log
        .named
        .as_ref()
        .and_then(|named| named.based_on.as_deref());
    let notes = kind(started_from)
        .or_else(|| kind(log.in_use.as_deref()))
        .filter(|_| top)
        .map(|kind| vec![kind])
        .unwrap_or_default();
    // The steps of yours the workflow itself says it holds. Those every workflow must hold
    // are marked by whoever answers with the view.
    let required = log
        .declared
        .get(&id)
        .map(|declared| declared.required.clone())
        .unwrap_or_default();
    let mut status_lines = log.status_lines(&id, top);
    let goal = top.then(|| super::goal::goal_view(events, run)).flatten();
    if let Some(goal) = &goal {
        status_lines.insert(0, goal_line(goal));
    }
    // A goal is a condition around the workflow: the view shows one once the run has taken it.
    let style = match goal {
        Some(_) => Some(argonauts_core::settings::workflows::RunStyle::Goal),
        None => log.named.as_ref().filter(|_| top).map(|named| named.style),
    };
    let mut view = WorkflowView {
        run: run.clone(),
        status_lines,
        style,
        id,
        name,
        description,
        agents_done: built.agents_done,
        agents_total: built.agents_total,
        elapsed_ms: built.elapsed_ms,
        spent: built.spent,
        goals: super::goal_feed::agent_goals(events, &built.phases),
        phases: built.phases,
        notes,
        required,
        worker: None,
        state: if top {
            log.state()
        } else {
            WorkflowState::Running
        },
    };
    if top {
        append_later_phases(&mut view, events);
        // A slug stays the run's id, and is drawn as its words.
        view.name = argonauts_core::text::title_words(&view.name);
        if let Some(own) = own::as_the_runs(&mut view, log, run) {
            // The mode and the workflow that really runs, then what your words asked of it:
            // `Captain · Claude Code's workflow fix-money-rounding · Codex reviews, as you asked`.
            let cli = DisplayNames::default().cli(view.worker.as_deref().unwrap_or_default());
            view.notes = vec!["Captain".to_owned(), format!("{cli}'s workflow {own}")];
            if let Some(reviewers) = &log.reviewers_asked {
                view.description = format!("{reviewers}, as you asked");
            }
            // A merged run's line tells the whole story: who built it, reviewed it and signed
            // it off.
            let merged = matches!(log.finished, Some((RunOutcome::Merged, _)));
            if let Some(who) = who_did_it(&view, log).filter(|_| merged)
                && let Some(ended) = view
                    .status_lines
                    .iter_mut()
                    .find(|line| line.kind == StatusKind::Ended)
            {
                ended.text = format!("{} · {who}", ended.text);
            }
            // While it runs, the line under the header follows the asks of the phase at work,
            // unless something needs you.
            if log.finished.is_none()
                && let Some(line) = asks_at_work(&view, log)
            {
                let quiet = view
                    .status_lines
                    .iter()
                    .all(|line| !matches!(line.kind, StatusKind::NeedsYou | StatusKind::Ended));
                view.status_lines
                    .retain(|line| line.kind != StatusKind::Note);
                if quiet {
                    view.status_lines
                        .push(StatusLine::new(StatusKind::Note, line));
                }
            }
        }
    }
    Some(view)
}

/// The phases [`APPENDED`] gives, after the workflow's own, with their agents counted in the
/// view's.
fn append_later_phases(view: &mut WorkflowView, events: &[RunEvent]) {
    for appended in APPENDED {
        for phase in appended(events) {
            if let Some(count) = phase.count {
                view.agents_done += count.done;
                view.agents_total += count.total;
            }
            view.phases.push(phase);
        }
    }
}

/// While a CLI's own workflow runs, what Argonauts Code's asks came to in the phase at work:
/// the latest verdict, then what each ask at work does, as `judge: GPT 6 Luna on Codex approved
/// Haiku 5.5's change · Gemini 3.8 Flash on agy signs off`. The change is the last one the
/// workflow's own agents built. `None` before any ask.
fn asks_at_work(view: &WorkflowView, log: &WorkflowLog) -> Option<String> {
    let names = DisplayNames::default();
    let host = view.worker.as_deref()?;
    let named = |worker: &ModelRef| match names.model(&worker.agent, worker.model.as_deref()) {
        Some(model) if worker.agent == host => model,
        Some(model) => format!("{model} on {}", names.cli(&worker.agent)),
        None => names.cli(&worker.agent),
    };
    let open = view
        .phases
        .iter()
        .position(|phase| phase.status == RowStatus::Running)?;
    fn agents(phases: &[PhaseRow]) -> Vec<&AgentRow> {
        phases
            .iter()
            .flat_map(|phase| &phase.rows)
            .filter_map(|row| match row {
                WorkflowRow::Agent(agent) => Some(agent),
                _ => None,
            })
            .collect()
    }
    let so_far = agents(&view.phases[..=open]);
    let change = so_far
        .iter()
        .rfind(|agent| !agent.answered && agent.inner.is_some() && agent.status == RowStatus::Done)
        .map(|built| format!("{}'s change", named(&built.worker)));
    let mut said = Vec::new();
    let given = so_far
        .iter()
        .rfind(|agent| agent.answered && agent.status == RowStatus::Done)
        .and_then(|agent| Some((agent, agent.note.as_ref()?)));
    if let Some((agent, verdict)) = given {
        // The verdict's `it` is the change, as in `sent it back`; else the change follows it.
        let did = match &change {
            Some(change) if verdict.text.split(' ').any(|word| word == "it") => verdict
                .text
                .split(' ')
                .map(|word| if word == "it" { change.as_str() } else { word })
                .collect::<Vec<_>>()
                .join(" "),
            Some(change) => format!("{} {change}", verdict.text),
            None => verdict.text.clone(),
        };
        let mut words = format!("{} {did}", named(&agent.worker));
        for then in &verdict.then {
            words.push_str(&format!(", {then}"));
        }
        said.push(words);
    }
    for agent in agents(&view.phases[open..=open])
        .into_iter()
        .filter(|agent| agent.answered && agent.status == RowStatus::Running)
    {
        let role = log
            .sessions
            .session(&agent.session)
            .map(|facts| facts.started.role);
        let does = match (role, &change) {
            (Some(RoleName::Judge), _) => "signs off".to_owned(),
            (_, Some(change)) => format!("reviews {change}"),
            (_, None) => "reviews".to_owned(),
        };
        said.push(format!("{} {does}", named(&agent.worker)));
    }
    (!said.is_empty()).then(|| {
        let phase = view.phases[open].name.trim().to_lowercase();
        format!("{phase}: {}", said.join(" · "))
    })
}

/// `Haiku 5.5 built it, GPT 6 Luna reviewed, Gemini 3.8 Flash signed off`: the models of a
/// CLI's own workflow that did its work, and those its reviews and sign-off were asked of.
fn who_did_it(view: &WorkflowView, log: &WorkflowLog) -> Option<String> {
    let names = DisplayNames::default();
    let mut built: Vec<String> = Vec::new();
    let mut reviewed: Vec<String> = Vec::new();
    let mut signed: Vec<String> = Vec::new();
    let agents = view
        .phases
        .iter()
        .flat_map(|phase| &phase.rows)
        .filter_map(|row| match row {
            WorkflowRow::Agent(agent) => Some(agent),
            _ => None,
        });
    for agent in agents {
        let role = log
            .sessions
            .session(&agent.session)
            .map(|facts| facts.started.role);
        let who = match (agent.answered, role) {
            (true, Some(RoleName::Judge)) => &mut signed,
            (true, _) => &mut reviewed,
            (false, _) if agent.inner.is_some() => &mut built,
            (false, _) => continue,
        };
        let worker = &agent.worker;
        let model = names
            .model(&worker.agent, worker.model.as_deref())
            .unwrap_or_else(|| names.cli(&worker.agent));
        if !who.contains(&model) {
            who.push(model);
        }
    }
    let said: Vec<String> = [
        (built, "built it"),
        (reviewed, "reviewed"),
        (signed, "signed off"),
    ]
    .into_iter()
    .filter(|(models, _)| !models.is_empty())
    .map(|(models, did)| format!("{} {did}", argonauts_core::text::listed(&models)))
    .collect();
    (!said.is_empty()).then(|| said.join(", "))
}

/// `Codex reviews`, when the run's request asked for its reviewers: the CLIs that review.
fn reviewers_asked(started: &RunStarted) -> Option<String> {
    let asked = started
        .instructions
        .iter()
        .any(|instruction| instruction.kind == InstructionKind::Review && instruction.followed());
    if !asked {
        return None;
    }
    let names = DisplayNames::default();
    let mut clis: Vec<String> = Vec::new();
    for worker in started.settings["roles"]["reviewer"]["models"].as_array()? {
        let Some(cli) = worker.as_str().and_then(|worker| worker.split('/').next()) else {
            continue;
        };
        let cli = names.cli(cli.trim());
        if !cli.is_empty() && !clis.contains(&cli) {
            clis.push(cli);
        }
    }
    match clis.as_slice() {
        [] => None,
        [one] => Some(format!("{one} reviews")),
        [rest @ .., last] => Some(format!("{} and {last} review", rest.join(", "))),
    }
}

/// The run's workflow, or the nested one named, as it is now, with the name it's saved under:
/// the run's own name for the run's workflow, made safe for a file name.
pub(crate) fn saved_workflow(
    events: &[RunEvent],
    workflow: Option<&WorkflowId>,
) -> Option<(String, Workflow)> {
    let log = WorkflowLog::of(events);
    let (id, facts) = match workflow {
        Some(id) => (id, log.workflows.get(id)?),
        None => {
            let id = log.newest_top()?;
            (id, log.workflows.get(id)?)
        }
    };
    let name = match (&log.named, facts.parent.is_none()) {
        (Some(named), true) => named.name.clone(),
        _ => log
            .declared
            .get(id)
            .map_or_else(|| facts.definition.name.clone(), |d| d.name.clone()),
    };
    let safe: String = name
        .trim()
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
                c.to_ascii_lowercase()
            } else {
                '-'
            }
        })
        .collect();
    let safe = safe.trim_matches('-').to_owned();
    let safe = if safe.is_empty() {
        "workflow".to_owned()
    } else {
        safe
    };
    Some((safe, facts.definition.clone()))
}

/// `Goal · 3 of 4 met · round 2 · not yet: median([]) raises ValueError`, the line the workflow
/// view keeps under its description while a goal goes round the run.
fn goal_line(goal: &argonauts_core::views::GoalView) -> StatusLine {
    use argonauts_core::events::goal::CriterionState;
    let met = goal
        .criteria
        .iter()
        .filter(|row| row.state == Some(CriterionState::Met))
        .count();
    let mut text = format!("Goal · {met} of {} met", goal.criteria.len());
    if let Some(last_try) = goal.tries.last() {
        text.push_str(&format!(" · round {}", last_try.n));
        if let Some(max) = goal.max_rounds {
            text.push_str(&format!(" of up to {max}"));
        }
    }
    if let Some(unmet) = goal
        .criteria
        .iter()
        .find(|row| row.state.is_some() && row.state != Some(CriterionState::Met))
    {
        text.push_str(&format!(" · not yet: {}", unmet.criterion.text.trim()));
    }
    StatusLine::new(StatusKind::Goal, text)
}

/// Every workflow the agents' own CLIs run in this run, newest state of each, with the session
/// that runs it, in the order they started.
pub(super) fn cli_workflow_runs(events: &[RunEvent]) -> Vec<(SessionKey, CliWorkflowRun)> {
    let mut found: Vec<(SessionKey, CliWorkflowRun)> = Vec::new();
    for event in events {
        let Some(session) = &event.session else {
            continue;
        };
        match &event.kind {
            RunEventKind::Agent(AgentEvent::Workflow { workflow }) => {
                let mut workflow = (**workflow).clone();
                let seen = epoch_ms(event.ts);
                match found.iter_mut().find(|(_, known)| known.id == workflow.id) {
                    Some((_, known)) => {
                        workflow.started_at_ms = known.started_at_ms.or(seen);
                        *known = workflow;
                    }
                    None => {
                        workflow.started_at_ms = seen;
                        found.push((session.clone(), workflow));
                    }
                }
            }
            // A workflow its CLI ran ends with it, whether or not the stream said so.
            RunEventKind::Session(SessionEvent::Ended(_)) => {
                for (_, workflow) in found.iter_mut().filter(|(key, _)| key == session) {
                    workflow.end_with_session("its session ended before the workflow did");
                }
            }
            _ => {}
        }
    }
    found
}

pub(super) fn epoch_ms(at: OffsetDateTime) -> Option<u64> {
    u64::try_from((at - OffsetDateTime::UNIX_EPOCH).whole_milliseconds()).ok()
}

/// How long until `at` by the clock on the wall, since a wait goes on while the run says
/// nothing.
fn ms_until(at: OffsetDateTime) -> u64 {
    u64::try_from((at - OffsetDateTime::now_utc()).whole_milliseconds()).unwrap_or(0)
}

/// An open approval, as the view's status line speaks of it. The question itself draws under
/// the view, so the line says only what waits.
#[derive(Debug, Clone)]
struct Asked {
    id: ApprovalId,
    /// `Claude Code is down · 2 tasks wait for it` for a CLI that is down, else the approval's
    /// title.
    says: String,
    /// When the CLI that is down is tested again.
    retried_at: Option<OffsetDateTime>,
}

impl Asked {
    fn of(opened: &ApprovalOpened) -> Self {
        let (says, retried_at) = match &opened.packet {
            ApprovalPacket::AgentDown(down) => {
                let cli = DisplayNames::default().cli(&down.agent_id.0);
                let paused = match down.paused.len() {
                    0 => String::new(),
                    1 => " · 1 task waits for it".to_owned(),
                    tasks => format!(" · {tasks} tasks wait for it"),
                };
                (format!("{cli} is down{paused}"), down.test_at)
            }
            _ => (opened.title.trim().to_owned(), None),
        };
        Self {
            id: opened.id.clone(),
            says,
            retried_at,
        }
    }
}

struct WorkflowFacts {
    /// The latest definition, after any approved change.
    definition: Workflow,
    parent: Option<StepRef>,
    started_at: OffsetDateTime,
}

/// A step's latest round: still running, or how it ended and when.
#[derive(Clone, Copy)]
enum StepProgress {
    Running,
    Ended(StepEnd, OffsetDateTime),
}

/// Where a declared phase is, as the workflow reported it.
#[derive(Clone)]
enum PhaseState {
    Started,
    Ended(StepEnd),
    Skipped,
}

struct WorkflowLog {
    order: Vec<WorkflowId>,
    workflows: BTreeMap<WorkflowId, WorkflowFacts>,
    steps: HashMap<StepRef, StepProgress>,
    /// The latest declaration of each workflow: the phases it says it has.
    declared: HashMap<WorkflowId, WorkflowDeclared>,
    phases: HashMap<(WorkflowId, u32), PhaseState>,
    named: Option<RunNamed>,
    /// The latest note, and the workflow it's about when it's about one. A skipped phase's
    /// reason is one, since the phase leaves the list.
    note: Option<(Option<WorkflowId>, String)>,
    /// Open approvals that wait for you, oldest first.
    asking: Vec<Asked>,
    /// Every open approval, oldest first: one auto couldn't settle, or any the run waiting on,
    /// waits for you too.
    open: Vec<Asked>,
    finished: Option<(RunOutcome, Option<String>)>,
    agent_ids: Vec<(SessionKey, CliWorkflowRun)>,
    /// The workflows the runner runs as steps, as each was last reported.
    reported: Vec<StepWorkflow>,
    sessions: RunFacts,
    /// Whether the run is paused and waits to be resumed.
    paused: bool,
    /// Why nothing could take a job the run needs, once it stopped fully until you carry it
    /// on.
    stopped: Option<String>,
    /// The jobs a workflow went on without, as each was left out.
    left_out: Vec<(OffsetDateTime, AgentLeftOut)>,
    /// The branch the run merged onto and the commit, once it did.
    merged: Option<(String, String)>,
    /// The workflow your settings had in use when a crew's run started.
    in_use: Option<String>,
    /// The whole change the latest merge question put up: its files and lines.
    change: Option<DiffStat>,
    /// Who reviews, when the run's request named its reviewers, such as `Codex reviews`.
    reviewers_asked: Option<String>,
}

/// A workflow's rows and totals, counted through every level below it.
struct Built {
    phases: Vec<PhaseRow>,
    agents_done: u32,
    agents_total: u32,
    elapsed_ms: u64,
    spent: Spend,
    workers: Vec<ModelRef>,
    status: RowStatus,
}

impl WorkflowLog {
    fn of(events: &[RunEvent]) -> Self {
        let mut log = Self {
            order: Vec::new(),
            workflows: BTreeMap::new(),
            steps: HashMap::new(),
            declared: HashMap::new(),
            phases: HashMap::new(),
            named: None,
            note: None,
            asking: Vec::new(),
            open: Vec::new(),
            finished: None,
            agent_ids: cli_workflow_runs(events),
            reported: Vec::new(),
            sessions: RunFacts::of(events),
            paused: false,
            stopped: None,
            left_out: Vec::new(),
            merged: None,
            in_use: None,
            change: None,
            reviewers_asked: None,
        };
        for event in events {
            match &event.kind {
                RunEventKind::Workflow(workflow) => log.note_workflow(workflow, event.ts),
                RunEventKind::Run(RunLifecycle::Named(named)) => log.named = Some(named.clone()),
                RunEventKind::Run(RunLifecycle::Started(started)) => {
                    log.reviewers_asked = reviewers_asked(started);
                    // Captain's own steps are no workflow of yours, whatever your settings name.
                    if started.mode == RunMode::Crew {
                        log.in_use = Some(started.workflow.clone());
                    }
                }
                RunEventKind::Run(RunLifecycle::Finished { outcome, detail }) => {
                    log.finished = Some((*outcome, detail.clone()));
                }
                // A new try: what the last one said of itself no longer stands.
                RunEventKind::Run(RunLifecycle::Resumed(_)) => {
                    log.paused = false;
                    log.stopped = None;
                    log.note = None;
                }
                RunEventKind::Merge(MergeEvent::RunMerged { target, sha, .. }) => {
                    log.merged = Some((target.clone(), sha.clone()));
                }
                RunEventKind::Approval(ApprovalEvent::Opened(opened)) => {
                    if let ApprovalPacket::Merge(merge) = &opened.packet {
                        log.change = Some(merge.combined);
                    }
                    log.open.push(Asked::of(opened));
                    if opened.needs_you {
                        log.ask(&opened.id);
                    }
                }
                // Auto couldn't settle it, so it's yours.
                RunEventKind::Approval(ApprovalEvent::Checked(check))
                    if !check.violations.is_empty() =>
                {
                    log.ask(&check.id);
                }
                RunEventKind::Run(RunLifecycle::Waiting { reason, waiting_on }) => {
                    log.paused = *reason == WaitReason::Paused;
                    log.stopped = match reason {
                        WaitReason::NoFallback { detail, stop, .. } => Some(
                            stop.as_ref()
                                .map_or(detail.as_str(), |stop| stop.why.as_str())
                                .trim()
                                .to_owned(),
                        ),
                        _ => None,
                    };
                    if *reason == WaitReason::Approval {
                        let waiting: Vec<ApprovalId> = if waiting_on.is_empty() {
                            log.open.iter().map(|open| open.id.clone()).collect()
                        } else {
                            waiting_on.clone()
                        };
                        for id in &waiting {
                            log.ask(id);
                        }
                    }
                }
                RunEventKind::Approval(ApprovalEvent::Decided(decided)) => {
                    log.asking.retain(|asked| asked.id != decided.id);
                    log.open.retain(|open| open.id != decided.id);
                }
                RunEventKind::Approval(ApprovalEvent::Withdrawn { id, .. }) => {
                    log.asking.retain(|asked| asked.id != *id);
                    log.open.retain(|open| open.id != *id);
                }
                _ => {}
            }
        }
        log
    }

    /// The open approval `id` waits for you, once.
    fn ask(&mut self, id: &ApprovalId) {
        if self.asking.iter().any(|asked| asked.id == *id) {
            return;
        }
        if let Some(open) = self.open.iter().find(|open| open.id == *id) {
            self.asking.push(open.clone());
        }
    }

    fn note_workflow(&mut self, workflow: &WorkflowEvent, at: OffsetDateTime) {
        match workflow {
            WorkflowEvent::Started(started) => {
                self.order.push(started.id.clone());
                self.workflows.insert(
                    started.id.clone(),
                    WorkflowFacts {
                        definition: started.workflow.clone(),
                        parent: started.parent.clone(),
                        started_at: at,
                    },
                );
            }
            WorkflowEvent::Changed(changed) => {
                if let Some(facts) = self.workflows.get_mut(&changed.id) {
                    facts.definition = changed.workflow.clone();
                }
            }
            WorkflowEvent::StepStarted(step) => {
                self.steps.insert(step.step.clone(), StepProgress::Running);
            }
            WorkflowEvent::StepFinished(step) => {
                self.steps
                    .insert(step.step.clone(), StepProgress::Ended(step.end, at));
            }
            WorkflowEvent::Declared(declared) => {
                self.declared
                    .insert(declared.id.clone(), (**declared).clone());
            }
            WorkflowEvent::PhaseStarted(mark) => {
                self.phases
                    .insert((mark.id.clone(), mark.index), PhaseState::Started);
            }
            WorkflowEvent::PhaseEnded { phase, end } => {
                self.phases
                    .insert((phase.id.clone(), phase.index), PhaseState::Ended(*end));
            }
            WorkflowEvent::PhaseSkipped { phase, why } => {
                self.phases
                    .insert((phase.id.clone(), phase.index), PhaseState::Skipped);
                let why = why.trim();
                let text = if why.is_empty() {
                    format!("{} skipped", phase.name.trim())
                } else {
                    format!("{} skipped · {why}", phase.name.trim())
                };
                self.note = Some((Some(phase.id.clone()), text));
            }
            WorkflowEvent::PhaseAdded { .. } => {}
            WorkflowEvent::Reported(report) => {
                let agent_sessions = report
                    .sessions
                    .iter()
                    .map(|known| (known.agent, known.session.clone()))
                    .collect();
                self.note_report(&report.step, &report.workflow, agent_sessions);
            }
            WorkflowEvent::Note(note) => {
                self.note = Some((note.workflow.clone(), note.text.trim().to_owned()));
            }
            WorkflowEvent::LeftOut(left) => self.left_out.push((at, left.clone())),
        }
    }

    /// Takes in what the runner reports of the workflow `step` runs: its name, its phases in
    /// the order it declares them, its agents, and in `agent_sessions` the session behind each
    /// agent that has one, by the agent's index. Each report is the whole state, so it replaces the
    /// last one of the same workflow.
    fn note_report(
        &mut self,
        step: &StepRef,
        workflow: &CliWorkflowRun,
        agent_sessions: Vec<(u32, SessionKey)>,
    ) {
        let report = StepWorkflow {
            step: step.clone(),
            workflow: workflow.clone(),
            agent_sessions,
        };
        match self
            .reported
            .iter_mut()
            .find(|known| known.workflow.id == workflow.id)
        {
            Some(known) => *known = report,
            None => self.reported.push(report),
        }
    }

    /// The name and description of the workflow a step of `id` runs, when it gave a name.
    fn reported_name(&self, id: &WorkflowId) -> Option<(String, String)> {
        let report = self
            .reported
            .iter()
            .find(|report| report.step.workflow == *id)?;
        let name = report.workflow.name.trim();
        (!name.is_empty()).then(|| (name.to_owned(), report.workflow.description.clone()))
    }

    /// A workflow an agent's own CLI runs, by the id the views give it, with the row of the
    /// session running it.
    fn cli_workflow(&self, id: &WorkflowId) -> Option<(AgentRow, &CliWorkflowRun)> {
        let (host, workflow) = self
            .agent_ids
            .iter()
            .find(|(_, workflow)| cli_workflow_run_id(&workflow.id) == *id)?;
        let host = self.sessions.agent_row(self.sessions.session(host)?);
        Some((host, workflow))
    }

    /// The view of a workflow an agent's own CLI runs.
    fn agent_view(&self, run: &RunId, id: &WorkflowId) -> Option<WorkflowView> {
        let (host, workflow) = self.cli_workflow(id)?;
        let now = self.sessions.last_ts.and_then(epoch_ms);
        let mut view = WorkflowView::of_agent(run, &host, workflow, now);
        super::asked::answered_by_the_crew(&mut view, &self.sessions, &host.session, workflow);
        Some(view)
    }

    fn inner_agent_view(&self, run: &RunId, agent: &InnerAgent) -> Option<AgentView> {
        let (host, workflow) = self.cli_workflow(&agent.workflow)?;
        let now = self.sessions.last_ts.and_then(epoch_ms);
        AgentView::of_inner(run, &host, workflow, agent.index, now)
    }

    /// Where the run stands, for the header's totals.
    fn state(&self) -> WorkflowState {
        match &self.finished {
            Some((RunOutcome::Merged | RunOutcome::PullRequest | RunOutcome::BranchLeft, _)) => {
                WorkflowState::Done
            }
            Some((RunOutcome::Stopped | RunOutcome::Abandoned, _)) => WorkflowState::Stopped,
            Some((RunOutcome::Failed, _)) => WorkflowState::Failed,
            None if self.paused => WorkflowState::Paused,
            None if self.stopped.is_some() => WorkflowState::Stopped,
            None => WorkflowState::Running,
        }
    }

    /// What the view says under its header: what waits for you, else how the run ended or why
    /// it stopped, else the latest note about this workflow since the run last went on. The
    /// words before the first ` · ` are the headline.
    fn status_lines(&self, id: &WorkflowId, top: bool) -> Vec<StatusLine> {
        if self.finished.is_none()
            && let Some(asked) = self.asking.last()
        {
            let more = match self.asking.len() {
                1 => String::new(),
                n => format!(" · and {} more", n - 1),
            };
            let line = StatusLine::new(StatusKind::NeedsYou, format!("{}{more}", asked.says));
            return vec![match asked.retried_at {
                Some(at) => line.counting("tried again in", ms_until(at)),
                None => line,
            }];
        }
        if let Some((outcome, detail)) = &self.finished
            && top
        {
            let (how, tone) = match outcome {
                RunOutcome::Merged => (self.merged_words(), Tone::Good),
                RunOutcome::PullRequest => (PULL_REQUEST_OPEN.to_owned(), Tone::Good),
                RunOutcome::BranchLeft => (self.kept_words(), Tone::Warn),
                RunOutcome::Abandoned => ("Abandoned".to_owned(), Tone::Bad),
                RunOutcome::Stopped => ("Stopped".to_owned(), Tone::Bad),
                RunOutcome::Failed => ("Failed".to_owned(), Tone::Bad),
            };
            let text = match detail.as_deref().map(str::trim) {
                Some(detail) if !detail.is_empty() => format!("{how} · {detail}"),
                _ => how,
            };
            // No commit of yours undoes it there, so the line says what does.
            let text = if *outcome == RunOutcome::Merged && self.written_into_your_folder() {
                format!("{text} · {PUT_BACK}")
            } else {
                text
            };
            return vec![StatusLine::new(StatusKind::Ended, text).toned(tone)];
        }
        // A run that stopped fully says why, as its item in the conversation does, in place
        // of what it noted before.
        if let Some(why) = self.stopped.as_ref().filter(|_| top) {
            let text = match why.as_str() {
                "" => "Stopped".to_owned(),
                why => format!("Stopped · {why}"),
            };
            return vec![StatusLine::new(StatusKind::Ended, text).toned(Tone::Bad)];
        }
        match &self.note {
            Some((about, text)) if about.as_ref().is_none_or(|about| about == id) => {
                vec![StatusLine::new(StatusKind::Note, text.clone())]
            }
            _ => Vec::new(),
        }
    }

    /// `Merged onto main · 3f9c2a1 · 4 files +52 −6`, from the merge the run's own steps
    /// record and the change its merge question put up, or `Merged`. A folder without git has
    /// no commit of yours to name: `Written into your folder · 4 files +52 −6`.
    fn merged_words(&self) -> String {
        let Some((target, sha)) = &self.merged else {
            return "Merged".to_owned();
        };
        let into = MergedInto::of(target);
        let mut said = vec![into.heading()];
        if matches!(into, MergedInto::Branch(_)) {
            said.push(sha.chars().take(7).collect());
        }
        if let Some(change) = self.change.filter(|change| !change.is_empty()) {
            said.push(changed(&change));
        }
        said.join(" · ")
    }

    /// `Kept on its branch, not merged · 4 files +105 −4`: the change the merge question put up,
    /// which is still on the run's branch.
    fn kept_words(&self) -> String {
        let mut said = vec!["Kept on its branch, not merged".to_owned()];
        if let Some(change) = self.change.filter(|change| !change.is_empty()) {
            said.push(changed(&change));
        }
        said.join(" · ")
    }

    /// Whether the run's work was written into a folder without git.
    fn written_into_your_folder(&self) -> bool {
        self.merged
            .as_ref()
            .is_some_and(|(target, _)| MergedInto::of(target) == MergedInto::YourFolder)
    }

    /// Whether one of the run's own reviewers sent the work back and its next round is still
    /// to come, which keeps it from counting as done.
    fn waits_for_its_round(&self, session: &SessionFacts) -> bool {
        self.finished.is_none() && self.sessions.verdicts.sent_back(session)
    }

    /// Whether `facts` is the top of what the log holds: the run's own workflow, or before the
    /// main agent picks it, Understand, which runs ahead of it.
    /// The run's own workflow: the newest that started with no parent.
    fn newest_top(&self) -> Option<&WorkflowId> {
        self.order.iter().rev().find(|id| {
            self.workflows
                .get(*id)
                .is_some_and(|facts| facts.parent.is_none())
        })
    }

    fn is_top(&self, facts: &WorkflowFacts) -> bool {
        facts
            .parent
            .as_ref()
            .is_none_or(|parent| !self.workflows.contains_key(&parent.workflow))
    }

    /// Whether a workflow at the top has its shape yet ([`shape_known`]): from what its script
    /// step reported, and from the CLI workflows its tasks' sessions ran.
    fn shape_known(&self, facts: &WorkflowFacts) -> bool {
        let tasks_own = self
            .agent_ids
            .iter()
            .filter(|(host, _)| {
                self.sessions
                    .session(host)
                    .is_some_and(|session| session.task.is_some())
            })
            .map(|(_, workflow)| workflow);
        shape_known(
            writes_its_phases(&facts.definition),
            self.reported
                .iter()
                .map(|report| &report.workflow)
                .chain(tasks_own),
        )
    }

    /// Whether the workflow is over: the run finished, or the step that started a nested one
    /// ended.
    fn ended(&self, facts: &WorkflowFacts) -> bool {
        match &facts.parent {
            Some(parent) => matches!(self.steps.get(parent), Some(StepProgress::Ended(..))),
            None => self.sessions.finished_at.is_some(),
        }
    }

    fn build(&self, id: &WorkflowId, depth: usize) -> Option<Built> {
        let facts = self.workflows.get(id)?;
        let mut built = Built {
            phases: Vec::new(),
            agents_done: 0,
            agents_total: 0,
            elapsed_ms: self.elapsed(facts),
            spent: Spend::default(),
            workers: Vec::new(),
            status: RowStatus::Waiting,
        };
        let ended = self.ended(facts);
        let now_ms = self.sessions.last_ts.and_then(epoch_ms);
        for (index, phase) in facts.definition.phases.iter().enumerate() {
            let index = u32::try_from(index).unwrap_or(u32::MAX);
            let mark = self.phases.get(&(id.clone(), index));
            // A skipped phase leaves the list, and its reason shows as the note.
            if matches!(mark, Some(PhaseState::Skipped)) {
                continue;
            }
            let report = self
                .reported
                .iter()
                .find(|report| report.runs_in(id, index));
            let counted = (built.agents_done, built.agents_total);
            let mut rows: Vec<(OffsetDateTime, WorkflowRow)> = Vec::new();
            // The sessions this phase's own step started, which a workflow that step runs
            // doesn't count among its agents.
            let mut own: Vec<SessionKey> = Vec::new();
            // Only the agents the workflow ran are its rows: an estimate or the run review, which
            // no step started, isn't one.
            for session in &self.sessions.sessions {
                let started = &session.started;
                let here = started
                    .workflow_step
                    .as_ref()
                    .is_some_and(|step| &step.workflow == id && step.phase == index);
                if !here || !is_workflow_agent(started.step, started.workflow_step.as_ref()) {
                    continue;
                }
                let row = self.sessions.agent_row(session);
                add_spend(&mut built.spent, &row.spent);
                remember(&mut built.workers, &row.worker);
                // An agent of the workflow this phase's step runs is a row of that workflow's
                // own phase, below.
                if report.is_some_and(|report| report.holds(&row)) {
                    continue;
                }
                built.agents_total += 1;
                if row.status == RowStatus::Done && !self.waits_for_its_round(session) {
                    built.agents_done += 1;
                }
                own.push(row.session.clone());
                rows.push((session.started_at, WorkflowRow::Agent(row)));
            }
            if depth < DEEPEST {
                for (child_id, child) in &self.workflows {
                    let placed_here = child
                        .parent
                        .as_ref()
                        .is_some_and(|parent| &parent.workflow == id && parent.phase == index);
                    let Some(nested) = placed_here
                        .then(|| self.build(child_id, depth + 1))
                        .flatten()
                    else {
                        continue;
                    };
                    built.agents_done += nested.agents_done;
                    built.agents_total += nested.agents_total;
                    add_spend(&mut built.spent, &nested.spent);
                    for worker in &nested.workers {
                        remember(&mut built.workers, worker);
                    }
                    let name = self
                        .declared
                        .get(child_id)
                        .map_or_else(|| child.definition.name.clone(), |d| d.name.clone());
                    let row = NestedWorkflowRow {
                        id: child_id.clone(),
                        name,
                        status: nested.status,
                        agents_done: nested.agents_done,
                        agents_total: nested.agents_total,
                        workers: nested.workers,
                        elapsed_ms: nested.elapsed_ms,
                        parent: None,
                        worker: None,
                        phases: nested.phases,
                        rows: Vec::new(),
                    };
                    rows.push((child.started_at, WorkflowRow::Workflow(row)));
                }
            }
            // A job the workflow went on without is a row of its phase all the same.
            for (at, left) in &self.left_out {
                if left.step.workflow == *id && left.step.phase == index {
                    rows.push((*at, WorkflowRow::Agent(left_out_row(left))));
                }
            }
            rows.sort_by_key(|(started, _)| *started);
            let mut rows: Vec<WorkflowRow> = rows.into_iter().map(|(_, row)| row).collect();
            let reported = report.map(|report| report.rows(&self.sessions, phase.standard));
            if let Some(reported) = &reported {
                rows.extend(reported.loose.iter().cloned().map(WorkflowRow::Agent));
                built.agents_done += reported.agents_done;
                built.agents_total += reported.agents_total;
            }
            let waits = |row: &AgentRow| {
                self.sessions
                    .session(&row.session)
                    .is_some_and(|session| self.waits_for_its_round(session))
            };
            let (rows, folded) = tries::fold_counting(rows, &self.sessions, &waits);
            let rows = inner::under_agents(rows, self, now_ms);
            built.agents_total = built.agents_total.saturating_sub(folded.tries);
            built.agents_done = built.agents_done.saturating_sub(folded.done);
            // The agents of a workflow an agent's own CLI runs are the run's agents too.
            let (inner_done, inner_total) = inner::counted(&rows);
            built.agents_done += inner_done;
            built.agents_total += inner_total;
            let steps: Vec<Option<StepProgress>> = (0..phase.steps.len())
                .map(|step| {
                    let step = u32::try_from(step).unwrap_or(u32::MAX);
                    self.steps
                        .get(&StepRef {
                            workflow: id.clone(),
                            phase: index,
                            path: vec![step],
                        })
                        .copied()
                })
                .collect();
            // Only agents are rows, as in Claude Code's view: what the run's own code did, its
            // checks and the merge, is the Workflow row's and the answer's to say. So once the
            // workflow is over, a phase with no agent leaves the list. Until then every phase
            // is listed, the merge after the phases a script wrote among them.
            let replaced = reported
                .as_ref()
                .is_some_and(|reported| !reported.phases.is_empty());
            if ended && rows.is_empty() && !replaced {
                continue;
            }
            // The phases of the workflow its step runs take the place of this one, and that
            // workflow's agents are the view's: the session that wrote its steps is none of
            // them, so it is no row here and no count, in the header or in any phase.
            if let Some(reported) = reported.filter(|reported| !reported.phases.is_empty()) {
                built.agents_done = counted.0 + reported.agents_done;
                built.agents_total = counted.1 + reported.agents_total;
                let mut phases = reported.phases;
                let wrote_the_steps = |row: &WorkflowRow| {
                    matches!(row, WorkflowRow::Agent(agent)
                        if agent.inner.is_none() && own.contains(&agent.session))
                };
                let mut rows = rows;
                rows.retain(|row| !wrote_the_steps(row));
                if let Some(first) = phases.first_mut() {
                    // An agent the workflow put in no phase of its own shows in its first, and
                    // counts there, so a phase never lists more agents than its count says.
                    let loose = || rows.iter().filter_map(agent_status);
                    let total = u32::try_from(loose().count()).unwrap_or(u32::MAX);
                    if total > 0 {
                        let done = loose().filter(|status| *status == RowStatus::Done).count();
                        let done = u32::try_from(done).unwrap_or(u32::MAX);
                        let (was_done, was_total) = first
                            .count
                            .map_or((0, 0), |count| (count.done, count.total));
                        first.count = PhaseCount::of(was_done + done, was_total + total);
                    }
                    first.rows.splice(0..0, rows);
                }
                for phase in &mut phases {
                    let (rows, folded) =
                        tries::fold(std::mem::take(&mut phase.rows), &self.sessions);
                    phase.rows = inner::under_agents(rows, self, now_ms);
                    if folded.tries > 0 {
                        built.agents_total = built.agents_total.saturating_sub(folded.tries);
                        built.agents_done = built.agents_done.saturating_sub(folded.done);
                        phase.count = phase.count.and_then(|count| {
                            PhaseCount::of(
                                count.done.saturating_sub(folded.done),
                                count.total.saturating_sub(folded.tries),
                            )
                        });
                    }
                }
                built.phases.extend(phases);
                continue;
            }
            // Done of the agents started so far, those of a workflow inside among them, and
            // nothing before the first one, so a phase that hasn't started never reads 0/0. The
            // run's own steps aren't agents.
            let (done, total) = agents_counted(&rows, waits);
            let count = PhaseCount::of(done, total);
            let status = match mark {
                Some(PhaseState::Ended(StepEnd::Failed)) => RowStatus::Failed,
                Some(PhaseState::Ended(_)) if !has_status(&rows, RowStatus::NeedsYou) => {
                    RowStatus::Done
                }
                _ => phase_status(&rows, &steps),
            };
            let named = self
                .declared
                .get(id)
                .and_then(|declared| declared.phases.iter().find(|p| p.index == index));
            built.phases.push(PhaseRow {
                name: named.map_or_else(|| phase.name.clone(), |named| named.name.clone()),
                detail: named.map_or_else(|| phase.detail.clone(), |named| named.detail.clone()),
                standard: phase.standard,
                status,
                try_number: None,
                count,
                note: self.waits_on(&rows),
                rows,
            });
        }
        for phase in &mut built.phases {
            if phase.status == RowStatus::Done
                && let Some(status) = self.sent_back_for_good(&phase.rows)
            {
                phase.status = status;
            }
        }
        built.status = overall(&built.phases);
        Some(built)
    }

    /// A phase whose review sent the work back with no round left isn't done: the work waits
    /// for you while you're asked about it, and otherwise its review didn't pass. Once the
    /// task merged anyway, the phase is as its rows are.
    fn sent_back_for_good(&self, rows: &[WorkflowRow]) -> Option<RowStatus> {
        let facts = &self.sessions;
        let task = rows.iter().find_map(|row| match row {
            WorkflowRow::Agent(agent) if agent.inner.is_none() => {
                let session = facts.session(&agent.session)?;
                let left = facts.verdicts.no_round_followed(facts, session);
                left.then(|| session.task.clone()).flatten()
            }
            _ => None,
        })?;
        if facts.verdicts.merged(&task) {
            return None;
        }
        let asked = facts
            .waiting
            .values()
            .any(|about| about.as_ref() == Some(&task));
        Some(if asked {
            RowStatus::NeedsYou
        } else {
            RowStatus::Failed
        })
    }

    /// What a phase's rows wait for: the work a reviewer among them sent back, while its
    /// worker is on the next round.
    fn waits_on(&self, rows: &[WorkflowRow]) -> Option<String> {
        rows.iter().find_map(|row| match row {
            WorkflowRow::Agent(agent) => {
                let reviewer = self.sessions.session(&agent.session)?;
                self.sessions.verdicts.waits_on(&self.sessions, reviewer)
            }
            _ => None,
        })
    }

    /// Until the step that started a nested workflow ended, or the run finished, or the log's
    /// last event.
    fn elapsed(&self, facts: &WorkflowFacts) -> u64 {
        let ended = match &facts.parent {
            Some(parent) => match self.steps.get(parent) {
                Some(StepProgress::Ended(_, at)) => Some(*at),
                _ => None,
            },
            None => self.sessions.finished_at,
        };
        match ended {
            Some(ended) => {
                u64::try_from((ended - facts.started_at).whole_milliseconds()).unwrap_or(0)
            }
            None => self.sessions.since(facts.started_at),
        }
    }
}

/// `4 files +52 −6`: what a merge changed.
fn changed(change: &DiffStat) -> String {
    let files = match change.files_changed {
        1 => "1 file".to_owned(),
        files => format!("{files} files"),
    };
    format!(
        "{files} +{} \u{2212}{}",
        change.insertions, change.deletions
    )
}

fn remember(workers: &mut Vec<ModelRef>, worker: &ModelRef) {
    if !workers.contains(worker) {
        workers.push(worker.clone());
    }
}

/// An agent row's status. `None` for a step Argonauts Code ran or a nested workflow.
fn agent_status(row: &WorkflowRow) -> Option<RowStatus> {
    match row {
        WorkflowRow::Agent(agent) => Some(agent.status),
        _ => None,
    }
}

fn row_status(row: &WorkflowRow) -> RowStatus {
    match row {
        WorkflowRow::Agent(agent) => agent.status,
        WorkflowRow::Workflow(nested) => nested.status,
        WorkflowRow::Command(command) => command.status,
    }
}

fn has_status(rows: &[WorkflowRow], status: RowStatus) -> bool {
    rows.iter().any(|row| row_status(row) == status)
}

/// The row of a job no model could take, which its workflow goes on without: the model it
/// was meant for and why, grey. Nothing ran, so it has no session, tokens or time, and no
/// count counts it.
fn left_out_row(left: &AgentLeftOut) -> AgentRow {
    let key: String = format!("left-out-{}", left.label)
        .chars()
        .map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
        .take(64)
        .collect();
    AgentRow {
        session: SessionKey::parse(&key).unwrap_or_else(|| SessionKey::numbered(0)),
        label: left.label.clone(),
        worker: left.meant_for.clone(),
        status: RowStatus::Skipped,
        tokens: 0,
        spent: Spend::default(),
        elapsed_ms: 0,
        subagents: 0,
        context: None,
        fallback_for: None,
        answered: false,
        tries: Vec::new(),
        note: Some(RowNote::new(left.why.clone(), Tone::Plain)),
        inner: None,
        left_out: true,
        fallback_why: None,
    }
}

/// The job left out whose row `session` keys, with that row, for the agent view it opens.
pub(super) fn left_out_job(
    events: &[RunEvent],
    session: &SessionKey,
) -> Option<(AgentRow, AgentLeftOut)> {
    events.iter().rev().find_map(|event| match &event.kind {
        RunEventKind::Workflow(WorkflowEvent::LeftOut(left)) => {
            let row = left_out_row(left);
            (row.session == *session).then(|| (row, left.clone()))
        }
        _ => None,
    })
}

fn phase_status(rows: &[WorkflowRow], steps: &[Option<StepProgress>]) -> RowStatus {
    let statuses: Vec<RowStatus> = rows.iter().map(row_status).collect();
    if statuses.contains(&RowStatus::NeedsYou) {
        return RowStatus::NeedsYou;
    }
    let step_running = steps
        .iter()
        .any(|step| matches!(step, Some(StepProgress::Running)));
    if statuses.contains(&RowStatus::Running) || step_running {
        return RowStatus::Running;
    }
    let started = steps.iter().filter(|step| step.is_some()).count();
    if started == 0 && rows.is_empty() {
        return RowStatus::Waiting;
    }
    if started < steps.len() {
        return RowStatus::Running;
    }
    let step_failed = steps
        .iter()
        .any(|step| matches!(step, Some(StepProgress::Ended(StepEnd::Failed, _))));
    let all_rows_failed = !statuses.is_empty() && statuses.iter().all(|s| *s == RowStatus::Failed);
    if step_failed || (steps.is_empty() && all_rows_failed) {
        RowStatus::Failed
    } else {
        RowStatus::Done
    }
}

fn overall(phases: &[PhaseRow]) -> RowStatus {
    let statuses: Vec<RowStatus> = phases.iter().map(|phase| phase.status).collect();
    if statuses.contains(&RowStatus::NeedsYou) {
        RowStatus::NeedsYou
    } else if statuses.contains(&RowStatus::Running) {
        RowStatus::Running
    } else if statuses.contains(&RowStatus::Failed) {
        RowStatus::Failed
    } else if !statuses.is_empty() && statuses.iter().all(|s| *s == RowStatus::Done) {
        RowStatus::Done
    } else if statuses.contains(&RowStatus::Done) {
        RowStatus::Running
    } else {
        RowStatus::Waiting
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::test_support::{event, run_id, session_ended, session_started, usage};
    use argonauts_core::events::session::{EndReason, SessionEvent};
    use argonauts_core::events::workflow::PhaseMark;
    use argonauts_core::events::workflow::{StepFinished, StepStarted, WorkflowStarted};
    use argonauts_core::names::{RoleName, SessionKey};
    use argonauts_core::settings::workflows::RunStyle;
    use argonauts_core::workflow::{StandardPhase, StepKind};

    fn definition(value: serde_json::Value) -> Workflow {
        serde_json::from_value(value).unwrap()
    }

    fn step(workflow: &WorkflowId, phase: u32, index: u32) -> StepRef {
        StepRef {
            workflow: workflow.clone(),
            phase,
            path: vec![index],
        }
    }

    fn started(seq: u64, id: &WorkflowId, parent: Option<StepRef>, workflow: Workflow) -> RunEvent {
        let started = WorkflowStarted {
            id: id.clone(),
            from: None,
            version: "v1".to_owned(),
            parent,
            workflow,
        };
        event(
            seq,
            RunEventKind::Workflow(WorkflowEvent::Started(Box::new(started))),
        )
    }

    fn step_started(seq: u64, at: StepRef, kind: StepKind, standard: StandardPhase) -> RunEvent {
        let step = StepStarted {
            step: at,
            kind,
            standard,
            round: 1,
        };
        event(
            seq,
            RunEventKind::Workflow(WorkflowEvent::StepStarted(step)),
        )
    }

    fn step_finished(seq: u64, at: StepRef, kind: StepKind, standard: StandardPhase) -> RunEvent {
        let step = StepFinished {
            step: at,
            kind,
            standard,
            round: 1,
            end: StepEnd::Done,
            summary: None,
        };
        event(
            seq,
            RunEventKind::Workflow(WorkflowEvent::StepFinished(step)),
        )
    }

    fn placed(
        seq: u64,
        session: &SessionKey,
        role: RoleName,
        worker: &str,
        at: StepRef,
    ) -> RunEvent {
        let mut event = session_started(seq, session, None, role, worker);
        if let RunEventKind::Session(SessionEvent::Started(started)) = &mut event.kind {
            started.workflow_step = Some(at);
        }
        event
    }

    /// A run that switched workflows, as a goal's rounds do after the plan, is drawn as the
    /// newest one: its builds and checks, not the workflow whose plan came before them.
    #[test]
    fn a_run_that_switched_workflows_shows_the_newest() {
        let first = WorkflowId::numbered(1);
        let rounds = WorkflowId::numbered(2);
        let session = SessionKey::numbered(1);
        let events = vec![
            started(
                0,
                &first,
                None,
                definition(serde_json::json!({
                    "name": "fix-slugify",
                    "phases": [
                        {"name": "Plan", "standard": "plan", "steps": [{"kind": "draft"}]},
                        {"name": "Build", "standard": "do", "steps": [{"kind": "build"}]},
                        {"name": "Merge", "standard": "decide", "steps": [{"kind": "merge"}]}
                    ]
                })),
            ),
            step_started(1, step(&first, 0, 0), StepKind::Draft, StandardPhase::Plan),
            step_finished(2, step(&first, 0, 0), StepKind::Draft, StandardPhase::Plan),
            started(
                3,
                &rounds,
                None,
                definition(serde_json::json!({
                    "name": "fix-slugify",
                    "phases": [
                        {"name": "Build", "standard": "do", "steps": [{"kind": "build"}]},
                        {"name": "Check", "standard": "check", "steps": [{"kind": "review"}]}
                    ]
                })),
            ),
            step_started(4, step(&rounds, 0, 0), StepKind::Build, StandardPhase::Do),
            placed(
                5,
                &session,
                RoleName::Worker,
                "opencode",
                step(&rounds, 0, 0),
            ),
        ];
        let view = workflow_view(&events, &run_id(), None).expect("the run's view");
        assert_eq!(view.id, rounds);
        let phases: Vec<&str> = view
            .phases
            .iter()
            .map(|phase| phase.name.as_str())
            .collect();
        assert_eq!(phases, ["Build", "Check"]);
        assert_eq!(view.phases[0].status, RowStatus::Running);
        assert!(!being_written(&events));
        // The first one is still there by its id.
        let earlier = workflow_view(&events, &run_id(), Some(&first)).expect("the first one");
        assert_eq!(earlier.id, first);
    }

    #[test]
    fn workflow_view_built_from_events() {
        let top = WorkflowId::numbered(1);
        let review = WorkflowId::numbered(2);
        let look = step(&top, 0, 0);
        let nested_step = step(&top, 0, 1);
        let reviewing = step(&review, 0, 0);
        let (s1, s2, s3) = (
            SessionKey::numbered(1),
            SessionKey::numbered(2),
            SessionKey::numbered(3),
        );
        let understand = StandardPhase::Understand;
        let events = vec![
            started(
                0,
                &top,
                None,
                definition(serde_json::json!({
                    "name": "council",
                    "description": "Agents check each other",
                    "phases": [
                        {"name": "Look", "standard": "understand",
                         "steps": [{"kind": "read"}, {"kind": "workflow", "workflow": "review"}]},
                        {"name": "Build", "standard": "do", "steps": [{"kind": "build"}]}
                    ]
                })),
            ),
            step_started(1, look.clone(), StepKind::Read, understand),
            placed(2, &s1, RoleName::Reader, "claude-code", look.clone()),
            placed(3, &s2, RoleName::Reader, "codex", look),
            usage(4, &s1, 800, 200, 0.1),
            session_ended(5, &s1, EndReason::Completed, 1_000),
            step_started(6, nested_step.clone(), StepKind::Workflow, understand),
            started(
                7,
                &review,
                Some(nested_step.clone()),
                definition(serde_json::json!({
                    "name": "review",
                    "phases": [{"name": "Check", "standard": "check", "steps": [{"kind": "review"}]}]
                })),
            ),
            step_started(8, reviewing.clone(), StepKind::Review, StandardPhase::Check),
            placed(
                9,
                &s3,
                RoleName::Reviewer,
                "antigravity-cli",
                reviewing.clone(),
            ),
            session_ended(10, &s3, EndReason::Completed, 2_000),
            step_finished(11, reviewing, StepKind::Review, StandardPhase::Check),
            step_finished(12, nested_step, StepKind::Workflow, understand),
        ];

        let view = workflow_view(&events, &run_id(), None).unwrap();
        assert_eq!(
            (view.name.as_str(), view.id.clone()),
            ("council", top.clone())
        );
        assert_eq!((view.agents_done, view.agents_total), (2, 3));
        assert_eq!(view.elapsed_ms, 12_000);
        assert_eq!(
            view.spent.input_tokens, 0,
            "ended sessions count their final spend"
        );

        let look = &view.phases[0];
        assert_eq!(look.status, RowStatus::Running);
        assert_eq!(look.count, PhaseCount::of(2, 3));
        let WorkflowRow::Workflow(nested) = &look.rows[2] else {
            panic!("the nested workflow is the last row");
        };
        assert_eq!(nested.name, "review");
        assert_eq!(nested.status, RowStatus::Done);
        assert_eq!((nested.agents_done, nested.agents_total), (1, 1));
        assert_eq!(nested.workers[0].agent, "antigravity-cli");
        assert_eq!(nested.elapsed_ms, 5_000);
        assert_eq!(
            nested.phases.first().map(|phase| phase.status),
            Some(RowStatus::Done),
            "its phases open under its row"
        );

        let build = &view.phases[1];
        assert_eq!(build.status, RowStatus::Waiting);
        assert_eq!((build.count, build.rows.len()), (None, 0));

        let inner = workflow_view(&events, &run_id(), Some(&review)).unwrap();
        assert_eq!(inner.phases[0].status, RowStatus::Done);
        assert!(workflow_view(&events, &run_id(), Some(&WorkflowId::numbered(9))).is_none());
        assert!(workflow_view(&[], &run_id(), None).is_none());
    }

    fn captain() -> Workflow {
        definition(serde_json::json!({
            "name": "captain",
            "description": "The main agent alone",
            "phases": [
                {"name": "Understand", "standard": "understand", "steps": [{"kind": "read"}]},
                {"name": "Build", "standard": "do", "steps": [{"kind": "build"}]},
                {"name": "Review", "standard": "check", "steps": [{"kind": "review"}]},
                {"name": "Merge", "standard": "decide", "steps": [{"kind": "merge"}]}
            ]
        }))
    }

    fn workflow_event(seq: u64, workflow: WorkflowEvent) -> RunEvent {
        event(seq, RunEventKind::Workflow(workflow))
    }

    fn mark(id: &WorkflowId, index: u32, title: &str) -> PhaseMark {
        PhaseMark {
            id: id.clone(),
            index,
            name: title.to_owned(),
        }
    }

    /// Only the agents the workflow ran are rows: the run review, which no step
    /// started, isn't one, and a phase never counts what hasn't started.
    #[test]
    fn only_the_agents_the_workflow_ran_are_rows() {
        let top = WorkflowId::numbered(1);
        let (build, merge) = (step(&top, 1, 0), step(&top, 3, 0));
        let (worker, run_review) = (SessionKey::numbered(1), SessionKey::numbered(2));
        let events = vec![
            started(0, &top, None, captain()),
            step_started(1, build.clone(), StepKind::Build, StandardPhase::Do),
            placed(2, &worker, RoleName::Worker, "claude-code", build.clone()),
            session_ended(3, &worker, EndReason::Completed, 1_000),
            step_finished(4, build, StepKind::Build, StandardPhase::Do),
            step_started(5, merge.clone(), StepKind::Merge, StandardPhase::Decide),
            step_finished(6, merge, StepKind::Merge, StandardPhase::Decide),
            session_started(7, &run_review, None, RoleName::RunReview, "claude-code"),
            session_ended(8, &run_review, EndReason::Completed, 1_000),
        ];
        let view = workflow_view(&events, &run_id(), None).unwrap();
        assert_eq!((view.agents_done, view.agents_total), (1, 1));
        let shown: Vec<(&str, Option<PhaseCount>)> = view
            .phases
            .iter()
            .map(|phase| (phase.name.as_str(), phase.count))
            .collect();
        assert_eq!(
            shown,
            [
                ("Understand", None),
                ("Build", PhaseCount::of(1, 1)),
                ("Review", None),
                ("Merge", None)
            ]
        );
        assert!(view.phases.iter().all(|phase| phase.rows.len() <= 1));
    }

    /// The run's own name heads its workflow, never the template's. A skipped phase leaves the
    /// list with its reason as the note, and once the run ends, so does a phase that never ran.
    #[test]
    fn the_view_draws_only_what_the_workflow_declares() {
        let top = WorkflowId::numbered(1);
        let build = step(&top, 1, 0);
        let worker = SessionKey::numbered(1);
        let mut events = vec![
            started(0, &top, None, captain()),
            event(
                1,
                RunEventKind::Run(RunLifecycle::Named(RunNamed {
                    name: "fix-money-rounding".to_owned(),
                    description: "Round invoice totals half up, with tests".to_owned(),
                    style: RunStyle::Workflow,
                    based_on: Some("captain".to_owned()),
                    done_when: Vec::new(),
                })),
            ),
            workflow_event(
                2,
                WorkflowEvent::PhaseSkipped {
                    phase: mark(&top, 0, "Understand"),
                    why: "Your prompt is the goal".to_owned(),
                },
            ),
            workflow_event(3, WorkflowEvent::PhaseStarted(mark(&top, 1, "Build"))),
            step_started(4, build.clone(), StepKind::Build, StandardPhase::Do),
            placed(5, &worker, RoleName::Worker, "claude-code", build.clone()),
        ];
        let view = workflow_view(&events, &run_id(), None).unwrap();
        // The run's slug, drawn as its words.
        assert_eq!(view.name, "Fix money rounding");
        assert_eq!(view.description, "Round invoice totals half up, with tests");
        let names: Vec<&str> = view.phases.iter().map(|p| p.name.as_str()).collect();
        assert_eq!(names, ["Build", "Review", "Merge"]);
        assert_eq!(
            view.status_lines,
            [StatusLine::new(
                StatusKind::Note,
                "Understand skipped · Your prompt is the goal"
            )]
        );

        events.extend([
            session_ended(6, &worker, EndReason::Completed, 1_000),
            step_finished(7, build, StepKind::Build, StandardPhase::Do),
            workflow_event(
                8,
                WorkflowEvent::PhaseEnded {
                    phase: mark(&top, 1, "Build"),
                    end: StepEnd::Done,
                },
            ),
            event(
                9,
                RunEventKind::Run(RunLifecycle::Finished {
                    outcome: RunOutcome::Merged,
                    detail: None,
                }),
            ),
        ]);
        let view = workflow_view(&events, &run_id(), None).unwrap();
        let names: Vec<&str> = view.phases.iter().map(|p| p.name.as_str()).collect();
        assert_eq!(names, ["Build"]);
        assert_eq!(view.phases[0].status, RowStatus::Done);
        assert_eq!(view.status_lines[0].kind, StatusKind::Ended);
    }

    /// A workflow Claude Code runs itself draws from its own reports, with its own names.
    #[test]
    fn a_agents_own_workflow_is_drawn_from_its_reports() {
        use argonauts_core::events::session::{
            CliAgent, CliAgentState, CliPhase, CliWorkflowRun, SubagentStatus,
        };
        let host = SessionKey::numbered(1);
        let agent = |index: u32, label: &str, phase: u32, state: CliAgentState| CliAgent {
            index,
            label: label.to_owned(),
            phase: Some(phase),
            model: Some("claude-haiku-4-5-20251001".to_owned()),
            state,
            tokens: 10_311,
            tool_calls: 0,
            duration_ms: 3_266,
            started_at_ms: None,
            try_number: 1,
            prompt: None,
            result: None,
            ..CliAgent::default()
        };
        let workflow = CliWorkflowRun {
            id: "w4d273qmn".to_owned(),
            call: None,
            name: "probe".to_owned(),
            description: "Two-phase workflow".to_owned(),
            phases: ["Ask", "Answer", "Wrap"]
                .iter()
                .zip(1..)
                .map(|(title, index)| CliPhase {
                    index,
                    title: (*title).to_owned(),
                    detail: String::new(),
                })
                .collect(),
            agents: vec![
                agent(1, "ask:one", 1, CliAgentState::Done),
                agent(2, "answer:one", 2, CliAgentState::Running),
            ],
            status: SubagentStatus::Running,
            tokens: 20_626,
            tool_calls: 0,
            duration_ms: 6_661,
            summary: None,
            started_at_ms: None,
        };
        let mut events = vec![
            session_started(0, &host, None, RoleName::Worker, "claude-code"),
            event(
                1,
                RunEventKind::Agent(AgentEvent::Workflow {
                    workflow: Box::new(workflow.clone()),
                }),
            ),
        ];
        events[1].session = Some(host.clone());
        let id = cli_workflow_run_id("w4d273qmn");
        let view = workflow_view(&events, &run_id(), Some(&id)).unwrap();
        assert_eq!((view.name.as_str(), view.id.clone()), ("probe", id.clone()));
        let shown: Vec<(&str, Option<PhaseCount>, RowStatus)> = view
            .phases
            .iter()
            .map(|phase| (phase.name.as_str(), phase.count, phase.status))
            .collect();
        assert_eq!(
            shown,
            [
                ("Ask", PhaseCount::of(1, 1), RowStatus::Done),
                ("Answer", PhaseCount::of(0, 1), RowStatus::Running),
                ("Wrap", None, RowStatus::Waiting),
            ]
        );
        let WorkflowRow::Agent(row) = &view.phases[1].rows[0] else {
            panic!("an agent row");
        };
        assert_eq!(row.label, "answer:one");
        assert_eq!(row.session, host);
        assert_eq!((view.agents_done, view.agents_total), (1, 2));
        assert_eq!(cli_workflow_runs(&events).len(), 1);
    }

    /// A run whose one task is a session running Claude Code's own workflow: its phases are
    /// the run's, the session that plans first, the reviewer it asked in the row it answered,
    /// and no second run nested under the session.
    #[test]
    fn claude_codes_own_workflow_is_the_runs_steps() {
        use argonauts_core::events::session::{
            CliAgent, CliAgentState, CliPhase, CliWorkflowRun, SubagentStatus,
        };
        let top = WorkflowId::numbered(1);
        let script = step(&top, 0, 0);
        let host = SessionKey::numbered(1);
        let reviewer = SessionKey::numbered(2);
        let task = argonauts_core::TaskId::new("task-add-due-dates").unwrap();
        let agent = |index: u32, label: &str, phase: u32, state: CliAgentState| CliAgent {
            index,
            label: label.to_owned(),
            phase: Some(phase),
            model: Some("claude-haiku-4-5".to_owned()),
            state,
            tokens: 1_000,
            tool_calls: 0,
            duration_ms: 1_000,
            started_at_ms: None,
            try_number: 1,
            prompt: None,
            result: None,
            ..CliAgent::default()
        };
        let workflow = CliWorkflowRun {
            id: "wf_1".to_owned(),
            call: None,
            name: "add-due-dates".to_owned(),
            description: "Add due dates".to_owned(),
            phases: ["Build", "Review", "Final"]
                .iter()
                .zip(1..)
                .map(|(title, index)| CliPhase {
                    index,
                    title: (*title).to_owned(),
                    detail: String::new(),
                })
                .collect(),
            agents: vec![
                agent(1, "build:due-dates", 1, CliAgentState::Done),
                agent(2, "review:due-dates", 2, CliAgentState::Done),
                agent(3, "final:due-dates", 3, CliAgentState::Running),
            ],
            status: SubagentStatus::Running,
            tokens: 3_000,
            tool_calls: 0,
            duration_ms: 3_000,
            summary: None,
            started_at_ms: None,
        };
        let mut events = vec![
            started(
                0,
                &top,
                None,
                definition(serde_json::json!({
                    "name": "add-due-dates",
                    "phases": [{"name": "Work", "standard": "do", "steps": [{"kind": "script"}]}]
                })),
            ),
            step_started(1, script.clone(), StepKind::Script, StandardPhase::Do),
            placed(
                2,
                &host,
                RoleName::Worker,
                "claude-code/haiku",
                script.clone(),
            ),
            event(
                3,
                RunEventKind::Agent(AgentEvent::Workflow {
                    workflow: Box::new(workflow),
                }),
            ),
            placed(
                4,
                &reviewer,
                RoleName::Reviewer,
                "codex/gpt-5.6-luna",
                script,
            ),
        ];
        if let RunEventKind::Session(SessionEvent::Started(started)) = &mut events[2].kind {
            started.label = Some("build:add-due-dates".to_owned());
        }
        events[2].task_id = Some(task);
        events[3].session = Some(host.clone());
        if let RunEventKind::Session(SessionEvent::Started(started)) = &mut events[4].kind {
            started.label = Some("review:due-dates".to_owned());
        }
        let view = workflow_view(&events, &run_id(), None).unwrap();
        let shown: Vec<(&str, Vec<String>)> = view
            .phases
            .iter()
            .map(|phase| {
                let rows = phase
                    .rows
                    .iter()
                    .map(|row| match row {
                        WorkflowRow::Agent(agent) => {
                            format!("{} {}", agent.label, agent.worker.agent)
                        }
                        WorkflowRow::Workflow(nested) => format!("run {}", nested.name),
                        WorkflowRow::Command(_) => "command".to_owned(),
                    })
                    .collect();
                (phase.name.as_str(), rows)
            })
            .collect();
        assert_eq!(shown.len(), 3, "{shown:?}");
        assert_eq!(shown[0].0, "Build");
        // The session that plans it is the box, not one of its steps.
        assert_eq!(
            shown[0].1,
            ["build:due-dates claude-code/haiku"],
            "{shown:?}"
        );
        assert_eq!(shown[1].1, ["review:due-dates codex/gpt-5.6-luna"]);
        assert_eq!(shown[2].1, ["final:due-dates claude-code/haiku"]);
        assert_eq!((view.agents_done, view.agents_total), (1, 3));
        let counts: Vec<Option<(u32, u32)>> = view
            .phases
            .iter()
            .map(|phase| phase.count.map(|count| (count.done, count.total)))
            .collect();
        assert_eq!(counts[0], Some((1, 1)), "{counts:?}");
        // Whose own workflow it is: the session's CLI, which the test names with its model.
        assert_eq!(view.worker.as_deref(), Some("claude-code/haiku"));
    }

    /// Before the main agent picks the run's workflow, Understand runs nested under the
    /// workflow to come, and the view shows it rather than nothing.
    #[test]
    fn understand_shows_before_the_runs_own_workflow() {
        let own = WorkflowId::numbered(1);
        let understand = WorkflowId::numbered(2);
        let loop_step = step(&own, 0, 0);
        let reading = step(&understand, 0, 0);
        let nested = definition(serde_json::json!({
            "name": "understanding",
            "phases": [{"name": "Understand", "standard": "understand", "steps": [{"kind": "read"}]}]
        }));
        let mut events = vec![
            started(0, &understand, Some(loop_step), nested),
            step_started(
                1,
                reading.clone(),
                StepKind::Read,
                StandardPhase::Understand,
            ),
            placed(
                2,
                &SessionKey::numbered(1),
                RoleName::Reader,
                "claude-code",
                reading,
            ),
        ];
        let view = workflow_view(&events, &run_id(), None).unwrap();
        assert_eq!(
            (view.name.as_str(), &view.id),
            ("understanding", &understand)
        );
        assert_eq!(view.phases[0].name, "Understand");
        assert_eq!(view.agents_total, 1);
        // Once the run has its name, Understand goes by it, never by a template's.
        events.push(event(
            3,
            RunEventKind::Run(RunLifecycle::Named(RunNamed {
                name: "per-key-rate-limits".to_owned(),
                description: "Limit requests per API key".to_owned(),
                style: RunStyle::Workflow,
                based_on: Some("council".to_owned()),
                done_when: Vec::new(),
            })),
        ));
        let view = workflow_view(&events, &run_id(), None).unwrap();
        assert_eq!(view.name, "Per key rate limits");

        events.push(started(
            4,
            &own,
            None,
            definition(serde_json::json!({
                "name": "council",
                "phases": [{"name": "Understand", "standard": "understand",
                            "steps": [{"kind": "workflow", "workflow": "understanding"}]}]
            })),
        ));
        let view = workflow_view(&events, &run_id(), None).unwrap();
        assert_eq!(
            (view.name.as_str(), &view.id),
            ("Per key rate limits", &own)
        );
    }

    /// A default crew run as the engine records it (`crates/crew/tests/it/engine_goals.rs`
    /// writes it with `ARGONAUTS_RECORD_FIXTURES=1`).
    const DEFAULT_CREW_RUN: &str = include_str!("../../tests/fixtures/runs/default-crew-run.jsonl");

    #[test]
    fn a_default_crew_runs_build_phase_holds_its_build_and_its_review() {
        let events: Vec<RunEvent> = DEFAULT_CREW_RUN
            .lines()
            .map(|line| serde_json::from_str(line).unwrap())
            .collect();
        let run = events[0].run_id.clone();
        let view = workflow_view(&events, &run, None).unwrap();
        let labels = |phase: &PhaseRow| -> Vec<String> {
            phase
                .rows
                .iter()
                .filter_map(|row| match row {
                    WorkflowRow::Agent(agent) => Some(agent.label.clone()),
                    _ => None,
                })
                .collect()
        };
        let build = view
            .phases
            .iter()
            .find(|phase| phase.name == "Build")
            .unwrap();
        assert_eq!(labels(build), ["build:flag", "review:flag"]);
        assert_eq!(build.count, PhaseCount::of(2, 2));
        // The main agent's own asks beside the workflow are notes, never rows or counts.
        let all: Vec<String> = view.phases.iter().flat_map(labels).collect();
        assert!(
            all.iter().all(|label| !label.starts_with("main:")),
            "{all:?}"
        );
        assert_eq!(view.agents_total as usize, all.len(), "{all:?}");
        assert_eq!(view.agents_done, view.agents_total);
        assert!(all.iter().all(|label| !label.contains("task-")), "{all:?}");
    }

    /// Only agents are rows, as in Claude Code's view: the run's own checks and its merge are
    /// no rows of any phase, and a phase no agent worked in leaves the list once the run
    /// ended. The agents' rows say what was decided, and the header says how the run ended.
    #[test]
    fn a_default_crew_runs_checks_and_merge_are_no_rows() {
        let events: Vec<RunEvent> = DEFAULT_CREW_RUN
            .lines()
            .map(|line| serde_json::from_str(line).unwrap())
            .collect();
        let run = events[0].run_id.clone();
        let view = workflow_view(&events, &run, None).unwrap();
        let phase = |name: &str| {
            view.phases
                .iter()
                .find(|phase| phase.name == name)
                .unwrap_or_else(|| panic!("no {name} phase"))
        };
        let said = |phase: &PhaseRow| -> Vec<(String, Option<String>)> {
            phase
                .rows
                .iter()
                .filter_map(|row| match row {
                    WorkflowRow::Agent(agent) => Some((
                        agent.label.clone(),
                        agent.note.as_ref().map(|note| note.text.clone()),
                    )),
                    _ => None,
                })
                .collect()
        };

        // The worker's checks passed and its reviewer approved.
        assert_eq!(
            said(phase("Build")),
            [
                ("build:flag".to_owned(), Some("passed".to_owned())),
                ("review:flag".to_owned(), Some("approved".to_owned())),
            ]
        );
        let commands: Vec<&WorkflowRow> = view
            .phases
            .iter()
            .flat_map(|phase| &phase.rows)
            .filter(|row| matches!(row, WorkflowRow::Command(_)))
            .collect();
        assert!(commands.is_empty(), "{commands:?}");
        // The phase that checks keeps the agent that checks the goal, and only it counts.
        let check = phase("Check");
        assert_eq!(check.rows.len(), 1, "{:?}", check.rows);
        assert_eq!(check.count, PhaseCount::of(1, 1));
        // git merged it, and no agent worked in the Merge phase, so it isn't listed.
        assert!(view.phases.iter().all(|phase| phase.name != "Merge"));

        assert_eq!(view.state, WorkflowState::Done);
        let ended = view.status_lines.last().unwrap();
        assert_eq!(ended.kind, StatusKind::Ended);
        assert_eq!(ended.tone, Tone::Good);
        assert_eq!(ended.text, "Merged onto main · 5362b60");
    }

    /// The Merge phase's rows, each as its label and what it says after it. Only agents are
    /// rows, so a command is a mistake the asserts show.
    fn merge_rows(view: &WorkflowView) -> Vec<(String, Option<String>)> {
        let merge = view
            .phases
            .iter()
            .find(|phase| phase.name == "Merge")
            .expect("a Merge phase");
        merge
            .rows
            .iter()
            .map(|row| match row {
                WorkflowRow::Agent(agent) => (
                    agent.label.clone(),
                    agent.note.as_ref().map(|note| note.text.clone()),
                ),
                WorkflowRow::Command(step) => {
                    let result = step.result.as_deref().unwrap_or_default();
                    let said = format!("{} · {result}", step.command);
                    (step.label.clone(), Some(said))
                }
                WorkflowRow::Workflow(nested) => (nested.name.clone(), None),
            })
            .collect()
    }

    /// The merger is asked only for the commit message, with the diff in its prompt and no
    /// tools. Its session is a row of the Merge phase under the label the crew gives it, the
    /// phase's only row, since the merge git ran is none, and it says how it was asked while
    /// it is the phase's only agent. A merge that also needed a conflict settled leaves that
    /// to the rows.
    #[test]
    fn the_mergers_commit_message_is_a_row_of_the_merge_phase() {
        use argonauts_core::session_runner::Access;
        use argonauts_core::steps::StepName;
        use argonauts_crew::merge::merger::{COMMIT_MESSAGE, SETTLE_CONFLICT, fix_after_move};

        let recorded: Vec<RunEvent> = DEFAULT_CREW_RUN
            .lines()
            .map(|line| serde_json::from_str(line).unwrap())
            .collect();
        let run = recorded[0].run_id.clone();
        let merge_step = recorded
            .iter()
            .find_map(|event| match &event.kind {
                RunEventKind::Workflow(WorkflowEvent::StepStarted(step))
                    if step.kind == StepKind::Merge =>
                {
                    Some(step.step.clone())
                }
                _ => None,
            })
            .expect("the run's Merge step");
        let merged = recorded
            .iter()
            .position(|event| {
                matches!(
                    event.kind,
                    RunEventKind::Merge(MergeEvent::RunMerged { .. })
                )
            })
            .expect("the run merged");
        // A session of the merger at the Merge step, just before git merged the run.
        let session_of = |key: &SessionKey, label: &str| {
            let at = recorded[merged - 1].ts;
            let mut started = session_started(0, key, None, RoleName::Worker, "claude-code");
            if let RunEventKind::Session(SessionEvent::Started(session)) = &mut started.kind {
                session.label = Some(label.to_owned());
                session.access = Access::NoTools;
                session.workflow_step = Some(merge_step.clone());
            }
            let mut ended = session_ended(0, key, EndReason::Completed, 19_000);
            for event in [&mut started, &mut ended] {
                event.run_id = run.clone();
                event.ts = at;
            }
            [started, ended]
        };
        let message = SessionKey::numbered(90);

        let mut clean = recorded.clone();
        clean.splice(merged..merged, session_of(&message, COMMIT_MESSAGE));
        let view = workflow_view(&clean, &run, None).unwrap();
        assert_eq!(
            merge_rows(&view),
            [(
                "commit message".to_owned(),
                Some("given the diff, no tools".to_owned())
            )]
        );
        let merge = view.phases.iter().find(|phase| phase.name == "Merge");
        assert_eq!(merge.unwrap().count, PhaseCount::of(1, 1));

        let mut settled = recorded.clone();
        let both = session_of(&SessionKey::numbered(89), SETTLE_CONFLICT)
            .into_iter()
            .chain(session_of(&message, COMMIT_MESSAGE));
        settled.splice(merged..merged, both);
        let view = workflow_view(&settled, &run, None).unwrap();
        let rows = merge_rows(&view);
        assert_eq!(rows[0], ("settle conflict".to_owned(), None));
        assert_eq!(rows[1], ("commit message".to_owned(), None));

        // A session the merge needed says what it did once it ended with its report: the one
        // that settled a conflict in its own words, and the one that fixed what broke after
        // your branch came in by the files it changed.
        let reported = |key: &SessionKey, label: &str, step: Option<StepName>, did: &str| {
            let [mut started, mut ended] = session_of(key, label);
            if let RunEventKind::Session(SessionEvent::Started(session)) = &mut started.kind {
                session.step = step;
                session.access = Access::Build;
            }
            if let RunEventKind::Session(SessionEvent::Ended(end)) = &mut ended.kind {
                end.structured = Some(serde_json::json!({
                    "status": "done", "summary": did, "files_changed": ["src/cli.js"],
                    "tests_run": [], "concerns": []
                }));
            }
            [started, ended]
        };
        let settler = reported(
            &SessionKey::numbered(88),
            SETTLE_CONFLICT,
            Some(StepName::ResolveConflict),
            "kept both changes to the --due parsing",
        );
        let fixer = reported(
            &SessionKey::numbered(89),
            &fix_after_move("main"),
            None,
            "Made the date test read the new format.",
        );
        let mut moved = recorded.clone();
        let sessions = settler
            .into_iter()
            .chain(fixer)
            .chain(session_of(&message, COMMIT_MESSAGE));
        moved.splice(merged..merged, sessions);
        let rows = merge_rows(&workflow_view(&moved, &run, None).unwrap());
        assert_eq!(
            rows[0],
            (
                "settle conflict".to_owned(),
                Some("kept both changes to the --due parsing".to_owned())
            )
        );
        assert_eq!(
            rows[1],
            ("fix after main moved".to_owned(), Some("1 file".to_owned()))
        );
        assert_eq!(rows[2], ("commit message".to_owned(), None));
    }

    /// The line a merged run ends on says what the merge changed, as its merge question put
    /// it up: `Merged onto main · 3f9c2a1 · 4 files +52 −6`, and in a folder without git
    /// `Written into your folder · 4 files +52 −6 · /rewind puts it back`.
    #[test]
    fn a_merged_run_says_what_its_merge_changed() {
        use argonauts_core::hidden_git;

        let mut events: Vec<RunEvent> = DEFAULT_CREW_RUN
            .lines()
            .map(|line| serde_json::from_str(line).unwrap())
            .collect();
        for event in &mut events {
            if let RunEventKind::Approval(ApprovalEvent::Opened(opened)) = &mut event.kind
                && let ApprovalPacket::Merge(merge) = &mut opened.packet
            {
                merge.combined = DiffStat {
                    files_changed: 4,
                    insertions: 52,
                    deletions: 6,
                };
            }
        }
        let run = events[0].run_id.clone();
        let ended = |events: &[RunEvent]| {
            let view = workflow_view(events, &run, None).unwrap();
            view.status_lines.last().unwrap().text.clone()
        };
        assert_eq!(
            ended(&events),
            "Merged onto main · 5362b60 · 4 files +52 \u{2212}6"
        );

        for event in &mut events {
            if let RunEventKind::Merge(MergeEvent::RunMerged { target, .. }) = &mut event.kind {
                hidden_git::BRANCH.clone_into(target);
            }
        }
        assert_eq!(
            ended(&events),
            "Written into your folder · 4 files +52 \u{2212}6 · /rewind puts it back"
        );
    }

    /// A review that sent the work back with no round left keeps its phase from reading done:
    /// the phase waits for you while you're asked, and otherwise its review didn't pass.
    #[test]
    fn a_final_send_back_keeps_its_phase_from_reading_done() {
        use argonauts_core::events::task::{ReviewEvent, TaskEvent, TaskState};
        use argonauts_core::steps::judgment::ReviewVerdict;

        let mut events: Vec<RunEvent> = DEFAULT_CREW_RUN
            .lines()
            .map(|line| serde_json::from_str(line).unwrap())
            .collect();
        events.retain(|event| {
            !matches!(
                event.kind,
                RunEventKind::Merge(MergeEvent::Merged { .. } | MergeEvent::RunMerged { .. })
            )
        });
        let mut reviewer = None;
        for event in &mut events {
            match &mut event.kind {
                RunEventKind::Review(ReviewEvent::Reviewed { review, .. }) => {
                    review.verdict = ReviewVerdict::RequestChanges;
                    reviewer.clone_from(&event.session);
                }
                RunEventKind::Task(TaskEvent::State { from, to, reason })
                    if *from == Some(TaskState::InReview) =>
                {
                    *to = TaskState::NeedsYou;
                    "review asked for changes again after 2 rounds".clone_into(reason);
                }
                RunEventKind::Task(TaskEvent::State { to, .. }) if *to == TaskState::Merged => {
                    *to = TaskState::NeedsYou;
                }
                RunEventKind::Run(RunLifecycle::Finished { outcome, .. }) => {
                    *outcome = RunOutcome::BranchLeft;
                }
                _ => {}
            }
        }
        let reviewer = reviewer.unwrap();
        let run = events[0].run_id.clone();
        let view = workflow_view(&events, &run, None).unwrap();
        let (phase, row) = view
            .phases
            .iter()
            .find_map(|phase| {
                phase.rows.iter().find_map(|row| match row {
                    WorkflowRow::Agent(agent) if agent.session == reviewer => Some((phase, agent)),
                    _ => None,
                })
            })
            .unwrap();
        assert_eq!(
            row.note.as_ref().map(|note| note.text.as_str()),
            Some("sent it back · no rounds left")
        );
        assert_eq!(phase.status, RowStatus::Failed, "{}", phase.name);
    }

    /// A run kept on its branch ends on a warning that says so, with the change its merge
    /// question put up and why it wasn't merged.
    #[test]
    fn a_run_kept_on_its_branch_ends_on_a_warning() {
        let mut events: Vec<RunEvent> = DEFAULT_CREW_RUN
            .lines()
            .map(|line| serde_json::from_str(line).unwrap())
            .collect();
        events.retain(|event| {
            !matches!(
                event.kind,
                RunEventKind::Merge(MergeEvent::RunMerged { .. })
            )
        });
        for event in &mut events {
            match &mut event.kind {
                RunEventKind::Approval(ApprovalEvent::Opened(opened)) => {
                    if let ApprovalPacket::Merge(merge) = &mut opened.packet {
                        merge.combined = DiffStat {
                            files_changed: 4,
                            insertions: 105,
                            deletions: 4,
                        };
                    }
                }
                RunEventKind::Run(RunLifecycle::Finished { outcome, detail }) => {
                    *outcome = RunOutcome::BranchLeft;
                    *detail = Some("its reviewer runs on the worker's CLI".to_owned());
                }
                _ => {}
            }
        }
        let run = events[0].run_id.clone();
        let view = workflow_view(&events, &run, None).unwrap();
        let ended = view.status_lines.last().unwrap();
        assert_eq!(
            ended.text,
            "Kept on its branch, not merged · 4 files +105 \u{2212}4 · its reviewer runs on the \
             worker's CLI"
        );
        assert_eq!(ended.tone, Tone::Warn);
    }

    /// A crew's run that started from no learned workflow names the workflow in use after its
    /// own name, as its Workflow item in the conversation does. Captain's own steps are no
    /// workflow of yours, so its run names none.
    #[test]
    fn a_crews_run_names_the_workflow_in_use_after_its_name() {
        use crate::client::test_support::run_started;

        let top = WorkflowId::numbered(1);
        let begun = |mode: RunMode| {
            let mut event = run_started(0, "add per-key rate limiting", None);
            if let RunEventKind::Run(RunLifecycle::Started(started)) = &mut event.kind {
                started.mode = mode;
                started.workflow = "council".to_owned();
            }
            event
        };
        let named = event(
            2,
            RunEventKind::Run(RunLifecycle::Named(RunNamed {
                name: "per-key-rate-limiting".to_owned(),
                description: "Per-key rate limiting for the public API".to_owned(),
                style: RunStyle::Workflow,
                based_on: None,
                done_when: Vec::new(),
            })),
        );
        let notes = |mode: RunMode| {
            let events = vec![
                begun(mode),
                started(1, &top, None, captain()),
                named.clone(),
            ];
            workflow_view(&events, &run_id(), None).unwrap().notes
        };
        // A council is the mode itself.
        assert_eq!(notes(RunMode::Crew), ["Council"]);
        assert!(notes(RunMode::Captain).is_empty());
    }

    /// In a folder without git a run's merge names the hidden history's branch, which is
    /// never shown. Code wrote it into your folder, and no model wrote a commit message, so
    /// the Merge phase has no agent and isn't listed; the view's ending says how to undo it.
    #[test]
    fn a_run_in_a_folder_without_git_ends_written_into_your_folder() {
        use argonauts_core::hidden_git;

        let mut events: Vec<RunEvent> = DEFAULT_CREW_RUN
            .lines()
            .map(|line| serde_json::from_str(line).unwrap())
            .collect();
        for event in &mut events {
            if let RunEventKind::Merge(MergeEvent::RunMerged { target, .. }) = &mut event.kind {
                hidden_git::BRANCH.clone_into(target);
            }
        }
        let run = events[0].run_id.clone();
        let view = workflow_view(&events, &run, None).unwrap();
        assert!(view.phases.iter().all(|phase| phase.name != "Merge"));
        let ended = view.status_lines.last().unwrap();
        assert_eq!(ended.kind, StatusKind::Ended);
        assert_eq!(
            ended.text,
            "Written into your folder · /rewind puts it back"
        );
        let shown = serde_json::to_string(&view).unwrap();
        assert!(!shown.contains(hidden_git::BRANCH), "{shown}");
    }

    /// A review that stays on its CLI with the next model: the run says who stopped and who
    /// it fell back to in one line about the workflow, which shows under the header while the
    /// phase's row is the fallback's, for the model the step asked and why it stopped.
    #[test]
    fn a_steps_fallback_is_said_under_the_header() {
        use argonauts_core::ErrorCategory;
        use argonauts_core::events::workflow::RunNote;

        let top = WorkflowId::numbered(1);
        let review = step(&top, 2, 0);
        let (first, second) = (SessionKey::numbered(1), SessionKey::numbered(2));
        let opus: ModelRef = "claude-code/claude-opus-5-5".parse().unwrap();
        let sonnet: ModelRef = "claude-code/claude-sonnet-5-5".parse().unwrap();
        let reviewing = |seq, key: &SessionKey, worker: &ModelRef, asked: Option<&ModelRef>| {
            let mut started = placed(seq, key, RoleName::Reviewer, "claude-code", review.clone());
            if let RunEventKind::Session(SessionEvent::Started(session)) = &mut started.kind {
                session.worker = worker.clone();
                session.label = Some("review:due-tests".to_owned());
                session.asked = asked.cloned();
            }
            started
        };
        let overloaded = EndReason::CliError {
            category: ErrorCategory::Overloaded,
            message: "Overloaded".to_owned(),
        };
        let said = "Opus 5.5 on Claude Code is overloaded · fell back to Sonnet 5.5 on Claude \
                    Code, as same_cli asks";
        let events = vec![
            started(0, &top, None, captain()),
            step_started(1, review.clone(), StepKind::Review, StandardPhase::Check),
            reviewing(2, &first, &opus, None),
            session_ended(3, &first, overloaded, 2_000),
            reviewing(4, &second, &sonnet, Some(&opus)),
            workflow_event(
                5,
                WorkflowEvent::Note(RunNote {
                    text: said.to_owned(),
                    workflow: Some(top.clone()),
                    left: false,
                }),
            ),
        ];
        let view = workflow_view(&events, &run_id(), None).unwrap();
        let [line] = view.status_lines.as_slice() else {
            panic!("{:?}", view.status_lines);
        };
        assert_eq!((line.kind, line.text.as_str()), (StatusKind::Note, said));

        let phase = view
            .phases
            .iter()
            .find(|phase| phase.name == "Review")
            .expect("a Review phase");
        let [WorkflowRow::Agent(fallback)] = phase.rows.as_slice() else {
            panic!("one row, the fallback's: {:?}", phase.rows);
        };
        assert_eq!(fallback.label, "review:due-tests");
        assert_eq!(fallback.status, RowStatus::Running);
        assert_eq!(fallback.worker, sonnet);
        assert_eq!(fallback.fallback_for, Some(opus));
        assert_eq!(
            fallback.note.as_ref().map(|note| note.text.as_str()),
            Some("overloaded")
        );
        assert_eq!(phase.count, PhaseCount::of(0, 1));
    }

    /// A build task that fell back to another worker: the line the run wrote about the task's
    /// workflow shows under the header, and the task's row in Build is the fallback's, for the
    /// worker that stopped and why.
    #[test]
    fn a_tasks_fallback_is_said_under_the_header() {
        use argonauts_core::events::agent_status::{FallbackEvent, FallbackPlan};
        use argonauts_core::events::run::Party;
        use argonauts_core::events::workflow::RunNote;
        use argonauts_core::settings::fallback::{FallbackMode, FallbackStrategy};
        use argonauts_core::{DiffStat, TaskId};

        let top = WorkflowId::numbered(1);
        let build = step(&top, 1, 0);
        let task = TaskId::new("task-001").unwrap();
        let (first, second) = (SessionKey::numbered(1), SessionKey::numbered(2));
        let opus: ModelRef = "claude-code/claude-opus-5-5".parse().unwrap();
        let gpt: ModelRef = "codex/gpt-5.6".parse().unwrap();
        let building = |seq, key: &SessionKey, worker: &ModelRef| {
            let mut started = session_started(
                seq,
                key,
                Some(&task),
                RoleName::Worker,
                worker.agent.as_str(),
            );
            if let RunEventKind::Session(SessionEvent::Started(session)) = &mut started.kind {
                session.worker = worker.clone();
                session.label = Some("build:due-flag".to_owned());
                session.workflow_step = Some(build.clone());
            }
            started
        };
        let about_the_task = |seq, kind| {
            let mut event = event(seq, kind);
            event.task_id = Some(task.clone());
            event
        };
        let plan = FallbackPlan {
            task: task.clone(),
            title: "The --due flag".to_owned(),
            stopped: opus.clone(),
            reason: "usage limit".to_owned(),
            resets_at: None,
            work: DiffStat::default(),
            checks_ran: false,
            candidates: Vec::new(),
            mode: FallbackMode::Auto,
            strategy: FallbackStrategy::First,
            race_count: 1,
            also_moving: Vec::new(),
        };
        let fell_back = FallbackEvent::FellBack {
            task: task.clone(),
            to: gpt.clone(),
            mode: FallbackMode::Auto,
            by: Party::Rules,
            context: None,
        };
        let said = "Opus 5.5 on Claude Code's session ended early (its usage limit) · fell back \
                    to GPT 5.6 on Codex";
        let events = vec![
            started(0, &top, None, captain()),
            step_started(1, build.clone(), StepKind::Build, StandardPhase::Do),
            building(2, &first, &opus),
            session_ended(3, &first, EndReason::UsageLimit { resets_at: None }, 2_000),
            about_the_task(4, RunEventKind::Fallback(FallbackEvent::Needed(plan))),
            about_the_task(5, RunEventKind::Fallback(fell_back)),
            workflow_event(
                6,
                WorkflowEvent::Note(RunNote {
                    text: said.to_owned(),
                    workflow: Some(top.clone()),
                    left: false,
                }),
            ),
            building(7, &second, &gpt),
        ];
        let view = workflow_view(&events, &run_id(), None).unwrap();
        let [line] = view.status_lines.as_slice() else {
            panic!("{:?}", view.status_lines);
        };
        assert_eq!((line.kind, line.text.as_str()), (StatusKind::Note, said));

        let phase = view
            .phases
            .iter()
            .find(|phase| phase.name == "Build")
            .expect("a Build phase");
        let [WorkflowRow::Agent(fallback)] = phase.rows.as_slice() else {
            panic!("one row, the fallback's: {:?}", phase.rows);
        };
        assert_eq!(fallback.label, "build:due-flag");
        assert_eq!(fallback.status, RowStatus::Running);
        assert_eq!(fallback.worker, gpt);
        assert_eq!(fallback.fallback_for, Some(opus));
        assert_eq!(
            fallback.note.as_ref().map(|note| note.text.as_str()),
            Some("usage limit")
        );
    }

    #[test]
    fn a_task_auto_left_to_you_says_so_under_the_header() {
        let mut events: Vec<RunEvent> = DEFAULT_CREW_RUN
            .lines()
            .map(|line| serde_json::from_str(line).unwrap())
            .collect();
        // Cut the run where its first approval opened, before auto settled it, and wait there.
        let opened = events
            .iter()
            .position(|event| {
                matches!(event.kind, RunEventKind::Approval(ApprovalEvent::Opened(_)))
            })
            .unwrap();
        events.truncate(opened + 1);
        let RunEventKind::Approval(ApprovalEvent::Opened(task)) = &events[opened].kind else {
            unreachable!();
        };
        assert!(!task.needs_you, "auto may settle it when it opens");
        let (id, title) = (task.id.clone(), task.title.clone());
        let mut waiting = events[opened].clone();
        waiting.seq += 1;
        waiting.kind = RunEventKind::Run(RunLifecycle::Waiting {
            reason: WaitReason::Approval,
            waiting_on: vec![id],
        });
        events.push(waiting);
        let run = events[0].run_id.clone();
        let view = workflow_view(&events, &run, None).unwrap();
        let lines: Vec<&str> = view
            .status_lines
            .iter()
            .map(|line| line.text.as_str())
            .collect();
        // The question itself draws under the view, so the line only names it.
        assert_eq!(lines, [title.trim()]);
    }

    /// A CLI that is down says so under the header: which CLI, how many tasks wait on it, and
    /// how long until it is tried again, for the screen to count down.
    #[test]
    fn a_cli_that_is_down_says_what_waits_and_when_it_is_tried_again() {
        use argonauts_core::TaskId;
        use argonauts_core::events::approval::{AgentDownPacket, PausedTask};
        use argonauts_core::names::{AgentId, RoleName};

        let mut events: Vec<RunEvent> = DEFAULT_CREW_RUN
            .lines()
            .map(|line| serde_json::from_str(line).unwrap())
            .collect();
        let opened = events
            .iter()
            .position(|event| {
                matches!(event.kind, RunEventKind::Approval(ApprovalEvent::Opened(_)))
            })
            .unwrap();
        events.truncate(opened + 1);
        let RunEventKind::Approval(ApprovalEvent::Opened(asked)) = &mut events[opened].kind else {
            unreachable!();
        };
        let paused = |task: &str| PausedTask {
            task: TaskId::new(task).unwrap(),
            title: "a task".to_owned(),
            role: RoleName::Worker,
        };
        asked.needs_you = true;
        asked.packet = ApprovalPacket::AgentDown(AgentDownPacket {
            agent_id: AgentId("claude-code".into()),
            evidence: "overloaded".to_owned(),
            test_at: Some(OffsetDateTime::now_utc() + time::Duration::minutes(10)),
            paused: vec![paused("task-001"), paused("task-002")],
            fits_all: None,
            kept_by: None,
        });
        let run = events[0].run_id.clone();
        let view = workflow_view(&events, &run, None).unwrap();
        let [line] = view.status_lines.as_slice() else {
            panic!("{:?}", view.status_lines);
        };
        assert_eq!(line.kind, StatusKind::NeedsYou);
        assert_eq!(line.text, "Claude Code is down · 2 tasks wait for it");
        let left = line.left.as_ref().expect("a wait to count down");
        assert_eq!(left.words, "tried again in");
        assert!((590_000..=600_000).contains(&left.ms), "{}", left.ms);
    }
    /// A workflow's phases are whatever it declares, in its order: none of them is a name the
    /// view knows, two share one name, and there are twelve.
    #[test]
    fn any_phases_a_workflow_declares_are_the_views_phases() {
        let names = [
            "Survey", "Outline", "Draft", "Critique", "Draft", "Critique", "Numbers", "Figures",
            "Proof", "Typeset", "Package", "Announce",
        ];
        let phases: Vec<serde_json::Value> = names
            .iter()
            .map(|name| {
                serde_json::json!({"name": name, "standard": "do", "steps": [{"kind": "build"}]})
            })
            .collect();
        let top = WorkflowId::numbered(1);
        let second_draft = step(&top, 4, 0);
        let writer = SessionKey::numbered(1);
        let events = vec![
            started(
                0,
                &top,
                None,
                definition(serde_json::json!({"name": "write-the-paper", "phases": phases})),
            ),
            step_started(1, second_draft.clone(), StepKind::Build, StandardPhase::Do),
            placed(2, &writer, RoleName::Worker, "codex", second_draft),
        ];
        let view = workflow_view(&events, &run_id(), None).unwrap();
        let shown: Vec<&str> = view
            .phases
            .iter()
            .map(|phase| phase.name.as_str())
            .collect();
        assert_eq!(shown, names);
        // The agent sits in the second Draft, the phase it was placed in, not the first.
        let with_agents: Vec<usize> = view
            .phases
            .iter()
            .enumerate()
            .filter(|(_, phase)| !phase.rows.is_empty())
            .map(|(i, _)| i)
            .collect();
        assert_eq!(with_agents, [4]);
        assert_eq!(view.phases[4].status, RowStatus::Running);
        assert_eq!(view.phases[2].status, RowStatus::Waiting);
    }

    #[test]
    fn a_workflow_of_one_phase_and_one_with_no_phase_yet_both_open() {
        let top = WorkflowId::numbered(1);
        let one = vec![started(
            0,
            &top,
            None,
            definition(serde_json::json!({
                "name": "translate",
                "phases": [{"name": "Translate the changelog", "standard": "do",
                            "steps": [{"kind": "build"}]}]
            })),
        )];
        let view = workflow_view(&one, &run_id(), None).unwrap();
        assert_eq!(view.phases.len(), 1);
        assert_eq!(view.phases[0].name, "Translate the changelog");
        assert_eq!(view.phases[0].count, None);

        let none = vec![started(
            0,
            &top,
            None,
            definition(serde_json::json!({"name": "warming-up", "phases": []})),
        )];
        let view = workflow_view(&none, &run_id(), None).unwrap();
        assert!(view.phases.is_empty());
        assert_eq!((view.agents_done, view.agents_total), (0, 0));
    }

    /// `orchestrator workflow` follows the run's own name, and a run named after the workflow
    /// it started from doesn't say so twice.
    #[test]
    fn the_workflow_a_run_started_from_is_noted_after_its_name() {
        let top = WorkflowId::numbered(1);
        let named = |name: &str| {
            event(
                1,
                RunEventKind::Run(RunLifecycle::Named(RunNamed {
                    name: name.to_owned(),
                    description: "Add due dates to tasks".to_owned(),
                    style: RunStyle::Workflow,
                    based_on: Some("orchestrator".to_owned()),
                    done_when: Vec::new(),
                })),
            )
        };
        let events = vec![started(0, &top, None, captain()), named("add-due-dates")];
        let view = workflow_view(&events, &run_id(), None).unwrap();
        assert_eq!(view.notes, ["orchestrator workflow"]);

        let events = vec![started(0, &top, None, captain()), named("orchestrator")];
        let view = workflow_view(&events, &run_id(), None).unwrap();
        assert!(view.notes.is_empty());
    }

    /// The steps a workflow's own declaration says you require are the view's, so one asked
    /// for in a single request is marked as one kept in a file is.
    #[test]
    fn the_steps_a_declaration_requires_are_the_views() {
        let top = WorkflowId::numbered(1);
        let workflow = definition(serde_json::json!({
            "name": "switch-reads",
            "phases": [
                {"name": "Build", "standard": "do", "steps": [{"kind": "build"}]},
                {"name": "Benchmark", "standard": "do", "steps": [{"kind": "build"}]}
            ]
        }));
        let mut declared = WorkflowDeclared::of(&top, &workflow);
        declared.required = vec!["benchmark".to_owned()];
        let events = vec![
            started(0, &top, None, workflow),
            event(
                1,
                RunEventKind::Workflow(WorkflowEvent::Declared(Box::new(declared))),
            ),
        ];
        let view = workflow_view(&events, &run_id(), None).unwrap();
        assert_eq!(view.required, ["benchmark"]);
        assert!(!view.requires(&view.phases[0]));
        assert!(view.requires(&view.phases[1]));
    }

    /// A job no model could take, which the council goes on without, is a grey row of its
    /// phase: the model it was meant for and why, never counted, and its row opens a view
    /// that says why it was left out. Once nothing can take a job the run needs, the run
    /// reads as stopped.
    #[test]
    fn a_job_left_out_is_a_row_of_its_phase_and_a_full_stop_reads_stopped() {
        use argonauts_core::events::workflow::AgentLeftOut;

        let top = WorkflowId::numbered(1);
        let propose = step(&top, 0, 0);
        let (p1, p2) = (SessionKey::numbered(1), SessionKey::numbered(2));
        let left = AgentLeftOut {
            step: propose.clone(),
            label: "propose:p3".to_owned(),
            meant_for: "antigravity-cli/gemini-3.8-flash".parse().unwrap(),
            why: "signed out".to_owned(),
            said: "agy is signed out".to_owned(),
            acts_as: "proposer".to_owned(),
            means: vec![
                "a council can do without one proposal, so it goes on with two".to_owned(),
                "sign in to agy and the next run asks it again".to_owned(),
            ],
        };
        let mut events = vec![
            started(
                0,
                &top,
                None,
                definition(serde_json::json!({
                    "name": "council",
                    "phases": [
                        {"name": "Propose", "standard": "understand", "steps": [{"kind": "read"}]}
                    ]
                })),
            ),
            step_started(
                1,
                propose.clone(),
                StepKind::Read,
                StandardPhase::Understand,
            ),
            placed(2, &p1, RoleName::Reader, "claude-code", propose.clone()),
            placed(3, &p2, RoleName::Reader, "codex", propose),
            session_ended(4, &p1, EndReason::Completed, 1_000),
            event(5, RunEventKind::Workflow(WorkflowEvent::LeftOut(left))),
        ];

        let view = workflow_view(&events, &run_id(), None).unwrap();
        let propose = &view.phases[0];
        let left_out: Vec<&AgentRow> = propose
            .rows
            .iter()
            .filter_map(|row| match row {
                WorkflowRow::Agent(agent) if agent.left_out => Some(agent),
                _ => None,
            })
            .collect();
        let [job] = left_out.as_slice() else {
            panic!("one job left out: {:?}", propose.rows);
        };
        assert_eq!(job.label, "propose:p3");
        assert_eq!(job.worker.agent, "antigravity-cli");
        assert_eq!(
            job.note.as_ref().map(|note| note.text.as_str()),
            Some("signed out")
        );
        assert_eq!((job.tokens, job.elapsed_ms), (0, 0));
        let opened = crate::views::agent::agent_view(&events, &run_id(), &job.session)
            .expect("the row left out opens its view");
        assert_eq!(&opened.row, *job);
        assert_eq!(
            opened.moved,
            Some(argonauts_core::views::Moved::LeftOut {
                because: "agy is signed out".to_owned(),
                lines: vec![
                    "a council can do without one proposal, so it goes on with two".to_owned(),
                    "sign in to agy and the next run asks it again".to_owned(),
                ],
            })
        );
        assert_eq!(opened.facts, ["proposer"]);
        assert_eq!((opened.tool_calls, opened.prompt.as_str()), (0, ""));
        assert_eq!(
            propose.count,
            PhaseCount::of(1, 2),
            "the job left out isn't counted"
        );
        assert_eq!((view.agents_done, view.agents_total), (1, 2));
        assert_eq!(view.state, WorkflowState::Running);

        events.push(event(
            6,
            RunEventKind::Run(RunLifecycle::Waiting {
                reason: WaitReason::NoFallback {
                    detail: "a council needs two proposals".to_owned(),
                    until: None,
                    stop: None,
                },
                waiting_on: Vec::new(),
            }),
        ));
        let stopped = workflow_view(&events, &run_id(), None).unwrap();
        assert_eq!(stopped.state, WorkflowState::Stopped);
    }
}