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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
599ba2f
3dc792e
599ba2f
3ce1968
599ba2f
3ce1968
3dc792e
 
599ba2f
 
3dc792e
 
 
3ce1968
 
 
 
3dc792e
 
 
 
3ce1968
3dc792e
 
 
 
 
 
 
3ce1968
 
 
 
 
 
3dc792e
 
 
 
 
 
3ce1968
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
#!/usr/bin/env node
import {
  access,
  mkdir,
  readFile,
  readdir,
  stat,
  writeFile,
} from "node:fs/promises";
import { basename, dirname, join, resolve } from "node:path";
import { homedir } from "node:os";
import { freemem, totalmem } from "node:os";
import { statfs } from "node:fs/promises";
import { pathToFileURL } from "node:url";
import { fileURLToPath } from "node:url";
import { Command } from "commander";
import {
  getOAuthApiKey,
  getOAuthProvider,
  getOAuthProviders,
  type OAuthCredentials,
} from "@earendil-works/pi-ai/oauth";
import {
  DataLifecycleConfigSchema,
  RunResultSchema,
  TesterProfileSchema,
  UserJourneySchema,
  type BrowserDevicePreset,
  type BrowserEnvironment,
  type RunAttempt,
  type RunResult,
  type TesterProfile,
  type UserJourney,
} from "./core/schemas.js";
import {
  DraftJourneyInputSchema,
  createDraftUserJourney,
  createJsonSchemaDocument,
  createTesterProfileTemplate,
  createUserJourneyTemplate,
  defaultAuthoringDir,
  formatLintIssue,
  initAuthoringProject,
  lintUserJourney,
  parseSchemaKind,
  stringifyJsonDocument,
  writeJsonDocument,
  type LintIssue,
  type SchemaKind,
} from "./core/authoring.js";
import { readAndParseJsonFile, readJsonFile } from "./core/json.js";
import {
  validateAllowedOrigins,
  validateJourneyProfileMatch,
  type ValidationIssue,
} from "./core/validation.js";
import { emitGitHubAnnotations, writeCiReports } from "./reporters/ci.js";
import {
  buildSuiteRunHistory,
  runHealth,
  snapshotRun,
  type RunSnapshot,
  type SuiteSelectionSummary,
  type SuiteRunHistory,
} from "./reporters/runComparison.js";
import {
  renderSuiteDashboard,
  type SuiteDashboardRun,
} from "./reporters/suiteDashboard.js";
import { runJourney, type RunJourneyOptions } from "./runner/runJourney.js";
import { isoTimestampForPath, sanitizePathSegment } from "./utils/path.js";
import {
  AppLifecycleController,
  applyAppTargetOverride,
  DataLifecycleController,
} from "./lifecycle/index.js";
import {
  createDefaultJourneyTestFactoryRegistry,
  type JourneyTestFactoryRegistry,
} from "./factories/index.js";
import type {
  AgentDirector,
  UiChangeRecordingOptions,
} from "./directors/types.js";
import type { BrowserDriver } from "./drivers/types.js";
import type { BookmarkCurator } from "./curation/types.js";
import { spawn } from "node:child_process";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { assertCloudReasoningConfiguration, cognitionConfigStatus, createApiServer, createJourneyRuntime, isHuggingFaceSpace, loadRuntimeCognitionConfig, ManagedLayaService, reasoningModelStatus, resolveLayaMode, assertSafeJourneyNetwork, assertSafeRedirectChain } from "./app/index.js";

export interface RunCliOptions {
  factories?: JourneyTestFactoryRegistry;
}

export type ParallelBrowserMode = "shared-tabs" | "isolated-sessions";

const collectRepeatableOption = (value: string, previous: string[] = []): string[] => [
  ...previous,
  value,
];

export async function runCli(
  argv = process.argv,
  cliOptions: RunCliOptions = {},
): Promise<void> {
  const factories =
    cliOptions.factories ?? createDefaultJourneyTestFactoryRegistry();
  const program = new Command();

  program
    .name("journeytest")
    .description("AI-agent-directed user journey testing for web apps.")
    .version("0.1.0");

  program
    .command("init")
    .description("Create starter JourneyTest authoring files.")
    .option(
      "--dir <dir>",
      "Directory for generated authoring files.",
      defaultAuthoringDir,
    )
    .option(
      "--app-name <name>",
      "App name to use in generated journeys.",
      "Acme Admin",
    )
    .option(
      "--base-url <url>",
      "Base URL to use in generated journeys.",
      "http://127.0.0.1:3000",
    )
    .option("--profile-id <id>", "Tester profile id.", "admin")
    .option("--journey-id <id>", "Starter journey id.", "admin-invite-user")
    .option("--risk-level <level>", "Journey risk level.", "writes-test-data")
    .option("--force", "Overwrite files if they already exist.")
    .action(
      async (options: {
        dir: string;
        appName: string;
        baseUrl: string;
        profileId: string;
        journeyId: string;
        riskLevel: UserJourney["riskLevel"];
        force?: boolean;
      }) => {
        const result = await initAuthoringProject({
          rootDir: resolve(options.dir),
          appName: options.appName,
          baseUrl: options.baseUrl,
          profileId: options.profileId,
          id: options.journeyId,
          riskLevel: options.riskLevel,
          force: options.force,
        });
        console.log(`Wrote tester profile: ${result.profilePath}`);
        console.log(`Wrote user journey: ${result.journeyPath}`);
      },
    );

  const newCommand = program
    .command("new")
    .description("Generate JourneyTest authoring JSON.");

  newCommand
    .command("profile")
    .description("Generate a tester profile JSON file.")
    .argument("[id]", "Tester profile id.", "admin")
    .option("--name <name>", "Tester profile display name.")
    .option("--role <role>", "Tester role.")
    .option("--perspective <text>", "Tester perspective.")
    .option("--out <path>", "Output JSON path.")
    .option("--stdout", "Print JSON to stdout instead of writing a file.")
    .option("--force", "Overwrite the output file if it already exists.")
    .action(
      async (
        id: string,
        options: {
          name?: string;
          role?: string;
          perspective?: string;
          out?: string;
          stdout?: boolean;
          force?: boolean;
        },
      ) => {
        const profile = createTesterProfileTemplate({
          id,
          name: options.name,
          role: options.role,
          perspective: options.perspective,
        });
        if (options.stdout) {
          process.stdout.write(stringifyJsonDocument(profile));
          return;
        }

        const outPath = resolve(
          options.out ??
            join(defaultAuthoringDir, "profiles", `${profile.id}.json`),
        );
        await writeJsonDocument(outPath, profile, { force: options.force });
        console.log(`Wrote tester profile: ${outPath}`);
      },
    );

  newCommand
    .command("journey")
    .description("Generate a user journey JSON file.")
    .argument("[id]", "User journey id.", "admin-invite-user")
    .option("--title <title>", "Journey title.")
    .option("--profile <id>", "Tester profile id used by the journey.", "admin")
    .option("--app-name <name>", "App name.", "Acme Admin")
    .option("--base-url <url>", "App base URL.", "http://127.0.0.1:3000")
    .option("--risk-level <level>", "Journey risk level.", "writes-test-data")
    .option("--out <path>", "Output JSON path.")
    .option("--stdout", "Print JSON to stdout instead of writing a file.")
    .option("--force", "Overwrite the output file if it already exists.")
    .action(
      async (
        id: string,
        options: {
          title?: string;
          profile: string;
          appName: string;
          baseUrl: string;
          riskLevel: UserJourney["riskLevel"];
          out?: string;
          stdout?: boolean;
          force?: boolean;
        },
      ) => {
        const journey = createUserJourneyTemplate({
          id,
          title: options.title,
          appName: options.appName,
          baseUrl: options.baseUrl,
          testerProfile: options.profile,
          riskLevel: options.riskLevel,
        });
        if (options.stdout) {
          process.stdout.write(stringifyJsonDocument(journey));
          return;
        }

        const outPath = resolve(
          options.out ??
            join(defaultAuthoringDir, "journeys", `${journey.id}.json`),
        );
        await writeJsonDocument(outPath, journey, { force: options.force });
        console.log(`Wrote user journey: ${outPath}`);
      },
    );

  const draftCommand = program
    .command("draft")
    .description(
      "Draft richer JourneyTest authoring JSON from structured local inputs.",
    );

  draftCommand
    .command("journey")
    .description("Draft a user journey JSON file from flags or a JSON input file.")
    .argument("[id]", "User journey id.")
    .option("--input <path>", "JSON draft input file.")
    .option("--title <title>", "Journey title.")
    .option("--profile <id>", "Tester profile id used by the journey.")
    .option("--app-name <name>", "App name.")
    .option("--base-url <url>", "App base URL.")
    .option("--risk-level <level>", "Journey risk level.")
    .option("--objective <text>", "Journey objective.")
    .option(
      "--precondition <text>",
      "Precondition for the tester or environment. Repeatable.",
      collectRepeatableOption,
      [],
    )
    .option(
      "--task <instruction>",
      "Task instruction. Repeatable.",
      collectRepeatableOption,
      [],
    )
    .option(
      "--task-outcome <text>",
      "Expected outcome for the task at the same repeat index. Repeatable.",
      collectRepeatableOption,
      [],
    )
    .option(
      "--pass <statement>",
      "Pass criterion statement. Repeatable.",
      collectRepeatableOption,
      [],
    )
    .option(
      "--fail <statement>",
      "Fail criterion statement. Repeatable.",
      collectRepeatableOption,
      [],
    )
    .option(
      "--blocker <statement>",
      "Blocker criterion statement. Repeatable.",
      collectRepeatableOption,
      [],
    )
    .option(
      "--data <key=value>",
      "Journey data value. Repeatable. Values are parsed as JSON when possible.",
      collectRepeatableOption,
      [],
    )
    .option("--out <path>", "Output JSON path.")
    .option("--stdout", "Print JSON to stdout instead of writing a file.")
    .option("--force", "Overwrite the output file if it already exists.")
    .action(
      async (
        id: string | undefined,
        options: {
          input?: string;
          title?: string;
          profile?: string;
          appName?: string;
          baseUrl?: string;
          riskLevel?: UserJourney["riskLevel"];
          objective?: string;
          precondition: string[];
          task: string[];
          taskOutcome: string[];
          pass: string[];
          fail: string[];
          blocker: string[];
          data: string[];
          out?: string;
          stdout?: boolean;
          force?: boolean;
        },
      ) => {
        const input = options.input
          ? DraftJourneyInputSchema.parse(
              await readJsonFile(resolve(options.input)),
            )
          : undefined;
        const data = parseDataOptions(options.data);
        if (options.taskOutcome.length > options.task.length) {
          throw new Error(
            "--task-outcome cannot be provided more times than --task.",
          );
        }
        const tasks = options.task.map((instruction, index) => ({
          instruction,
          ...(options.taskOutcome[index]
            ? { expectedOutcome: options.taskOutcome[index] }
            : {}),
        }));
        const journey = createDraftUserJourney({
          input,
          id: id ?? input?.id,
          title: options.title,
          appName: options.appName,
          baseUrl: options.baseUrl,
          testerProfile: options.profile,
          riskLevel: options.riskLevel,
          objective: options.objective,
          preconditions:
            options.precondition.length > 0 ? options.precondition : undefined,
          ...(Object.keys(data).length > 0 ? { data } : {}),
          tasks: tasks.length > 0 ? tasks : undefined,
          passCriteria: options.pass.length > 0 ? options.pass : undefined,
          failCriteria: options.fail.length > 0 ? options.fail : undefined,
          blockerCriteria:
            options.blocker.length > 0 ? options.blocker : undefined,
        });
        if (options.stdout) {
          process.stdout.write(stringifyJsonDocument(journey));
          return;
        }

        const outPath = resolve(
          options.out ??
            join(defaultAuthoringDir, "journeys", `${journey.id}.json`),
        );
        await writeJsonDocument(outPath, journey, { force: options.force });
        console.log(`Wrote drafted user journey: ${outPath}`);
      },
    );

  program
    .command("schema")
    .description("Print JourneyTest authoring JSON schemas.")
    .argument("[kind]", "profile, journey, lifecycle, or all.", "all")
    .option("--out <path>", "Write schema JSON to a file.")
    .action(async (kindValue: string, options: { out?: string }) => {
      const kind: SchemaKind = parseSchemaKind(kindValue);
      const document = createJsonSchemaDocument(kind);
      if (options.out) {
        const outPath = resolve(options.out);
        await writeJsonDocument(outPath, document, { force: true });
        console.log(`Wrote schema: ${outPath}`);
        return;
      }

      process.stdout.write(stringifyJsonDocument(document));
    });

  program
    .command("lint")
    .description(
      "Lint JourneyTest authoring files for schema and quality issues.",
    )
    .requiredOption("--journeys <path>", "Journey JSON file or directory.")
    .option("--profiles <path>", "Tester profile JSON file or directory.")
    .action(async (options: { journeys: string; profiles?: string }) => {
      const result = await lintFiles(options.journeys, options.profiles);
      if (result.issueCount > 0) {
        process.exitCode = 1;
      }
    });

  program
    .command("validate")
    .description(
      "Validate JourneyTest user journey and tester profile JSON files.",
    )
    .requiredOption("--journeys <path>", "Journey JSON file or directory.")
    .requiredOption(
      "--profiles <path>",
      "Tester profile JSON file or directory.",
    )
    .action(async (options: { journeys: string; profiles: string }) => {
      const result = await validateFiles(options.journeys, options.profiles);
      if (result.issueCount > 0) {
        process.exitCode = 1;
      }
    });

  program
    .command("auth")
    .description("Inspect JourneyTest Pi OAuth authentication without printing secrets.")
    .option(
      "--auth <path>",
      "OAuth auth file. Defaults to JOURNEYTEST_AUTH_PATH, existing ./auth.json, then user config.",
    )
    .option("--json", "Print machine-readable JSON.")
    .action(async (options: { auth?: string; json?: boolean }) => {
      const authPath = await resolveAuthPath(options.auth);
      const status = await getOAuthAuthStatus(authPath);

      if (options.json) {
        process.stdout.write(`${JSON.stringify(status, null, 2)}\n`);
        return;
      }

      console.log(`Auth file: ${status.path}`);
      console.log(`Source: ${authPathSourceLabel(status.source)}`);
      console.log(`Exists: ${status.exists ? "yes" : "no"}`);
      console.log(
        `Configured providers: ${
          status.configuredProviders.length > 0
            ? status.configuredProviders.join(", ")
            : "none"
        }`,
      );
      console.log(
        `Pi OAuth providers: ${
          status.availableOAuthProviders.length > 0
            ? status.availableOAuthProviders.join(", ")
            : "none"
        }`,
      );

      const unknown = status.configuredProviders.filter(
        (provider) => !status.availableOAuthProviders.includes(provider),
      );
      if (unknown.length > 0) {
        console.log(
          `Configured but not registered by this Pi version: ${unknown.join(", ")}`,
        );
      }
    });

  program
    .command("run")
    .description(
      "Run one JourneyTest user journey file, or all journey JSON files under a directory.",
    )
    .argument("[journeys]", "Legacy journey JSON file or directory.")
    .option("--url <url>", "Start URL for a service journey.")
    .option("--goal <goal>", "Goal for a service journey.")
    .option("--max-steps <count>", "Maximum Laya steps.", "15")
    .option("--timeout <seconds>", "Journey timeout in seconds.", "120")
    .option("--no-screenshots", "Disable screenshot capture.")
    .option("--trace", "Request trace capture when supported.")
    .option("--allowed-domain <domain>", "Allowed destination domain.", collectRepeatableOption, [])
    .option("--laya-mode <mode>", "Laya mode: auto, remote, embedded, or mock.", "auto")
    .option("--cognition-profile <profile>", "Cognition profile: local-cloud, local-spark-cloud, dual-laya-cloud, or dual-laya-spark-cloud.")
    .option("--json", "Print only JourneyResult JSON to stdout.")
    .option("--output <dir>", "Service journey output directory.")
    .option("--verbose", "Print detailed service progress.")
    .option("--profile <path>", "Tester profile JSON file or directory.")
    .option("--profiles <path>", "Tester profile JSON file or directory.")
    .option("--out <dir>", "Output directory.", "runs")
    .option("--director <name>", "Agent director factory to use.", "pi")
    .option(
      "--browser <name>",
      "Browser driver factory to use.",
      "agent-browser",
    )
    .option("--provider <provider>", "Pi model provider.", "anthropic")
    .option("--model <model>", "Pi model id or Laya checkpoint.")
    .option(
      "--auth <path>",
      "OAuth auth file. Defaults to JOURNEYTEST_AUTH_PATH, existing ./auth.json, then user config.",
    )
    .option("--session <name>", "agent-browser session name.")
    .option("--state <path>", "agent-browser state file.")
    .option("--headed", "Run browser headed.")
    .option(
      "--viewport <width>x<height>",
      "Set browser viewport size, for example 1440x900.",
    )
    .option(
      "--device <preset>",
      "Set a curated browser device preset: iphone-14, pixel-7, or ipad-pro-11.",
    )
    .option(
      "--parallel-agents <count>",
      "Number of user journeys to run in parallel.",
      "1",
    )
    .option(
      "--parallel-browser-mode <mode>",
      "Browser strategy for parallel agent-browser runs: shared-tabs or isolated-sessions.",
      "shared-tabs",
    )
    .option(
      "--retries <count>",
      "Retry each journey up to this many times after a failing attempt.",
      "0",
    )
    .option(
      "--journey-timeout-ms <ms>",
      "Maximum wall-clock time for one journey director run before it is aborted.",
      "1800000",
    )
    .option("--no-video", "Disable video recording.")
    .option(
      "--no-ui-change-recording",
      "Disable automatic UI change timelines around browser actions.",
    )
    .option(
      "--ui-change-timeout-ms <ms>",
      "Maximum time to watch for UI changes after each browser action.",
    )
    .option(
      "--ui-change-quiet-ms <ms>",
      "Quiet window before ending UI change observation after changes are seen.",
    )
    .option(
      "--ui-change-max-changes <count>",
      "Maximum UI change records to keep per browser action.",
    )
    .option(
      "--ui-change-max-screenshots <count>",
      "Maximum change screenshots to keep per browser action.",
    )
    .option(
      "--no-ui-change-screenshots",
      "Do not capture before/change/after screenshots for UI change timelines.",
    )
    .option(
      "--no-ui-change-snapshots",
      "Do not capture before/after accessibility snapshots for UI change timelines.",
    )
    .option(
      "--no-ui-change-dom-snapshots",
      "Do not capture before/after visible DOM summaries for UI change timelines.",
    )
    .option(
      "--no-curate-bookmarks",
      "Disable post-run Pi bookmark chapter curation.",
    )
    .option(
      "--bookmark-curator <name>",
      "Bookmark curator factory to use.",
      "pi",
    )
    .option(
      "--bookmark-system-prompt <path>",
      "Path to a custom system prompt for bookmark curation.",
    )
    .option(
      "--data-lifecycle <path>",
      "JourneyTest data lifecycle config JSON file.",
    )
    .option(
      "--data-lifecycle-provider <name>",
      "Data lifecycle provider factory to use.",
      "default",
    )
    .option("--keep-data", "Skip configured data lifecycle cleanup operations.")
    .option("--junit <path>", "Write a JUnit XML report for CI systems.")
    .option(
      "--github-annotations",
      "Emit GitHub Actions workflow-command annotations.",
    )
    .option("--summary-json <path>", "Write a compact CI summary JSON report.")
    .option(
      "--compare-to <path>",
      "Previous run.json or suite run directory to compare against.",
    )
    .option(
      "--tag <tag>",
      "Only run journeys with this tag. Repeat for OR matching.",
      collectRepeatableOption,
      [],
    )
    .option(
      "--exclude-tag <tag>",
      "Skip journeys with this tag. Repeatable.",
      collectRepeatableOption,
      [],
    )
    .option(
      "--journey-id <id>",
      "Only run this journey id. Repeatable.",
      collectRepeatableOption,
      [],
    )
    .option(
      "--exclude-journey-id <id>",
      "Skip this journey id. Repeatable.",
      collectRepeatableOption,
      [],
    )
    .option(
      "--rerun-failed <path>",
      "Prior run.json, history.json, or suite run directory; rerun journeys that were not healthy.",
    )
    .option(
      "--shard <index>/<total>",
      "Run one deterministic shard after filtering, using 1-based shard index.",
    )
    .action(
      async (
        journeyPath: string | undefined,
        options: {
          url?: string; goal?: string; maxSteps: string; timeout: string; screenshots: boolean; trace?: boolean; allowedDomain: string[]; layaMode: string; cognitionProfile?: string; json?: boolean; output?: string; verbose?: boolean;
          profile?: string;
          profiles?: string;
          out: string;
          director: string;
          browser: string;
          provider: string;
          model: string;
          auth?: string;
          session?: string;
          state?: string;
          headed?: boolean;
          viewport?: string;
          device?: string;
          retries: string;
          journeyTimeoutMs: string;
          parallelAgents: string;
          parallelBrowserMode: string;
          video?: boolean;
          uiChangeRecording?: boolean;
          uiChangeTimeoutMs?: string;
          uiChangeQuietMs?: string;
          uiChangeMaxChanges?: string;
          uiChangeMaxScreenshots?: string;
          uiChangeScreenshots?: boolean;
          uiChangeSnapshots?: boolean;
          uiChangeDomSnapshots?: boolean;
          curateBookmarks?: boolean;
          bookmarkCurator: string;
          bookmarkSystemPrompt?: string;
          dataLifecycle?: string;
          dataLifecycleProvider: string;
          keepData?: boolean;
          junit?: string;
          githubAnnotations?: boolean;
          summaryJson?: string;
          compareTo?: string;
          tag: string[];
          excludeTag: string[];
          journeyId: string[];
          excludeJourneyId: string[];
          rerunFailed?: string;
          shard?: string;
        },
      ) => {
        if (options.url || options.goal) {
          if (!options.url || !options.goal) throw new Error("Service runs require both --url and --goal.");
          await assertSafeJourneyNetwork(options.url, { allowedDomains: options.allowedDomain }); await assertSafeRedirectChain(options.url, { allowedDomains: options.allowedDomain });
          const mode = resolveLayaMode(options.layaMode); if (mode === "mock") throw new Error("CLI does not silently substitute mock inference.");
          const managed = mode === "embedded" ? new ManagedLayaService() : undefined; await managed?.start();
          const runtime = createJourneyRuntime({ outputDir: options.output ?? options.out, model: options.model, endpoint: managed?.endpoint, cognitionOverrides: options.cognitionProfile ? { COGNITION_PROFILE: options.cognitionProfile } : undefined }); await runtime.backend.start();
          const id = await runtime.service.createJourney({ url: options.url, goal: options.goal, maxSteps: Number(options.maxSteps), timeoutMs: Number(options.timeout) * 1000, screenshots: options.screenshots, trace: Boolean(options.trace), cognitionProfile: options.cognitionProfile, context: { allowedDomains: options.allowedDomain } });
          const result = await runtime.service.runJourney(id).finally(async () => { await runtime.backend.close(); await managed?.stop(); });
          if (options.json) process.stdout.write(`${JSON.stringify(result)}\n`); else { for (const step of result.steps) console.log(`[${String(step.step).padStart(2, "0")}] LAYA ${step.decision?.operation ?? "INVALID"}${step.decision?.elementIndex === undefined ? "" : ` [${step.decision.elementIndex}]`} p=${step.decision?.confidence?.toFixed(3) ?? "-"}`); console.log(`${result.status === "completed" ? "✓" : "✗"} ${result.status}: ${result.termination.reason}`); console.log(`Result: ${result.artifacts.result}`); }
          if (result.status !== "completed") process.exitCode = 2; return;
        }
        if (!journeyPath) throw new Error("Pass a legacy journey path or --url and --goal.");
        if (!options.model) throw new Error("Legacy journey runs require --model.");
        const profilePath = options.profiles ?? options.profile;
        if (!profilePath) {
          throw new Error(
            "Pass --profile <path> or --profiles <path> so journeys can resolve tester profiles.",
          );
        }

        const bookmarkCuratorName = bookmarkCuratorNameForOptions(options);
        factories.directors.assertRegistered(options.director);
        factories.browserDrivers.assertRegistered(options.browser);
        factories.bookmarkCurators.assertRegistered(bookmarkCuratorName);
        if (options.dataLifecycle) {
          factories.dataLifecycleProviders.assertRegistered(
            options.dataLifecycleProvider,
          );
        }

        const journeyRoot = resolve(journeyPath);
        const collectedItems = await collectRunnableJourneys(
          journeyRoot,
          resolve(profilePath),
        );
        if (collectedItems.length === 0) {
          throw new Error(`No journey JSON files found under ${journeyRoot}.`);
        }
        const rerunFailed = options.rerunFailed
          ? await loadRerunFailedSelection(resolve(options.rerunFailed))
          : undefined;
        const selection = buildJourneySelection({
          tags: options.tag,
          excludeTags: options.excludeTag,
          journeyIds: options.journeyId,
          excludeJourneyIds: options.excludeJourneyId,
          ...(rerunFailed ? { rerunFailed } : {}),
          shard: options.shard,
        });
        const selectionResult = selectRunnableJourneys(
          collectedItems,
          selection,
        );
        const items = selectionResult.items;
        if (items.length === 0) {
          throw new Error(
            `No journeys selected after applying run filters. Collected ${collectedItems.length} journey(s).`,
          );
        }

        const parallelAgents = parseParallelAgents(options.parallelAgents);
        const retries = parseRetries(options.retries);
        const journeyTimeoutMs = parseJourneyTimeoutMs(
          options.journeyTimeoutMs,
        );
        const parallelBrowserMode = parseParallelBrowserMode(
          options.parallelBrowserMode,
        );
        const browserEnvironmentOverrides =
          buildBrowserEnvironmentOptions(options);
        const uiChangeRecordingOptions = buildUiChangeRecordingOptions(options);
        const authPath = await resolveAuthPath(options.auth);
        const authLoader = createOAuthAuthLoader(authPath.path);
        const bookmarkSystemPrompt = options.bookmarkSystemPrompt
          ? await readFile(resolve(options.bookmarkSystemPrompt), "utf8")
          : undefined;
        const directorFactoryContext = {
          provider: options.provider,
          modelId: options.model,
          getApiKey: authLoader,
        };
        const bookmarkCuratorFactoryContext = {
          provider: options.provider,
          modelId: options.model,
          getApiKey: authLoader,
          systemPrompt: bookmarkSystemPrompt,
        };
        await assertFactoryOAuthCredentialsAvailable(
          [
            factories.directors.authProvider(
              options.director,
              directorFactoryContext,
            ),
            factories.bookmarkCurators.authProvider(
              bookmarkCuratorName,
              bookmarkCuratorFactoryContext,
            ),
          ],
          authPath,
        );
        const dataLifecycleConfig = options.dataLifecycle
          ? await readAndParseJsonFile(
              resolve(options.dataLifecycle),
              DataLifecycleConfigSchema,
            )
          : undefined;
        const dataLifecycleProvider = dataLifecycleConfig
          ? factories.dataLifecycleProviders.create(
              options.dataLifecycleProvider,
              {},
            )
          : undefined;
        const baseOutputDir = resolve(options.out);
        const comparisonBaseline = options.compareTo
          ? await loadComparisonBaseline(resolve(options.compareTo))
          : undefined;
        const isDirectoryRun = (await stat(journeyRoot)).isDirectory();
        const runOutputDir = isDirectoryRun
          ? join(baseOutputDir, `${isoTimestampForPath()}-run`)
          : baseOutputDir;
        if (isDirectoryRun || dataLifecycleConfig?.suiteLifecycle) {
          await mkdir(runOutputDir, { recursive: true });
        }
        const suiteRunId = isDirectoryRun
          ? basename(runOutputDir)
          : `${isoTimestampForPath()}-suite`;
        console.log(`Collected ${collectedItems.length} journey(s).`);
        if (selectionResult.summary) {
          console.log(`Selected ${items.length} journey(s) after filters.`);
        }
        console.log(
          `Parallel agents: ${Math.min(parallelAgents, items.length)}`,
        );
        if (retries > 0) {
          console.log(`Retries: ${retries}`);
        }
        const sharedBrowserTabs = shouldUseSharedBrowserTabs({
          browser: options.browser,
          parallelAgents,
          journeyCount: items.length,
          parallelBrowserMode,
        });
        if (
          parallelBrowserMode === "shared-tabs" &&
          options.browser !== "agent-browser" &&
          parallelAgents > 1 &&
          items.length > 1
        ) {
          throw new Error(
            '--parallel-browser-mode shared-tabs is only supported by the "agent-browser" browser driver.',
          );
        }
        const sharedBrowserSessionName = sharedBrowserTabs
          ? parallelBrowserSessionName(options.session, suiteRunId)
          : undefined;
        if (parallelAgents > 1 && items.length > 1) {
          console.log(
            `Browser parallel mode: ${
              sharedBrowserTabs ? "shared-tabs" : "isolated-sessions"
            }`,
          );
          if (sharedBrowserTabs) {
            console.log(`Shared browser session: ${sharedBrowserSessionName}`);
            if (options.video ?? true) {
              console.log(
                "Shared-tab video recording is serialized per journey because agent-browser supports one active recording per session.",
              );
            }
          }
        }
        if (isDirectoryRun) {
          console.log(`Run output: ${runOutputDir}`);
        }
        if (comparisonBaseline) {
          console.log(
            `Comparison baseline: ${comparisonBaseline.sourcePath} (${comparisonBaseline.runs.length} run(s))`,
          );
        }
        if (dataLifecycleConfig) {
          console.log(
            `Data lifecycle config: ${resolve(options.dataLifecycle as string)}`,
          );
          if (options.keepData) {
            console.log("Data lifecycle cleanup: skipped by --keep-data");
          }
        }
        if (browserEnvironmentOverrides) {
          console.log(
            `Browser environment overrides: ${formatBrowserEnvironment(browserEnvironmentOverrides)}`,
          );
        }
        if (!sharedBrowserTabs && parallelAgents > 1 && options.session) {
          console.log(`Browser session prefix: ${options.session}`);
        }

        let appLifecycleController: AppLifecycleController | undefined;
        let removeAppLifecycleSignalHandlers: (() => void) | undefined;
        let runtimeItems = items;
        let suiteLifecycleController: DataLifecycleController | undefined;
        let suiteLifecycleBlocked = false;
        let suiteRuns: Array<{
          journeyFile: string;
          result: Awaited<ReturnType<typeof runJourney>>;
        }> = [];

        try {
          if (dataLifecycleConfig?.appLifecycle) {
            appLifecycleController = await AppLifecycleController.create({
              config: dataLifecycleConfig.appLifecycle,
              suiteRunId,
              runDir: join(runOutputDir, "_app-lifecycle"),
            });
            removeAppLifecycleSignalHandlers =
              installAppLifecycleSignalCleanup(appLifecycleController);

            console.log("");
            console.log("Running app lifecycle start");
            await appLifecycleController.runStart();
            console.log(
              `App lifecycle: ${appLifecycleController.result.status}`,
            );
            console.log(
              `App lifecycle artifacts: ${join(runOutputDir, "_app-lifecycle")}`,
            );

            if (appLifecycleController.appTarget) {
              runtimeItems = items.map((item) => ({
                ...item,
                journey: applyAppTargetOverride(
                  item.journey,
                  appLifecycleController?.appTarget,
                ),
              }));
              console.log(
                `App target: ${appLifecycleController.appTarget.baseUrl}`,
              );
            }
          }

          if (dataLifecycleConfig?.suiteLifecycle && dataLifecycleProvider) {
            suiteLifecycleController = new DataLifecycleController({
              definition: dataLifecycleConfig.suiteLifecycle,
              environments: dataLifecycleConfig.dataEnvironments,
              provider: dataLifecycleProvider,
              scope: "suite",
              runId: suiteRunId,
              runDir: join(runOutputDir, "_suite-lifecycle"),
              suiteRunId,
              keepData: options.keepData,
            });

            console.log("");
            console.log("Running suite data lifecycle setup/preflight");
            try {
              await suiteLifecycleController.runSetupAndPreflight();
              console.log(
                `Suite data lifecycle: ${suiteLifecycleController.result.status}`,
              );
            } catch (error) {
              suiteLifecycleBlocked = true;
              process.exitCode = 1;
              const message =
                error instanceof Error ? error.message : String(error);
              console.error(`Suite data lifecycle blocked the run: ${message}`);
            }
          }

          if (!suiteLifecycleBlocked) {
            suiteRuns = await mapConcurrent(
              runtimeItems,
              parallelAgents,
              async (item, index) => {
                console.log("");
                console.log(
                  `Running ${item.journey.id} (${index + 1}/${runtimeItems.length})`,
                );
                console.log(`Source: ${item.journeyFile}`);

                const result = await runJourneyWithRetries({
                  item,
                  retries,
                  outputDir: runOutputDir,
                  createDriver: () =>
                    factories.browserDrivers.create(options.browser, {}),
                  createDirector: () =>
                    factories.directors.create(
                      options.director,
                      directorFactoryContext,
                    ),
                  createBookmarkCurator: () =>
                    factories.bookmarkCurators.create(
                      bookmarkCuratorName,
                      bookmarkCuratorFactoryContext,
                  ),
                  video: options.video,
                  directorTimeoutMs: journeyTimeoutMs,
                  uiChangeRecording: options.uiChangeRecording,
                  uiChangeRecordingOptions,
                  sessionName: browserSessionNameForRun({
                    baseSession: options.session,
                    parallelAgents,
                    journeyId: item.journey.id,
                    index,
                    suiteRunId,
                    sharedBrowserTabs,
                  }),
                  tabLabel: browserTabLabelForRun({
                    parallelAgents,
                    journeyId: item.journey.id,
                    index,
                    sharedBrowserTabs,
                  }),
                  statePath: options.state ? resolve(options.state) : undefined,
                  headed: options.headed,
                  browserEnvironment: mergeBrowserEnvironmentOptions(
                    item.journey.browserEnvironment,
                    browserEnvironmentOverrides,
                  ),
                  ...(dataLifecycleConfig && dataLifecycleProvider
                    ? {
                        dataLifecycle: {
                          environments: dataLifecycleConfig.dataEnvironments,
                          provider: dataLifecycleProvider,
                          keepData: options.keepData,
                          suiteRunId,
                          suiteManifest: suiteLifecycleController?.manifest,
                        },
                      }
                    : {}),
                });

                printRunResult(result);

                if (!runPassed(result)) {
                  process.exitCode = 1;
                }

                return { journeyFile: item.journeyFile, result };
              },
            );
          }
        } finally {
          if (suiteLifecycleController) {
            if (!suiteLifecycleBlocked) {
              await suiteLifecycleController.runPostconditions();
            }
            await suiteLifecycleController.runCleanup();
            suiteLifecycleController.finish();
            console.log("");
            console.log(
              `Suite data lifecycle: ${suiteLifecycleController.result.status}`,
            );
            console.log(
              `Suite lifecycle artifacts: ${join(runOutputDir, "_suite-lifecycle")}`,
            );
            if (
              suiteLifecycleController.result.status === "failed" ||
              suiteLifecycleController.result.status === "blocked"
            ) {
              process.exitCode = 1;
            }
          }
          if (appLifecycleController) {
            await appLifecycleController.runCleanup();
            console.log("");
            console.log(
              `App lifecycle: ${appLifecycleController.result.status}`,
            );
            console.log(
              `App lifecycle artifacts: ${join(runOutputDir, "_app-lifecycle")}`,
            );
            if (appLifecycleController.result.status === "failed") {
              process.exitCode = 1;
            }
          }
          if (removeAppLifecycleSignalHandlers) {
            removeAppLifecycleSignalHandlers();
          }
        }

        if (suiteLifecycleBlocked) {
          await writeCliCiOutputs({
            suiteRunId,
            suiteRuns,
            junitPath: options.junit,
            summaryJsonPath: options.summaryJson,
            githubAnnotations: options.githubAnnotations,
          });
          return;
        }

        if (isDirectoryRun) {
          const suiteArtifacts = await writeSuiteArtifacts(
            runOutputDir,
            suiteRuns,
            comparisonBaseline,
            selectionResult.summary,
          );
          console.log("");
          console.log(`Run dashboard: ${suiteArtifacts.dashboard}`);
          console.log(
            `Run dashboard URL: ${pathToFileURL(suiteArtifacts.dashboard).href}`,
          );
          console.log(`Run history: ${suiteArtifacts.history}`);
          console.log("");
          console.log("Journey dashboards:");
          for (const run of suiteRuns) {
            console.log(
              `- ${run.result.journeyId}: ${run.result.artifacts.dashboard}`,
            );
          }
        }

        await writeCliCiOutputs({
          suiteRunId,
          suiteRuns,
          junitPath: options.junit,
          summaryJsonPath: options.summaryJson,
          githubAnnotations: options.githubAnnotations,
        });
      },
    );

  for (const name of ["serve", "ui", "space"] as const) program.command(name).description(`${name} the JourneyTest service.`).option("--host <host>", "Bind host.", "0.0.0.0").option("--port <port>", "Bind port.", "7860").option("--cognition-profile <profile>", "Runtime-only cognition profile override.").action(async (options: { host: string; port: string; cognitionProfile?: string }) => runServerCommand(name, options));
  program.command("info").description("Print JourneyTest runtime information without secret values.").option("--json", "Print JSON.").action(async (options: { json?: boolean }) => { const config = loadRuntimeCognitionConfig(); const data = { version: "0.1.2", space: isHuggingFaceSpace(), layaMode: config.localLaya.mode, ...cognitionConfigStatus(config) }; options.json ? process.stdout.write(`${JSON.stringify(data)}\n`) : process.stdout.write(`${JSON.stringify(data, null, 2)}\n`); });
  program.command("doctor").description("Check browser, Laya, artifacts, and service dependencies.").option("--smoke-model", "Require a ready SystemOne endpoint without downloading a model.").option("--smoke-reasoning", "Make one explicit live cloud reasoning request.").option("--json", "Print machine-readable diagnostics.").action(async (options: { smokeModel?: boolean; smokeReasoning?: boolean; json?: boolean }) => runDoctor(Boolean(options.smokeModel), Boolean(options.json), Boolean(options.smokeReasoning)));

  await program.parseAsync(argv);
}

type StoredOAuthAuth = Record<string, OAuthCredentials & { type?: string }>;
export type AuthPathSource = "cli" | "env" | "local" | "user-config";

export interface AuthPathResolution {
  path: string;
  source: AuthPathSource;
  localPath: string;
  userConfigPath: string;
}

export interface AuthPathResolutionOptions {
  cwd?: string;
  env?: NodeJS.ProcessEnv;
  homeDir?: string;
  platform?: NodeJS.Platform;
  fileExists?: (path: string) => Promise<boolean>;
}

export interface OAuthAuthStatus {
  path: string;
  source: AuthPathSource;
  exists: boolean;
  configuredProviders: string[];
  availableOAuthProviders: string[];
}

export interface RunnableJourney {
  journeyFile: string;
  journey: UserJourney;
  profile: TesterProfile;
}

interface ComparisonBaseline {
  sourcePath: string;
  kind: "run" | "suite";
  runs: RunResult[];
}

export interface RerunFailedSelection {
  sourcePath: string;
  unhealthyJourneyIds: string[];
}

export interface JourneySelection {
  tags: string[];
  excludeTags: string[];
  journeyIds: string[];
  excludeJourneyIds: string[];
  rerunFailed?: RerunFailedSelection;
  shard?: ShardSelection;
}

export interface ShardSelection {
  index: number;
  total: number;
}

export interface JourneySelectionResult {
  items: RunnableJourney[];
  summary?: SuiteSelectionSummary;
}

export function parseParallelAgents(value: string): number {
  const parsed = Number(value);
  if (!Number.isInteger(parsed) || parsed < 1) {
    throw new Error(
      `--parallel-agents must be a positive integer. Received "${value}".`,
    );
  }
  return parsed;
}

export function parseParallelBrowserMode(value: string): ParallelBrowserMode {
  if (value === "shared-tabs" || value === "isolated-sessions") {
    return value;
  }
  throw new Error(
    `--parallel-browser-mode must be "shared-tabs" or "isolated-sessions". Received "${value}".`,
  );
}

export function parseRetries(value: string): number {
  const parsed = Number(value);
  if (!Number.isInteger(parsed) || parsed < 0) {
    throw new Error(`--retries must be a non-negative integer. Received "${value}".`);
  }
  return parsed;
}

export function parseViewportOption(
  value: string,
): BrowserEnvironment["viewport"] {
  const match = /^(\d+)x(\d+)(?:@(\d+(?:\.\d+)?))?$/.exec(value.trim());
  if (!match) {
    throw new Error(
      `--viewport must use <width>x<height>, optionally <width>x<height>@<scale>. Received "${value}".`,
    );
  }

  const width = Number(match[1]);
  const height = Number(match[2]);
  const deviceScaleFactor = match[3] ? Number(match[3]) : undefined;
  if (
    !Number.isInteger(width) ||
    width < 1 ||
    !Number.isInteger(height) ||
    height < 1 ||
    (deviceScaleFactor !== undefined &&
      (!Number.isFinite(deviceScaleFactor) || deviceScaleFactor <= 0))
  ) {
    throw new Error(
      `--viewport must use positive dimensions and scale. Received "${value}".`,
    );
  }

  return {
    width,
    height,
    ...(deviceScaleFactor === undefined ? {} : { deviceScaleFactor }),
  };
}

export function parseBrowserDevicePreset(value: string): BrowserDevicePreset {
  if (value === "iphone-14" || value === "pixel-7" || value === "ipad-pro-11") {
    return value;
  }
  throw new Error(
    `--device must be one of: iphone-14, pixel-7, ipad-pro-11. Received "${value}".`,
  );
}

export function buildBrowserEnvironmentOptions(options: {
  viewport?: string;
  device?: string;
}): BrowserEnvironment | undefined {
  const environment: BrowserEnvironment = {
    ...(options.device ? { device: parseBrowserDevicePreset(options.device) } : {}),
    ...(options.viewport ? { viewport: parseViewportOption(options.viewport) } : {}),
  };

  return Object.keys(environment).length > 0 ? environment : undefined;
}

export function mergeBrowserEnvironmentOptions(
  defaults: BrowserEnvironment | undefined,
  overrides: BrowserEnvironment | undefined,
): BrowserEnvironment | undefined {
  const viewport = mergeViewportOptions(defaults?.viewport, overrides?.viewport);
  const environment: BrowserEnvironment = {
    ...(defaults ?? {}),
    ...(overrides ?? {}),
    ...(viewport ? { viewport } : {}),
  };

  return Object.keys(environment).length > 0 ? environment : undefined;
}

function mergeViewportOptions(
  defaults: BrowserEnvironment["viewport"] | undefined,
  overrides: BrowserEnvironment["viewport"] | undefined,
): BrowserEnvironment["viewport"] | undefined {
  if (!defaults && !overrides) {
    return undefined;
  }

  const width = overrides?.width ?? defaults?.width;
  const height = overrides?.height ?? defaults?.height;
  if (width === undefined || height === undefined) {
    return undefined;
  }

  const deviceScaleFactor = overrides?.deviceScaleFactor ?? defaults?.deviceScaleFactor;
  return {
    width,
    height,
    ...(deviceScaleFactor === undefined ? {} : { deviceScaleFactor }),
  };
}

function formatBrowserEnvironment(environment: BrowserEnvironment): string {
  const parts = [
    environment.device ? `device=${environment.device}` : undefined,
    environment.viewport
      ? `viewport=${environment.viewport.width}x${environment.viewport.height}${
          environment.viewport.deviceScaleFactor === undefined
            ? ""
            : `@${environment.viewport.deviceScaleFactor}`
        }`
      : undefined,
  ].filter(Boolean);
  return parts.join(", ");
}

export function buildJourneySelection(options: {
  tags?: string[];
  excludeTags?: string[];
  journeyIds?: string[];
  excludeJourneyIds?: string[];
  rerunFailed?: RerunFailedSelection;
  shard?: string;
}): JourneySelection {
  const tags = uniqueSelectorValues("--tag", options.tags ?? []);
  const excludeTags = uniqueSelectorValues(
    "--exclude-tag",
    options.excludeTags ?? [],
  );
  const journeyIds = uniqueSelectorValues(
    "--journey-id",
    options.journeyIds ?? [],
  );
  const excludeJourneyIds = uniqueSelectorValues(
    "--exclude-journey-id",
    options.excludeJourneyIds ?? [],
  );
  assertDisjointSelectors("--tag", tags, "--exclude-tag", excludeTags);
  assertDisjointSelectors(
    "--journey-id",
    journeyIds,
    "--exclude-journey-id",
    excludeJourneyIds,
  );

  return {
    tags,
    excludeTags,
    journeyIds,
    excludeJourneyIds,
    ...(options.rerunFailed ? { rerunFailed: options.rerunFailed } : {}),
    ...(options.shard ? { shard: parseShardSelection(options.shard) } : {}),
  };
}

export function parseShardSelection(value: string): ShardSelection {
  const match = /^([1-9]\d*)\/([1-9]\d*)$/.exec(value);
  if (!match) {
    throw new Error(
      `--shard must use the format <index>/<total> with positive integers. Received "${value}".`,
    );
  }
  const index = Number(match[1]);
  const total = Number(match[2]);
  if (index > total) {
    throw new Error(
      `--shard index must be less than or equal to total. Received "${value}".`,
    );
  }
  return { index, total };
}

export function selectRunnableJourneys(
  items: RunnableJourney[],
  selection: JourneySelection,
): JourneySelectionResult {
  validateKnownJourneyIds(items, selection.journeyIds, "--journey-id");
  validateKnownJourneyIds(
    items,
    selection.excludeJourneyIds,
    "--exclude-journey-id",
  );

  const rerunIds = selection.rerunFailed
    ? new Set(selection.rerunFailed.unhealthyJourneyIds)
    : undefined;
  let selected = items.filter((item) => {
    const journeyTags = new Set(item.journey.tags ?? []);
    if (
      selection.journeyIds.length > 0 &&
      !selection.journeyIds.includes(item.journey.id)
    ) {
      return false;
    }
    if (selection.excludeJourneyIds.includes(item.journey.id)) {
      return false;
    }
    if (
      selection.tags.length > 0 &&
      !selection.tags.some((tag) => journeyTags.has(tag))
    ) {
      return false;
    }
    if (selection.excludeTags.some((tag) => journeyTags.has(tag))) {
      return false;
    }
    if (rerunIds && !rerunIds.has(item.journey.id)) {
      return false;
    }
    return true;
  });

  if (selection.shard) {
    selected = selected.filter(
      (_item, index) =>
        index % selection.shard!.total === selection.shard!.index - 1,
    );
  }

  const summary = buildSelectionSummary(
    items.length,
    selected.length,
    selection,
  );
  return { items: selected, ...(summary ? { summary } : {}) };
}

function buildSelectionSummary(
  collected: number,
  selected: number,
  selection: JourneySelection,
): SuiteSelectionSummary | undefined {
  const hasSelection =
    selection.tags.length > 0 ||
    selection.excludeTags.length > 0 ||
    selection.journeyIds.length > 0 ||
    selection.excludeJourneyIds.length > 0 ||
    Boolean(selection.rerunFailed) ||
    Boolean(selection.shard);
  if (!hasSelection) {
    return undefined;
  }

  return {
    collected,
    selected,
    ...(selection.tags.length > 0 ? { tags: selection.tags } : {}),
    ...(selection.excludeTags.length > 0
      ? { excludeTags: selection.excludeTags }
      : {}),
    ...(selection.journeyIds.length > 0
      ? { journeyIds: selection.journeyIds }
      : {}),
    ...(selection.excludeJourneyIds.length > 0
      ? { excludeJourneyIds: selection.excludeJourneyIds }
      : {}),
    ...(selection.rerunFailed
      ? {
          rerunFailed: {
            path: selection.rerunFailed.sourcePath,
            unhealthyJourneyIds: selection.rerunFailed.unhealthyJourneyIds,
          },
        }
      : {}),
    ...(selection.shard ? { shard: selection.shard } : {}),
  };
}

function uniqueSelectorValues(optionName: string, values: string[]): string[] {
  const trimmed = values.map((value) => value.trim());
  if (trimmed.some((value) => value.length === 0)) {
    throw new Error(`${optionName} values must be non-empty.`);
  }
  return [...new Set(trimmed)];
}

function assertDisjointSelectors(
  includeName: string,
  includeValues: string[],
  excludeName: string,
  excludeValues: string[],
): void {
  const excludeSet = new Set(excludeValues);
  const overlaps = includeValues.filter((value) => excludeSet.has(value));
  if (overlaps.length > 0) {
    throw new Error(
      `${includeName} and ${excludeName} cannot contain the same value(s): ${overlaps.join(", ")}.`,
    );
  }
}

function validateKnownJourneyIds(
  items: RunnableJourney[],
  ids: string[],
  optionName: string,
): void {
  if (ids.length === 0) {
    return;
  }
  const knownIds = new Set(items.map((item) => item.journey.id));
  const unknown = ids.filter((id) => !knownIds.has(id));
  if (unknown.length > 0) {
    throw new Error(
      `${optionName} referenced journey id(s) that were not collected: ${unknown.join(", ")}.`,
    );
  }
}

export function parseDataOptions(values: string[]): Record<string, unknown> {
  const data: Record<string, unknown> = {};

  for (const value of values) {
    const equalsIndex = value.indexOf("=");
    if (equalsIndex <= 0) {
      throw new Error(
        `--data values must use key=value syntax. Received "${value}".`,
      );
    }

    const key = value.slice(0, equalsIndex).trim();
    if (!key) {
      throw new Error(
        `--data values must include a non-empty key. Received "${value}".`,
      );
    }

    const rawValue = value.slice(equalsIndex + 1);
    data[key] = parseJsonLikeValue(rawValue);
  }

  return data;
}

function parseJsonLikeValue(value: string): unknown {
  try {
    return JSON.parse(value) as unknown;
  } catch {
    return value;
  }
}

export function buildUiChangeRecordingOptions(options: {
  uiChangeTimeoutMs?: string;
  uiChangeQuietMs?: string;
  uiChangeMaxChanges?: string;
  uiChangeMaxScreenshots?: string;
  uiChangeScreenshots?: boolean;
  uiChangeSnapshots?: boolean;
  uiChangeDomSnapshots?: boolean;
}): UiChangeRecordingOptions | undefined {
  const recordingOptions: UiChangeRecordingOptions = {
    ...(options.uiChangeTimeoutMs
      ? {
          timeoutMs: parsePositiveIntegerOption(
            "--ui-change-timeout-ms",
            options.uiChangeTimeoutMs,
          ),
        }
      : {}),
    ...(options.uiChangeQuietMs
      ? {
          quietMs: parsePositiveIntegerOption(
            "--ui-change-quiet-ms",
            options.uiChangeQuietMs,
          ),
        }
      : {}),
    ...(options.uiChangeMaxChanges
      ? {
          maxChanges: parsePositiveIntegerOption(
            "--ui-change-max-changes",
            options.uiChangeMaxChanges,
          ),
        }
      : {}),
    ...(options.uiChangeMaxScreenshots
      ? {
          maxScreenshots: parseNonNegativeIntegerOption(
            "--ui-change-max-screenshots",
            options.uiChangeMaxScreenshots,
          ),
        }
      : {}),
    ...(options.uiChangeScreenshots === undefined
      ? {}
      : { screenshots: options.uiChangeScreenshots }),
    ...(options.uiChangeSnapshots === undefined
      ? {}
      : { snapshots: options.uiChangeSnapshots }),
    ...(options.uiChangeDomSnapshots === undefined
      ? {}
      : { domSnapshots: options.uiChangeDomSnapshots }),
  };

  return Object.keys(recordingOptions).length > 0
    ? recordingOptions
    : undefined;
}

export function parseJourneyTimeoutMs(value: string): number | undefined {
  if (value === "0") {
    return undefined;
  }
  return parsePositiveIntegerOption("--journey-timeout-ms", value);
}

function parsePositiveIntegerOption(name: string, value: string): number {
  const parsed = Number(value);
  if (!Number.isInteger(parsed) || parsed < 1) {
    throw new Error(`${name} must be a positive integer. Received "${value}".`);
  }
  return parsed;
}

function parseNonNegativeIntegerOption(name: string, value: string): number {
  const parsed = Number(value);
  if (!Number.isInteger(parsed) || parsed < 0) {
    throw new Error(
      `${name} must be a non-negative integer. Received "${value}".`,
    );
  }
  return parsed;
}

export function sessionNameForRun(
  baseSession: string | undefined,
  parallelAgents: number,
  journeyId: string,
  index: number,
): string | undefined {
  if (!baseSession) {
    return undefined;
  }

  if (parallelAgents <= 1) {
    return baseSession;
  }

  return `${baseSession}-${String(index + 1).padStart(2, "0")}-${sanitizePathSegment(journeyId)}`;
}

export function shouldUseSharedBrowserTabs(options: {
  browser: string;
  parallelAgents: number;
  journeyCount: number;
  parallelBrowserMode: ParallelBrowserMode;
}): boolean {
  return (
    options.browser === "agent-browser" &&
    options.parallelAgents > 1 &&
    options.journeyCount > 1 &&
    options.parallelBrowserMode === "shared-tabs"
  );
}

export function parallelBrowserSessionName(
  baseSession: string | undefined,
  suiteRunId: string,
): string {
  return baseSession ?? suiteRunId;
}

export function browserSessionNameForRun(options: {
  baseSession: string | undefined;
  parallelAgents: number;
  journeyId: string;
  index: number;
  suiteRunId: string;
  sharedBrowserTabs: boolean;
}): string | undefined {
  if (options.sharedBrowserTabs) {
    return parallelBrowserSessionName(options.baseSession, options.suiteRunId);
  }

  return sessionNameForRun(
    options.baseSession,
    options.parallelAgents,
    options.journeyId,
    options.index,
  );
}

export function browserTabLabelForRun(options: {
  parallelAgents: number;
  journeyId: string;
  index: number;
  sharedBrowserTabs: boolean;
}): string | undefined {
  if (!options.sharedBrowserTabs) {
    return undefined;
  }

  return `journey-${String(options.index + 1).padStart(2, "0")}-${sanitizePathSegment(options.journeyId)}`;
}

export function bookmarkCuratorNameForOptions(options: {
  bookmarkCurator?: string;
  curateBookmarks?: boolean;
}): string {
  if (options.curateBookmarks === false) {
    return "none";
  }

  return options.bookmarkCurator ?? "pi";
}

interface RunJourneyWithRetriesOptions {
  item: RunnableJourney;
  retries: number;
  outputDir: string;
  createDriver: () => BrowserDriver;
  createDirector: () => AgentDirector;
  createBookmarkCurator: () => BookmarkCurator | undefined;
  video?: boolean;
  directorTimeoutMs?: number;
  uiChangeRecording?: boolean;
  uiChangeRecordingOptions?: UiChangeRecordingOptions;
  sessionName?: string;
  tabLabel?: string;
  statePath?: string;
  headed?: boolean;
  browserEnvironment?: BrowserEnvironment;
  dataLifecycle?: RunJourneyOptions["dataLifecycle"];
}

async function runJourneyWithRetries(
  options: RunJourneyWithRetriesOptions,
): Promise<RunResult> {
  const maxAttempts = options.retries + 1;
  const attempts: RunAttempt[] = [];
  let finalResult: RunResult | undefined;

  for (let attempt = 1; attempt <= maxAttempts; attempt++) {
    if (attempt > 1) {
      console.log(
        `Retrying ${options.item.journey.id} (${attempt}/${maxAttempts})`,
      );
    }

    const result = await runJourney({
      journey: options.item.journey,
      profile: options.item.profile,
      driver: options.createDriver(),
      director: options.createDirector(),
      outputDir: options.outputDir,
      video: options.video,
      directorTimeoutMs: options.directorTimeoutMs,
      uiChangeRecording: options.uiChangeRecording,
      uiChangeRecordingOptions: options.uiChangeRecordingOptions,
      sessionName: attemptScopedValue(options.sessionName, attempt),
      tabLabel: attemptScopedValue(options.tabLabel, attempt),
      statePath: options.statePath,
      headed: options.headed,
      browserEnvironment: options.browserEnvironment,
      bookmarkCurator: options.createBookmarkCurator(),
      dataLifecycle: options.dataLifecycle,
    });

    attempts.push(createRunAttempt(attempt, result));
    finalResult = result;
    if (runPassed(result)) {
      break;
    }
  }

  if (!finalResult) {
    throw new Error(`No attempts were run for ${options.item.journey.id}.`);
  }

  const annotated = annotateFinalRunResult({
    result: finalResult,
    attempts,
    retries: options.retries,
    quarantined: options.item.journey.quarantined ?? false,
  });
  await persistRunResult(annotated);
  return annotated;
}

function annotateFinalRunResult(options: {
  result: RunResult;
  attempts: RunAttempt[];
  retries: number;
  quarantined: boolean;
}): RunResult {
  const passedAttempt = options.attempts.find((attempt) => attempt.passed);
  const failedAttemptsBeforePass = passedAttempt
    ? options.attempts.filter(
        (attempt) => attempt.attempt < passedAttempt.attempt && !attempt.passed,
      ).length
    : 0;

  return {
    ...options.result,
    attempts: options.attempts,
    flake: {
      isFlaky: Boolean(passedAttempt && failedAttemptsBeforePass > 0),
      retries: options.retries,
      attempts: options.attempts.length,
      ...(passedAttempt ? { passedAttempt: passedAttempt.attempt } : {}),
      failedAttemptsBeforePass,
    },
    ...(options.quarantined
      ? { quarantine: { quarantined: true } }
      : options.result.quarantine
        ? { quarantine: options.result.quarantine }
        : {}),
  };
}

function createRunAttempt(attempt: number, result: RunResult): RunAttempt {
  return {
    attempt,
    runId: result.runId,
    runStatus: result.runStatus,
    ...(result.verdict?.status ? { verdictStatus: result.verdict.status } : {}),
    ...(result.dataLifecycle?.status
      ? { dataLifecycleStatus: result.dataLifecycle.status }
      : {}),
    passed: runPassed(result),
    startedAt: result.startedAt,
    endedAt: result.endedAt,
    durationMs: result.durationMs,
    dashboard: result.artifacts.dashboard,
    result: result.artifacts.result,
    ...(result.verdict?.summary || result.error?.message
      ? { summary: result.verdict?.summary ?? result.error?.message }
      : {}),
  };
}

function runPassed(result: RunResult): boolean {
  return (
    result.runStatus === "completed" &&
    result.verdict?.status === "passed" &&
    result.dataLifecycle?.status !== "failed" &&
    result.dataLifecycle?.status !== "blocked"
  );
}

function attemptScopedValue(
  value: string | undefined,
  attempt: number,
): string | undefined {
  if (!value || attempt === 1) {
    return value;
  }
  return `${value}-attempt-${attempt}`;
}

async function persistRunResult(result: RunResult): Promise<void> {
  await writeFile(
    result.artifacts.result,
    `${JSON.stringify(result, null, 2)}\n`,
    "utf8",
  );
}

async function mapConcurrent<T, TResult>(
  items: T[],
  concurrency: number,
  mapper: (item: T, index: number) => Promise<TResult>,
): Promise<TResult[]> {
  const results = new Array<TResult>(items.length);
  let nextIndex = 0;
  const workerCount = Math.min(concurrency, items.length);

  await Promise.all(
    Array.from({ length: workerCount }, async () => {
      while (nextIndex < items.length) {
        const index = nextIndex++;
        results[index] = await mapper(items[index] as T, index);
      }
    }),
  );

  return results;
}

function createOAuthAuthLoader(
  authPath: string,
): (provider: string) => Promise<string | undefined> {
  let cachedAuth: StoredOAuthAuth | undefined;
  let queue: Promise<void> = Promise.resolve();

  return async (provider: string) => {
    const operation = queue.then(async () => {
      const auth = cachedAuth ?? (cachedAuth = await readOAuthAuth(authPath));
      const result = await getOAuthApiKey(provider, auth);
      if (!result) {
        return undefined;
      }

      if (auth[provider] !== result.newCredentials) {
        auth[provider] = { type: "oauth", ...result.newCredentials };
        cachedAuth = auth;
        await mkdir(dirname(authPath), { recursive: true });
        await writeFile(authPath, `${JSON.stringify(auth, null, 2)}\n`, "utf8");
      }

      return result.apiKey;
    });

    queue = operation.then(
      () => undefined,
      () => undefined,
    );
    return operation;
  };
}

export async function getOAuthAuthStatus(
  authPath: AuthPathResolution,
): Promise<OAuthAuthStatus> {
  const exists = await pathExists(authPath.path);
  const auth = await readOAuthAuth(authPath.path);
  return {
    path: authPath.path,
    source: authPath.source,
    exists,
    configuredProviders: Object.keys(auth).sort(),
    availableOAuthProviders: getOAuthProviders()
      .map((provider) => provider.id)
      .sort(),
  };
}

async function assertFactoryOAuthCredentialsAvailable(
  providers: Array<string | undefined>,
  authPath: AuthPathResolution,
): Promise<void> {
  const authProviders = new Set(
    providers.filter((provider): provider is string => Boolean(provider)),
  );
  await Promise.all(
    [...authProviders].map((provider) =>
      assertOAuthCredentialsAvailable(provider, authPath),
    ),
  );
}

async function assertOAuthCredentialsAvailable(
  provider: string,
  authPath: AuthPathResolution,
): Promise<void> {
  if (!getOAuthProvider(provider)) {
    return;
  }

  const auth = await readOAuthAuth(authPath.path);
  if (auth[provider]) {
    return;
  }

  throw new Error(formatMissingOAuthCredentialsMessage(provider, authPath));
}

async function readOAuthAuth(authPath: string): Promise<StoredOAuthAuth> {
  try {
    return JSON.parse(await readFile(authPath, "utf8")) as StoredOAuthAuth;
  } catch (error) {
    const code =
      typeof error === "object" && error && "code" in error
        ? error.code
        : undefined;
    if (code === "ENOENT") {
      return {};
    }
    const message = error instanceof Error ? error.message : String(error);
    if (error instanceof SyntaxError) {
      throw new Error(
        `Could not parse OAuth auth file at ${authPath}: ${message}`,
      );
    }
    throw new Error(
      `Could not read OAuth auth file at ${authPath}: ${message}`,
    );
  }
}

export async function resolveAuthPath(
  authPathOption?: string,
  options: AuthPathResolutionOptions = {},
): Promise<AuthPathResolution> {
  const cwd = options.cwd ?? process.cwd();
  const env = options.env ?? process.env;
  const localPath = resolve(cwd, "auth.json");
  const userConfigPath = defaultUserAuthPath({
    env,
    homeDir: options.homeDir,
    platform: options.platform,
  });

  if (authPathOption) {
    return {
      path: resolve(cwd, authPathOption),
      source: "cli",
      localPath,
      userConfigPath,
    };
  }

  if (env.JOURNEYTEST_AUTH_PATH) {
    return {
      path: resolve(cwd, env.JOURNEYTEST_AUTH_PATH),
      source: "env",
      localPath,
      userConfigPath,
    };
  }

  const fileExists = options.fileExists ?? pathExists;
  if (await fileExists(localPath)) {
    return {
      path: localPath,
      source: "local",
      localPath,
      userConfigPath,
    };
  }

  return {
    path: userConfigPath,
    source: "user-config",
    localPath,
    userConfigPath,
  };
}

export function defaultUserAuthPath(
  options: {
    env?: NodeJS.ProcessEnv;
    homeDir?: string;
    platform?: NodeJS.Platform;
  } = {},
): string {
  const env = options.env ?? process.env;
  const home = options.homeDir ?? homedir();
  const platform = options.platform ?? process.platform;

  if (platform === "darwin") {
    return join(
      home,
      "Library",
      "Application Support",
      "JourneyTest",
      "auth.json",
    );
  }
  if (platform === "win32") {
    return join(
      env.APPDATA ?? join(home, "AppData", "Roaming"),
      "JourneyTest",
      "auth.json",
    );
  }
  return join(
    env.XDG_CONFIG_HOME ?? join(home, ".config"),
    "journeytest",
    "auth.json",
  );
}

export function formatMissingOAuthCredentialsMessage(
  provider: string,
  authPath: AuthPathResolution,
): string {
  const authDir = dirname(authPath.path);
  const customFileNote =
    basename(authPath.path) === "auth.json"
      ? ""
      : `\nThe Pi login CLI writes auth.json; move it to ${authPath.path} after login if you keep this custom filename.`;
  return (
    [
      `No OAuth credentials found for "${provider}" in ${authPath.path} (${authPathSourceLabel(authPath.source)}).`,
      `Create credentials with:`,
      `  mkdir -p "${authDir}"`,
      `  (cd "${authDir}" && npx @earendil-works/pi-ai login ${provider})`,
      `Override the auth file with --auth <path> or JOURNEYTEST_AUTH_PATH.`,
      `Existing local auth files are still supported at ${authPath.localPath}.`,
    ].join("\n") + customFileNote
  );
}

function authPathSourceLabel(source: AuthPathSource): string {
  if (source === "cli") {
    return "--auth";
  }
  if (source === "env") {
    return "JOURNEYTEST_AUTH_PATH";
  }
  if (source === "local") {
    return "existing local auth.json";
  }
  return "user config";
}

async function pathExists(path: string): Promise<boolean> {
  try {
    await access(path);
    return true;
  } catch (error) {
    const code =
      typeof error === "object" && error && "code" in error
        ? error.code
        : undefined;
    if (code === "ENOENT") {
      return false;
    }
    throw error;
  }
}

async function validateFiles(
  journeyPath: string,
  profilePath: string,
): Promise<{ issueCount: number }> {
  const profileFiles = await collectJsonFiles(resolve(profilePath));
  const journeyFiles = await collectJsonFiles(resolve(journeyPath));

  const profiles = new Map<string, TesterProfile>();
  let issueCount = 0;

  for (const file of profileFiles) {
    try {
      const profile = await readAndParseJsonFile(file, TesterProfileSchema);
      profiles.set(profile.id, profile);
      console.log(`profile ok: ${file}`);
    } catch (error) {
      issueCount++;
      console.error(formatError(file, error));
    }
  }

  for (const file of journeyFiles) {
    try {
      const journey = await readAndParseJsonFile(file, UserJourneySchema);
      const profile = profiles.get(journey.testerProfile);
      const issues: ValidationIssue[] = [...validateAllowedOrigins(journey)];
      if (!profile) {
        issues.push({
          path: "testerProfile",
          message: `No profile with id "${journey.testerProfile}" was loaded.`,
        });
      } else {
        issues.push(...validateJourneyProfileMatch(journey, profile));
      }

      if (issues.length > 0) {
        issueCount += issues.length;
        console.error(`journey invalid: ${file}`);
        for (const issue of issues) {
          console.error(`  ${issue.path}: ${issue.message}`);
        }
      } else {
        console.log(`journey ok: ${file}`);
      }
    } catch (error) {
      issueCount++;
      console.error(formatError(file, error));
    }
  }

  console.log(
    `Validated ${profileFiles.length} profile(s), ${journeyFiles.length} journey file(s).`,
  );
  if (issueCount > 0) {
    console.error(`${issueCount} validation issue(s).`);
  }
  return { issueCount };
}

export async function lintFiles(
  journeyPath: string,
  profilePath?: string,
): Promise<{ issueCount: number }> {
  const journeyFiles = await collectJsonFiles(resolve(journeyPath));
  const profileFiles = profilePath
    ? await collectJsonFiles(resolve(profilePath))
    : [];
  const profiles = new Map<string, TesterProfile>();
  let issueCount = 0;

  for (const file of profileFiles) {
    try {
      const profile = await readAndParseJsonFile(file, TesterProfileSchema);
      profiles.set(profile.id, profile);
      console.log(`profile lint ok: ${file}`);
    } catch (error) {
      issueCount++;
      console.error(formatError(file, error));
    }
  }

  for (const file of journeyFiles) {
    try {
      const journey = await readAndParseJsonFile(file, UserJourneySchema);
      const issues: LintIssue[] = [];
      const profile = profilePath
        ? profiles.get(journey.testerProfile)
        : undefined;

      if (profilePath && !profile) {
        issues.push({
          severity: "error",
          code: "missing-profile",
          path: "testerProfile",
          message: `No profile with id "${journey.testerProfile}" was loaded.`,
        });
      }
      issues.push(...lintUserJourney(journey, profile ? { profile } : {}));

      if (issues.length > 0) {
        issueCount += issues.length;
        console.error(`journey lint issues: ${file}`);
        for (const issue of issues) {
          console.error(`  ${formatLintIssue(issue)}`);
        }
      } else {
        console.log(`journey lint ok: ${file}`);
      }
    } catch (error) {
      issueCount++;
      console.error(formatError(file, error));
    }
  }

  console.log(
    `Linted ${profileFiles.length} profile(s), ${journeyFiles.length} journey file(s).`,
  );
  if (issueCount > 0) {
    console.error(`${issueCount} lint issue(s).`);
  }
  return { issueCount };
}

async function collectRunnableJourneys(
  journeyPath: string,
  profilePath: string,
): Promise<RunnableJourney[]> {
  const journeyFiles = await collectJsonFiles(journeyPath);
  const profiles = await loadProfiles(profilePath);
  const items: RunnableJourney[] = [];
  const issues: string[] = [];

  for (const journeyFile of journeyFiles) {
    try {
      const journey = await readAndParseJsonFile(
        journeyFile,
        UserJourneySchema,
      );
      const profile = profiles.get(journey.testerProfile);
      if (!profile) {
        issues.push(
          `${journeyFile}: no profile with id "${journey.testerProfile}" was loaded from ${profilePath}.`,
        );
        continue;
      }

      const validationIssues = [
        ...validateAllowedOrigins(journey),
        ...validateJourneyProfileMatch(journey, profile),
      ];
      if (validationIssues.length > 0) {
        for (const issue of validationIssues) {
          issues.push(`${journeyFile}: ${issue.path}: ${issue.message}`);
        }
        continue;
      }

      items.push({ journeyFile, journey, profile });
    } catch (error) {
      issues.push(formatError(journeyFile, error));
    }
  }

  if (issues.length > 0) {
    throw new Error(
      `Cannot run journeys until collection issues are fixed:\n${issues.join("\n")}`,
    );
  }

  return items;
}

async function loadProfiles(
  profilePath: string,
): Promise<Map<string, TesterProfile>> {
  const profileFiles = await collectJsonFiles(profilePath);
  const profiles = new Map<string, TesterProfile>();
  const issues: string[] = [];

  for (const file of profileFiles) {
    try {
      const profile = await readAndParseJsonFile(file, TesterProfileSchema);
      profiles.set(profile.id, profile);
    } catch (error) {
      issues.push(formatError(file, error));
    }
  }

  if (issues.length > 0) {
    throw new Error(`Cannot load tester profiles:\n${issues.join("\n")}`);
  }

  return profiles;
}

async function loadComparisonBaseline(
  compareToPath: string,
): Promise<ComparisonBaseline> {
  const info = await stat(compareToPath);
  if (info.isFile()) {
    return {
      sourcePath: compareToPath,
      kind: "run",
      runs: [await readAndParseJsonFile(compareToPath, RunResultSchema)],
    };
  }

  const runFiles = await collectRunJsonFiles(compareToPath);
  if (runFiles.length === 0) {
    throw new Error(
      `No run.json files found under --compare-to directory ${compareToPath}.`,
    );
  }

  return {
    sourcePath: compareToPath,
    kind: "suite",
    runs: await Promise.all(
      runFiles.map((runFile) => readAndParseJsonFile(runFile, RunResultSchema)),
    ),
  };
}

export async function loadRerunFailedSelection(
  rerunFailedPath: string,
): Promise<RerunFailedSelection> {
  const snapshots = await loadRunSnapshotsForSelection(rerunFailedPath);
  const unhealthyJourneyIds = [
    ...new Set(
      snapshots
        .filter((snapshot) => runHealth(snapshot) !== "passed")
        .map((snapshot) => snapshot.journeyId),
    ),
  ];
  if (unhealthyJourneyIds.length === 0) {
    throw new Error(
      `--rerun-failed found no unhealthy journeys in ${rerunFailedPath}.`,
    );
  }
  return {
    sourcePath: rerunFailedPath,
    unhealthyJourneyIds,
  };
}

async function loadRunSnapshotsForSelection(path: string): Promise<RunSnapshot[]> {
  const info = await stat(path);
  if (info.isFile()) {
    if (basename(path) === "history.json") {
      return readHistorySnapshots(path);
    }
    return [snapshotRun(await readAndParseJsonFile(path, RunResultSchema))];
  }

  const historyPath = join(path, "history.json");
  if (await pathExists(historyPath)) {
    return readHistorySnapshots(historyPath);
  }

  const runFiles = await collectRunJsonFiles(path);
  if (runFiles.length === 0) {
    throw new Error(
      `No run.json or history.json files found under --rerun-failed directory ${path}.`,
    );
  }

  return Promise.all(
    runFiles.map(async (runFile) =>
      snapshotRun(await readAndParseJsonFile(runFile, RunResultSchema)),
    ),
  );
}

async function readHistorySnapshots(path: string): Promise<RunSnapshot[]> {
  let parsed: unknown;
  try {
    parsed = JSON.parse(await readFile(path, "utf8"));
  } catch (error) {
    const message = error instanceof Error ? error.message : String(error);
    throw new Error(`Could not parse history.json at ${path}: ${message}`);
  }

  if (!isRecord(parsed) || !Array.isArray(parsed.runs)) {
    throw new Error(`history.json at ${path} must contain a runs array.`);
  }

  return parsed.runs.map((run, index) =>
    parseHistoryRunSnapshot(run, `${path}: runs[${index}]`),
  );
}

function parseHistoryRunSnapshot(value: unknown, label: string): RunSnapshot {
  if (!isRecord(value)) {
    throw new Error(`${label} must be an object.`);
  }

  const snapshot = {
    journeyId: readRequiredString(value, "journeyId", label),
    runId: readRequiredString(value, "runId", label),
    runStatus: readRequiredString(value, "runStatus", label),
    verdictStatus: readRequiredString(value, "verdictStatus", label),
    dataLifecycleStatus: readRequiredString(
      value,
      "dataLifecycleStatus",
      label,
    ),
    startedAt: readRequiredString(value, "startedAt", label),
    endedAt: readRequiredString(value, "endedAt", label),
    durationMs:
      typeof value.durationMs === "number" && Number.isFinite(value.durationMs)
        ? value.durationMs
        : 0,
    ...(typeof value.summary === "string" ? { summary: value.summary } : {}),
    ...(typeof value.dashboard === "string"
      ? { dashboard: value.dashboard }
      : {}),
    ...(typeof value.result === "string" ? { result: value.result } : {}),
  } as RunSnapshot;
  assertRunSnapshotEnums(snapshot, label);
  return snapshot;
}

function readRequiredString(
  value: Record<string, unknown>,
  key: string,
  label: string,
): string {
  const field = value[key];
  if (typeof field !== "string" || field.length === 0) {
    throw new Error(`${label}.${key} must be a non-empty string.`);
  }
  return field;
}

function assertRunSnapshotEnums(snapshot: RunSnapshot, label: string): void {
  if (
    !["completed", "error", "cancelled", "blocked"].includes(
      snapshot.runStatus,
    )
  ) {
    throw new Error(`${label}.runStatus is not a valid run status.`);
  }
  if (
    !["passed", "failed", "blocked", "inconclusive", "none"].includes(
      snapshot.verdictStatus,
    )
  ) {
    throw new Error(`${label}.verdictStatus is not a valid verdict status.`);
  }
  if (
    !["passed", "failed", "blocked", "skipped", "none"].includes(
      snapshot.dataLifecycleStatus,
    )
  ) {
    throw new Error(
      `${label}.dataLifecycleStatus is not a valid data lifecycle status.`,
    );
  }
}

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}

async function writeSuiteArtifacts(
  outputDir: string,
  suiteRuns: SuiteDashboardRun[],
  comparisonBaseline?: ComparisonBaseline,
  selection?: SuiteSelectionSummary,
): Promise<{ dashboard: string; history: string }> {
  const suiteId =
    outputDir.split(/[\\/]/).at(-1) ?? `${isoTimestampForPath()}-run`;
  const generatedAt = new Date().toISOString();
  const suitePath = join(outputDir, "dashboard.html");
  const historyPath = join(outputDir, "history.json");
  const history = buildSuiteRunHistory({
    suiteId,
    generatedAt,
    runs: suiteRuns,
    ...(selection ? { selection } : {}),
    ...(comparisonBaseline
      ? {
          compareTo: {
            path: comparisonBaseline.sourcePath,
            kind: comparisonBaseline.kind,
            runs: comparisonBaseline.runs,
          },
        }
      : {}),
  });
  await writeHistoryArtifact(historyPath, history);
  await writeFile(
    suitePath,
    renderSuiteDashboard({
      suiteId,
      generatedAt,
      baseDir: outputDir,
      runs: suiteRuns,
      history,
      ...(selection ? { selection } : {}),
    }),
    "utf8",
  );
  return { dashboard: suitePath, history: historyPath };
}

async function writeHistoryArtifact(
  path: string,
  history: SuiteRunHistory,
): Promise<void> {
  await writeFile(path, `${JSON.stringify(history, null, 2)}\n`, "utf8");
}

async function writeCliCiOutputs(options: {
  suiteRunId: string;
  suiteRuns: SuiteDashboardRun[];
  junitPath?: string;
  summaryJsonPath?: string;
  githubAnnotations?: boolean;
}): Promise<void> {
  if (
    !options.junitPath &&
    !options.summaryJsonPath &&
    !options.githubAnnotations
  ) {
    return;
  }

  const generatedAt = new Date().toISOString();
  const written = await writeCiReports({
    suiteName: options.suiteRunId,
    generatedAt,
    runs: options.suiteRuns,
    ...(options.junitPath ? { junitPath: resolve(options.junitPath) } : {}),
    ...(options.summaryJsonPath
      ? { summaryJsonPath: resolve(options.summaryJsonPath) }
      : {}),
  });

  if (written.junitPath) {
    console.log(`JUnit report: ${written.junitPath}`);
  }
  if (written.summaryJsonPath) {
    console.log(`Summary JSON: ${written.summaryJsonPath}`);
  }
  if (options.githubAnnotations) {
    emitGitHubAnnotations(options.suiteRuns, process.stderr);
  }
}

function installAppLifecycleSignalCleanup(
  controller: AppLifecycleController,
): () => void {
  const signals: NodeJS.Signals[] = ["SIGINT", "SIGTERM"];
  let handling = false;
  const handler = async (signal: NodeJS.Signals) => {
    if (handling) {
      return;
    }
    handling = true;
    console.error(`Received ${signal}; running app lifecycle cleanup.`);
    await controller.runCleanup().catch((error: unknown) => {
      const message = error instanceof Error ? error.message : String(error);
      console.error(`App lifecycle cleanup failed: ${message}`);
    });
    process.exit(signal === "SIGINT" ? 130 : 143);
  };

  for (const signal of signals) {
    process.once(signal, handler);
  }

  return () => {
    for (const signal of signals) {
      process.off(signal, handler);
    }
  };
}

function printRunResult(result: Awaited<ReturnType<typeof runJourney>>): void {
  console.log(`Run: ${result.runId}`);
  console.log(`Run status: ${result.runStatus}`);
  if (result.verdict) {
    console.log(`Verdict: ${result.verdict.status}`);
  }
  if (result.dataLifecycle) {
    console.log(`Data lifecycle: ${result.dataLifecycle.status}`);
  }
  console.log(`Dashboard: ${result.artifacts.dashboard}`);
  console.log(
    `Dashboard URL: ${pathToFileURL(result.artifacts.dashboard).href}`,
  );
  console.log(`Report: ${result.artifacts.report}`);
  console.log(`Result: ${result.artifacts.result}`);
}

async function collectJsonFiles(path: string): Promise<string[]> {
  const info = await stat(path);
  if (info.isFile()) {
    return path.endsWith(".json") ? [path] : [];
  }

  const entries = await readdir(path, { withFileTypes: true });
  const files = await Promise.all(
    entries.map(async (entry) => {
      const child = resolve(path, entry.name);
      if (entry.isDirectory()) {
        return collectJsonFiles(child);
      }
      return child.endsWith(".json") ? [child] : [];
    }),
  );

  return files.flat().sort();
}

async function collectRunJsonFiles(path: string): Promise<string[]> {
  const info = await stat(path);
  if (info.isFile()) {
    return basename(path) === "run.json" ? [path] : [];
  }

  const entries = await readdir(path, { withFileTypes: true });
  const files = await Promise.all(
    entries.map(async (entry) => {
      const child = resolve(path, entry.name);
      if (entry.isDirectory()) {
        return collectRunJsonFiles(child);
      }
      return entry.name === "run.json" ? [child] : [];
    }),
  );

  return files.flat().sort();
}

function formatError(file: string, error: unknown): string {
  const message = error instanceof Error ? error.message : String(error);
  return `${file} is invalid:\n${message}`;
}

if (import.meta.url === `file://${process.argv[1]}`) {
  runCli().catch((error) => {
    console.error(error instanceof Error ? error.message : String(error));
    process.exitCode = 1;
  });
}

async function runServerCommand(name: "serve" | "ui" | "space", options: { host: string; port: string; cognitionProfile?: string }) {
  const port = Number(options.port); if (!Number.isInteger(port) || port <= 0) throw new Error("--port must be a positive integer");
  const loadedConfig = loadRuntimeCognitionConfig({ overrides: options.cognitionProfile ? { COGNITION_PROFILE: options.cognitionProfile } : undefined });
  assertCloudReasoningConfiguration(loadedConfig);
  const mode = resolveLayaMode(loadedConfig.localLaya.mode); let managed: ManagedLayaService | undefined;
  console.error("JourneyTest starting"); console.error(`Environment: ${isHuggingFaceSpace() ? "Hugging Face Space" : "local"}`); console.error(`Laya mode: ${mode}`); if (loadedConfig.cloud.enabled) { const resolution = reasoningModelStatus(loadedConfig); console.error(`Reasoning transport: ${loadedConfig.cloud.transport}`); if (loadedConfig.cloud.openAiCompatibleUrl) console.error(`Reasoning endpoint: ${new URL(loadedConfig.cloud.openAiCompatibleUrl).host}`); else console.error(`Reasoning provider: ${loadedConfig.cloud.provider}`); console.error(`Reasoning model: ${loadedConfig.cloud.model}`); console.error(`Credential configured: ${cognitionConfigStatus(loadedConfig).reasoning.credential_configured ? "yes" : "no"}`); console.error(`Reasoning model resolved: ${resolution.provider}/${resolution.model}`); }
  if (name === "space" && mode === "embedded") { console.error("Starting localdecide"); managed = new ManagedLayaService(); await managed.start(); console.error("Laya ready"); }
  if (name === "space") { await checkBrowserLaunch(); console.error("Browser ready"); }
  const runtime = createJourneyRuntime({ endpoint: managed?.endpoint, cognitionOverrides: options.cognitionProfile ? { COGNITION_PROFILE: options.cognitionProfile } : undefined }); await runtime.backend.start();
  const useGradio = name !== "serve"; const apiPort = useGradio ? port + 1 : port; const server = createApiServer({ service: runtime.service, backend: runtime.backend, model: runtime.config.localLaya.model, cognitionStatus: cognitionConfigStatus(runtime.config) });
  await new Promise<void>((resolveReady, reject) => { server.once("error", reject); server.listen(apiPort, useGradio ? "127.0.0.1" : options.host, resolveReady); });
  console.error("API mounted");
  let child: ReturnType<typeof spawn> | undefined;
  if (useGradio) { const script = resolve(dirname(fileURLToPath(import.meta.url)), "../python/journeytest_web.py"); child = spawn(process.env.JOURNEYTEST_PYTHON ?? "python3", [script], { stdio: "inherit", env: { ...process.env, JOURNEYTEST_API_BASE: `http://127.0.0.1:${apiPort}`, JOURNEYTEST_HOST: options.host, JOURNEYTEST_PORT: String(port) } }); }
  try { if (useGradio && child) { await waitForGradioReadiness(`http://127.0.0.1:${port}/api/v1/health`, 30_000, child); console.error("Gradio ready"); } }
  catch (error) { child?.kill("SIGTERM"); await new Promise<void>(resolveClose => server.close(() => resolveClose())); await runtime.backend.close(); await managed?.stop(); throw error; }
  console.error(`JourneyTest ready on :${port}`);
  await new Promise<void>(resolveStop => { const stop = () => resolveStop(); process.once("SIGINT", stop); process.once("SIGTERM", stop); child?.once("exit", () => resolveStop()); });
  child?.kill("SIGTERM"); await new Promise<void>(resolveClose => server.close(() => resolveClose())); await runtime.backend.close(); await managed?.stop();
}

async function runDoctor(smokeModel = false, json = false, smokeReasoning = false) {
  const checks: Array<{ name: string; ok: boolean; detail: string }> = [];
  checks.push({ name: "JourneyTest version", ok: true, detail: "0.1.2" }); checks.push({ name: "Node version", ok: true, detail: process.version }); checks.push({ name: "environment", ok: true, detail: isHuggingFaceSpace() ? "Hugging Face Space" : "local" }); checks.push({ name: "Space detection", ok: true, detail: isHuggingFaceSpace() ? "yes" : "no" });
  try { const { stdout } = await promisify(execFile)(process.env.JOURNEYTEST_PYTHON ?? "python3", ["--version"]); checks.push({ name: "Python", ok: true, detail: stdout.trim() }); } catch (error) { checks.push({ name: "Python", ok: false, detail: String(error) }); }
  try { const { stdout } = await promisify(execFile)("agent-browser", ["--version"]); checks.push({ name: "browser executable", ok: true, detail: stdout.trim() }); } catch (error) { checks.push({ name: "browser executable", ok: false, detail: String(error) }); }
  try { await checkBrowserLaunch(); checks.push({ name: "browser launch", ok: true, detail: "about:blank" }); } catch (error) { checks.push({ name: "browser launch", ok: false, detail: String(error) }); }
  try { await promisify(execFile)("localdecide", ["--help"]); checks.push({ name: "localdecide availability", ok: true, detail: "installed" }); } catch (error) { checks.push({ name: "localdecide availability", ok: false, detail: String(error) }); }
  try { await mkdir(resolve(process.env.JOURNEYTEST_OUTPUT ?? "runs"), { recursive: true }); await access(resolve(process.env.JOURNEYTEST_OUTPUT ?? "runs")); checks.push({ name: "artifact directory", ok: true, detail: resolve(process.env.JOURNEYTEST_OUTPUT ?? "runs") }); } catch (error) { checks.push({ name: "artifact directory", ok: false, detail: String(error) }); }
  checks.push({ name: "Laya backend", ok: true, detail: process.env.LAYA_BACKEND ?? "torch" }); checks.push({ name: "Laya checkpoint", ok: true, detail: process.env.LAYA_MODEL_REPO ?? "ichenney/laya-browser-v32b" }); checks.push({ name: "checkpoint revision", ok: true, detail: process.env.LAYA_MODEL_REVISION ?? "unpinned at runtime" });
  const cognition = loadRuntimeCognitionConfig(); const modelStatus = reasoningModelStatus(cognition); const cognitionStatus = cognitionConfigStatus(cognition); checks.push({ name: "cloud reasoning configuration", ok: !cognition.cloud.enabled || (modelStatus.modelResolved && (cognitionStatus.reasoning.credential_configured || (cognition.cloud.transport === "openai-compatible" && !cognition.cloud.authRequired))), detail: cognition.cloud.enabled ? `${cognition.cloud.provider}/${cognition.cloud.model}; resolved=${modelStatus.modelResolved}; credential=${cognitionStatus.reasoning.credential_configured ? "configured" : "missing"}` : "disabled" });
  let runtime: ReturnType<typeof createJourneyRuntime> | undefined; try { runtime = createJourneyRuntime({ config: cognition }); } catch (error) { checks.push({ name: "cloud reasoning startup validation", ok: false, detail: error instanceof Error ? error.message : String(error) }); }
  const health = runtime ? await runtime.backend.health() : { ok: false, detail: "runtime configuration invalid" }; checks.push({ name: "SystemOne endpoint", ok: health.ok, detail: `${runtime?.endpoint ?? cognition.localLaya.endpoint} (${health.backend ?? health.detail ?? "unavailable"})` });
  if (smokeModel && health.ok && runtime) { try { const smoke = await runtime.backend.infer({ state: { page: { url: "about:blank", title: "Doctor", text: "Ready" }, recent_actions: [] }, questions: { operation: { type: "choice", instructions: "Readiness smoke test", criteria: { DONE: "Ready", BLOCKED: "Not ready" } } } }); checks.push({ name: "model readiness", ok: true, detail: `${smoke.backend}, ${smoke.latencyMs} ms` }); } catch (error) { checks.push({ name: "model readiness", ok: false, detail: error instanceof Error ? error.message : String(error) }); } } else checks.push({ name: "model readiness", ok: !smokeModel, detail: smokeModel ? "SystemOne endpoint unavailable" : "not loaded (no unexpected download)" });
  if (smokeReasoning && runtime && cognition.cloud.enabled) { const started = Date.now(); try { const smoke = await runtime.cognitionRouter.initializeJourney({ goal: "Confirm reasoning readiness", successCriteria: [], context: {}, signal: new AbortController().signal }); checks.push({ name: "cloud reasoning live smoke", ok: true, detail: `${smoke.metadata.provider}/${smoke.metadata.model}, ${Date.now() - started} ms` }); } catch (error) { checks.push({ name: "cloud reasoning live smoke", ok: false, detail: error instanceof Error ? error.message : String(error) }); } } else checks.push({ name: "cloud reasoning live smoke", ok: true, detail: smokeReasoning ? "cloud reasoning disabled or invalid" : "not requested" }); checks.push({ name: "API dependencies", ok: true, detail: "node:http; Gradio/FastAPI checked by ui/space startup" });
  const disk = await statfs(resolve(process.env.JOURNEYTEST_OUTPUT ?? "runs")); checks.push({ name: "available disk", ok: disk.bavail > 0, detail: `${Math.round(Number(disk.bavail * disk.bsize) / 1024 ** 3)} GiB` }); checks.push({ name: "available memory", ok: freemem() > 0, detail: `${Math.round(freemem() / 1024 ** 3)} / ${Math.round(totalmem() / 1024 ** 3)} GiB` });
  if (json) process.stdout.write(`${JSON.stringify({ ok: checks.every(check => check.ok), checks })}\n`); else for (const check of checks) console.log(`${check.ok ? "OK" : "FAIL"} ${check.name}: ${check.detail}`); if (checks.some(check => !check.ok)) process.exitCode = 1;
}

async function checkBrowserLaunch() { const run = promisify(execFile); await run("agent-browser", ["--session", `journeytest-health-${process.pid}`, "open", "about:blank"], { timeout: 30_000 }); await run("agent-browser", ["--session", `journeytest-health-${process.pid}`, "close"], { timeout: 30_000 }).catch(() => undefined); }
async function waitForHttp(url: string, timeoutMs: number) { const deadline = Date.now() + timeoutMs; while (Date.now() < deadline) { try { if ((await fetch(url)).ok) return; } catch {} await new Promise(resolveWait => setTimeout(resolveWait, 250)); } throw new Error(`Timed out waiting for ${url}`); }
export async function waitForGradioReadiness(url: string, timeoutMs: number, child: Pick<ReturnType<typeof spawn>, "once">, wait = waitForHttp) { const exited = new Promise<never>((_, reject) => child.once("exit", (code, signal) => reject(new Error(`Gradio process exited before readiness (exit code ${code ?? "unknown"}${signal ? `, signal ${signal}` : ""})`)))); await Promise.race([wait(url, timeoutMs), exited]); }