rslph 0.1.1

CLI tool for LLM-powered autonomous task execution
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
//! Eval command handler.
//!
//! Orchestrates plan+build execution in persistent eval directories
//! for controlled benchmarking.

use chrono::Utc;
use color_eyre::eyre::eyre;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio_util::sync::CancellationToken;

/// Callback type for reporting build iteration progress.
/// Parameters: (current_iteration, max_iterations)
pub type ProgressCallback = Arc<dyn Fn(u32, u32) + Send + Sync>;

use crate::build::run_build_command;
use crate::build::tokens::{format_tokens, TokenUsage};
use crate::config::Config;
use crate::eval::{load_test_cases, TestResults, TestRunner};
use crate::planning::run_plan_command;
use crate::progress::ProgressFile;
use crate::prompts::{test_discovery_prompt, PromptMode};
use crate::subprocess::{ClaudeRunner, OutputLine, StreamResponse};
use crate::tui::run_dashboard_tui;

use super::parallel::{run_parallel_evals, TrialEvent, TrialResult as ParallelTrialResult};
use super::{EvalResult, StatSummary, TrialStatistics};

/// Run the eval command (EVAL-01, EVAL-05, EVAL-06).
///
/// Executes plan and build in a persistent eval directory,
/// collecting metrics for tokens and timing. Results are saved
/// to `result.json` in the workspace.
///
/// # Arguments
///
/// * `project` - Path to project directory to evaluate
/// * `trials` - Number of independent trials to run
/// * `modes` - Optional list of prompt modes to evaluate (parallel when > 1)
/// * `_keep` - Deprecated: workspaces are always persisted now
/// * `no_tui` - If true, disable TUI output
/// * `config` - Application configuration
/// * `cancel_token` - Token for graceful cancellation
///
/// # Returns
///
/// * `Ok(EvalResult)` - Eval completed with metrics
/// * `Err(e)` - Eval failed
pub async fn run_eval_command(
    project: String,
    trials: u32,
    modes: Option<Vec<PromptMode>>,
    _keep: bool, // Deprecated: always persist
    no_tui: bool,
    config: &Config,
    cancel_token: CancellationToken,
) -> color_eyre::Result<EvalResult> {
    // Resolve modes: use provided list, or fall back to config default
    let resolved_modes = modes.unwrap_or_else(|| vec![config.prompt_mode]);

    // If multiple modes or multiple trials, use parallel execution
    if resolved_modes.len() > 1 {
        return run_parallel_eval_mode(
            &project,
            trials,
            &resolved_modes,
            no_tui,
            config,
            cancel_token,
        )
        .await;
    }

    // Single mode - use existing sequential behavior
    // Get the single mode (we know there's exactly one)
    let mode = resolved_modes[0];

    // Execute trials
    let mut trial_results = Vec::with_capacity(trials as usize);

    for trial_num in 1..=trials {
        if trials > 1 {
            println!("\n=== TRIAL {}/{} ===\n", trial_num, trials);
        }
        let result = run_single_trial(
            &project,
            trial_num,
            mode,
            no_tui,
            config,
            cancel_token.clone(),
            None,
        )
        .await?;
        trial_results.push(result);
    }

    // For multi-trial runs, compute and print statistics, and save results
    if trials > 1 {
        let statistics = compute_statistics(&trial_results);
        print_statistics(&statistics, trials);

        // Save multi-trial results to JSON file (EVAL-08)
        let result_path =
            save_multi_trial_result(&config.eval_dir, &project, &trial_results, &statistics)?;
        println!("\nResults saved to: {}", result_path.display());
    }

    // Return the last trial result (for backward compatibility with single-trial case)
    // The caller can access all results through the statistics if needed
    trial_results
        .pop()
        .ok_or_else(|| eyre!("No trials completed"))
}

/// Run evals across multiple modes in parallel.
///
/// This function handles parallel execution when multiple modes are specified.
/// It uses tokio::JoinSet and channels to coordinate parallel trials.
///
/// # Arguments
///
/// * `project` - Path to project directory to evaluate
/// * `trials_per_mode` - Number of trials per mode
/// * `modes` - List of prompt modes to evaluate
/// * `no_tui` - If true, disable TUI output
/// * `config` - Application configuration
/// * `cancel_token` - Token for graceful cancellation
///
/// # Returns
///
/// * `Ok(EvalResult)` - Last trial result for backward compatibility
/// * `Err(e)` - Parallel eval failed
async fn run_parallel_eval_mode(
    project: &str,
    trials_per_mode: u32,
    modes: &[PromptMode],
    no_tui: bool,
    config: &Config,
    cancel_token: CancellationToken,
) -> color_eyre::Result<EvalResult> {
    use std::collections::HashMap;
    use tokio::sync::mpsc;

    if no_tui {
        println!(
            "\n=== PARALLEL EVAL: {} modes x {} trials = {} total trials ===\n",
            modes.len(),
            trials_per_mode,
            modes.len() as u32 * trials_per_mode
        );
    }

    // Create channel for trial events
    let (event_tx, event_rx) = mpsc::unbounded_channel::<TrialEvent>();

    // Spawn TUI or print-based event handler based on no_tui flag
    let tui_handle = if !no_tui {
        // Spawn dashboard TUI
        let modes_clone = modes.to_vec();
        let cancel_clone = cancel_token.clone();
        Some(tokio::spawn(async move {
            if let Err(e) =
                run_dashboard_tui(modes_clone, trials_per_mode, event_rx, cancel_clone).await
            {
                eprintln!("Dashboard error: {}", e);
            }
        }))
    } else {
        // Spawn print-based event handler for no-TUI mode
        let mut event_rx = event_rx;
        Some(tokio::spawn(async move {
            while let Some(event) = event_rx.recv().await {
                match &event.event {
                    super::parallel::TrialEventKind::Started => {
                        println!(
                            "[{}/{}] {} - Started",
                            event.mode, event.trial_num, event.mode
                        );
                    }
                    super::parallel::TrialEventKind::Planning => {
                        println!("[{}/{}] Planning...", event.mode, event.trial_num);
                    }
                    super::parallel::TrialEventKind::Building {
                        iteration,
                        max_iterations,
                    } => {
                        println!(
                            "[{}/{}] Building iteration {}/{}",
                            event.mode, event.trial_num, iteration, max_iterations
                        );
                    }
                    super::parallel::TrialEventKind::Testing => {
                        println!("[{}/{}] Testing...", event.mode, event.trial_num);
                    }
                    super::parallel::TrialEventKind::Complete { result } => {
                        let pass_rate = result
                            .eval_result
                            .test_results
                            .as_ref()
                            .map(|tr| tr.pass_rate())
                            .unwrap_or(0.0);
                        println!(
                            "[{}/{}] Complete - {:.1}% pass rate",
                            event.mode, event.trial_num, pass_rate
                        );
                    }
                    super::parallel::TrialEventKind::Failed { error } => {
                        println!("[{}/{}] FAILED: {}", event.mode, event.trial_num, error);
                    }
                }
            }
        }))
    };

    // Run parallel evals
    let results = run_parallel_evals(
        modes.to_vec(),
        trials_per_mode,
        project.to_string(),
        false, // keep
        no_tui,
        config.clone(),
        event_tx,
        cancel_token,
    )
    .await;

    // Wait for TUI/event handler to finish
    if let Some(handle) = tui_handle {
        // Don't wait forever - the handle will finish when the channel closes
        let _ = handle.await;
    }

    if results.is_empty() {
        return Err(eyre!("No trials completed successfully"));
    }

    // Group results by mode
    let mut by_mode: HashMap<PromptMode, Vec<&ParallelTrialResult>> = HashMap::new();
    for result in &results {
        by_mode.entry(result.mode).or_default().push(result);
    }

    // Print statistics for each mode
    println!("\n=== RESULTS BY MODE ===\n");
    for mode in modes {
        if let Some(mode_results) = by_mode.get(mode) {
            let eval_results: Vec<EvalResult> =
                mode_results.iter().map(|r| r.eval_result.clone()).collect();
            let statistics = compute_statistics(&eval_results);

            println!("--- {} ---", mode);
            print_statistics(&statistics, mode_results.len() as u32);
            println!();
        }
    }

    // Save multi-mode results to JSON
    let all_eval_results: Vec<EvalResult> = results.iter().map(|r| r.eval_result.clone()).collect();
    let _combined_stats = compute_statistics(&all_eval_results);
    let result_path = save_multi_mode_result(&config.eval_dir, project, modes, &results, &by_mode)?;
    println!("Results saved to: {}", result_path.display());

    // Return last result for backward compatibility
    results
        .into_iter()
        .last()
        .map(|r| r.eval_result)
        .ok_or_else(|| eyre!("No trials completed"))
}

/// Save multi-mode results to JSON file.
fn save_multi_mode_result(
    eval_dir: &Path,
    project: &str,
    modes: &[PromptMode],
    results: &[ParallelTrialResult],
    by_mode: &std::collections::HashMap<PromptMode, Vec<&ParallelTrialResult>>,
) -> color_eyre::Result<PathBuf> {
    // Generate filename: eval-results-{project}-multimode-{YYYY-MM-DD}.json
    let filename = format!(
        "eval-results-{}-multimode-{}.json",
        project,
        Utc::now().format("%Y-%m-%d-%H%M%S")
    );
    let path = eval_dir.join(&filename);

    // Build mode results
    let mode_results: Vec<SerializableModeResult> = modes
        .iter()
        .filter_map(|mode| {
            by_mode.get(mode).map(|mode_trials| {
                let eval_results: Vec<EvalResult> =
                    mode_trials.iter().map(|r| r.eval_result.clone()).collect();
                let statistics = compute_statistics(&eval_results);

                SerializableModeResult {
                    mode: mode.to_string(),
                    trial_count: mode_trials.len() as u32,
                    trials: mode_trials
                        .iter()
                        .map(|r| convert_trial_to_serializable(&r.eval_result))
                        .collect(),
                    statistics: convert_statistics_to_serializable(&statistics),
                }
            })
        })
        .collect();

    let result = SerializableMultiModeResult {
        project: project.to_string(),
        timestamp: Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string(),
        modes: modes.iter().map(|m| m.to_string()).collect(),
        total_trials: results.len() as u32,
        results_by_mode: mode_results,
    };

    // Write pretty-printed JSON
    let json = serde_json::to_string_pretty(&result)?;
    std::fs::write(&path, json)?;

    Ok(path)
}

/// Serializable multi-mode result for JSON output.
#[derive(Debug, Serialize, Deserialize)]
struct SerializableMultiModeResult {
    project: String,
    timestamp: String,
    modes: Vec<String>,
    total_trials: u32,
    results_by_mode: Vec<SerializableModeResult>,
}

/// Serializable mode result for JSON output.
#[derive(Debug, Serialize, Deserialize)]
struct SerializableModeResult {
    mode: String,
    trial_count: u32,
    trials: Vec<SerializableTrialSummary>,
    statistics: SerializableStatistics,
}

/// Run a single trial of the eval command.
///
/// This helper function contains the core eval logic:
/// 1. Resolve project (built-in or external path)
/// 2. Create persistent eval workspace
/// 3. Extract/copy project files
/// 4. Initialize git
/// 5. Detect prompt
/// 6. Run plan command
/// 7. Run build command
/// 8. Aggregate tokens
/// 9. Run hidden tests (for built-in projects)
/// 10. Save result.json
///
/// # Arguments
///
/// * `project` - Path to project directory to evaluate
/// * `trial_num` - Trial number (1-indexed, used in workspace naming)
/// * `no_tui` - If true, disable TUI output
/// * `config` - Application configuration
/// * `cancel_token` - Token for graceful cancellation
///
/// # Returns
///
/// * `Ok(EvalResult)` - Trial completed with metrics
/// * `Err(e)` - Trial failed
async fn run_single_trial(
    project: &str,
    trial_num: u32,
    mode: PromptMode,
    _no_tui: bool,
    config: &Config,
    cancel_token: CancellationToken,
    progress_callback: Option<ProgressCallback>,
) -> color_eyre::Result<EvalResult> {
    let start = Instant::now();

    // Step 1: Resolve project - check if built-in or external path
    let (is_builtin_project, project_source) = if crate::eval::is_builtin(project) {
        (true, None)
    } else {
        let path = PathBuf::from(&project);
        if !path.exists() {
            return Err(eyre!(
                "Project '{}' is neither a built-in project nor a valid path",
                project
            ));
        }
        (false, Some(path))
    };

    // Step 2: Create persistent eval workspace in config.eval_dir
    let project_name = if is_builtin_project {
        project.to_string()
    } else {
        project_source
            .as_ref()
            .unwrap()
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("project")
            .to_string()
    };

    // Create timestamped directory name with trial suffix
    let timestamp = Utc::now().format("%Y%m%d-%H%M%S");
    let workspace_name = format!("{}-{}-trial{}", project_name, timestamp, trial_num);
    let working_dir = config.eval_dir.join(&workspace_name);

    // Ensure eval_dir exists and create workspace
    std::fs::create_dir_all(&working_dir)?;

    println!("Eval workspace: {}", working_dir.display());

    // Step 3: Copy/extract project files to temp directory
    if is_builtin_project {
        let proj = crate::eval::get_project(project)
            .ok_or_else(|| eyre!("Built-in project not found: {}", project))?;
        crate::eval::extract_project_files(proj, &working_dir)?;
        println!("Extracted built-in project: {}", project);
    } else {
        copy_dir_recursive(project_source.as_ref().unwrap(), &working_dir)?;
        println!("Copied project files to workspace");
    }

    // Step 4: Initialize git in workspace (required for VCS tracking)
    init_git_repo(&working_dir)?;

    // Step 5: Detect starting prompt
    let prompt = detect_eval_prompt(&working_dir)?;
    println!("Detected prompt: {} chars", prompt.len());

    // Step 6: Run plan command and capture tokens
    println!("\n=== PLANNING PHASE ===\n");
    let timeout = Duration::from_secs(config.max_iterations as u64 * 600);
    let (progress_path, plan_tokens) = run_plan_command(
        &prompt,
        false, // not adaptive
        false, // not tui
        mode,
        config,
        &working_dir,
        cancel_token.clone(),
        timeout,
    )
    .await?;

    println!(
        "Planning tokens: In: {} | Out: {} | CacheW: {} | CacheR: {}",
        format_tokens(plan_tokens.input_tokens),
        format_tokens(plan_tokens.output_tokens),
        format_tokens(plan_tokens.cache_creation_input_tokens),
        format_tokens(plan_tokens.cache_read_input_tokens),
    );

    // Step 7: Run build command and capture tokens
    println!("\n=== BUILD PHASE ===\n");
    let build_tokens = run_build_command(
        progress_path.clone(),
        false, // not once
        false, // not dry-run
        true,  // force no-tui for eval to get clean output
        mode,
        config,
        cancel_token.clone(),
        progress_callback,
    )
    .await?;

    println!(
        "Build tokens: In: {} | Out: {} | CacheW: {} | CacheR: {}",
        format_tokens(build_tokens.input_tokens),
        format_tokens(build_tokens.output_tokens),
        format_tokens(build_tokens.cache_creation_input_tokens),
        format_tokens(build_tokens.cache_read_input_tokens),
    );

    // Step 8: Aggregate tokens from plan + build
    let total_tokens = TokenUsage {
        input_tokens: plan_tokens.input_tokens + build_tokens.input_tokens,
        output_tokens: plan_tokens.output_tokens + build_tokens.output_tokens,
        cache_creation_input_tokens: plan_tokens.cache_creation_input_tokens
            + build_tokens.cache_creation_input_tokens,
        cache_read_input_tokens: plan_tokens.cache_read_input_tokens
            + build_tokens.cache_read_input_tokens,
    };

    // Step 9: Collect metrics from progress file
    let progress = ProgressFile::load(&progress_path)?;
    let iterations = progress.iteration_log.len() as u32;

    let elapsed_secs = start.elapsed().as_secs_f64();

    // Step 10: Execute hidden tests for built-in projects (EVAL-02, EVAL-03)
    let test_results = if is_builtin_project {
        run_project_tests(project, &working_dir, config, cancel_token).await
    } else {
        None // External projects don't have hidden tests
    };

    // Step 11: Save result.json to workspace (EVAL-06)
    let result = EvalResult {
        project: project.to_string(),
        mode,
        trial_num,
        elapsed_secs,
        total_tokens: total_tokens.clone(),
        iterations,
        workspace_path: Some(working_dir.clone()),
        test_results: test_results.clone(),
    };
    save_result_json(&working_dir, &result)?;
    println!(
        "\nResults saved to: {}",
        working_dir.join("result.json").display()
    );

    Ok(result)
}

/// Run a single trial with a specific prompt mode.
///
/// This is the mode-aware version of run_single_trial for parallel evaluation.
/// It allows running trials with different prompt modes (basic, gsd, gsd_tdd).
///
/// # Arguments
///
/// * `project` - Path to project directory to evaluate
/// * `trial_num` - Trial number (1-indexed, used in workspace naming)
/// * `mode` - The prompt mode to use for this trial
/// * `no_tui` - If true, disable TUI output
/// * `config` - Application configuration
/// * `cancel_token` - Token for graceful cancellation
///
/// # Returns
///
/// * `Ok(EvalResult)` - Trial completed with metrics
/// * `Err(e)` - Trial failed
pub async fn run_single_trial_with_mode(
    project: &str,
    trial_num: u32,
    mode: PromptMode,
    no_tui: bool,
    config: &Config,
    cancel_token: CancellationToken,
    progress_callback: Option<ProgressCallback>,
) -> color_eyre::Result<EvalResult> {
    // Forward to run_single_trial with the mode parameter
    run_single_trial(
        project,
        trial_num,
        mode,
        no_tui,
        config,
        cancel_token,
        progress_callback,
    )
    .await
}

/// Re-run only the test phase on an existing eval workspace.
///
/// This is useful when:
/// - The build completed successfully
/// - But the test run script had a bug
/// - User fixed the script manually
/// - Now they want to re-run just the tests
///
/// # Arguments
///
/// * `workspace` - Path to existing eval workspace directory
/// * `config` - Application configuration
/// * `cancel_token` - Token for graceful cancellation
///
/// # Returns
///
/// * `Ok(EvalResult)` - Retest completed with updated metrics
/// * `Err(e)` - Retest failed
pub async fn run_retest_command(
    workspace: PathBuf,
    config: &Config,
    cancel_token: CancellationToken,
) -> color_eyre::Result<EvalResult> {
    let start = Instant::now();

    // Verify workspace exists
    if !workspace.exists() {
        return Err(eyre!(
            "Workspace directory does not exist: {}",
            workspace.display()
        ));
    }

    // Load existing result.json to get project name and previous metrics
    let result_path = workspace.join("result.json");
    if !result_path.exists() {
        return Err(eyre!(
            "No result.json found in workspace. Is this a valid eval workspace?\n\
             Expected: {}",
            result_path.display()
        ));
    }

    let existing_result = load_result_json(&result_path)?;
    let project = existing_result.project.clone();

    println!("Retest workspace: {}", workspace.display());
    println!("Project: {}", project);

    // Check if this is a built-in project
    if !crate::eval::is_builtin(&project) {
        return Err(eyre!(
            "Retest is only supported for built-in projects.\n\
             Project '{}' is not a built-in project.",
            project
        ));
    }

    // Run tests
    let test_results = run_project_tests(&project, &workspace, config, cancel_token).await;

    let elapsed_secs = start.elapsed().as_secs_f64();

    // Build result with original metrics but updated test results
    let result = EvalResult {
        project: existing_result.project,
        mode: existing_result.mode,
        trial_num: 1, // Retest is always a single-trial operation
        elapsed_secs: existing_result.elapsed_secs, // Keep original timing
        total_tokens: TokenUsage {
            input_tokens: existing_result.tokens.input,
            output_tokens: existing_result.tokens.output,
            cache_creation_input_tokens: existing_result.tokens.cache_creation,
            cache_read_input_tokens: existing_result.tokens.cache_read,
        },
        iterations: existing_result.iterations,
        workspace_path: Some(workspace.clone()),
        test_results: test_results.clone(),
    };

    // Save updated result.json
    save_result_json(&workspace, &result)?;

    println!("\nRetest completed in {:.1}s", elapsed_secs);
    println!("Results saved to: {}", result_path.display());

    Ok(result)
}

/// Load result.json from workspace directory.
fn load_result_json(path: &PathBuf) -> color_eyre::Result<StoredResult> {
    let content = std::fs::read_to_string(path)?;
    let result: StoredResult = serde_json::from_str(&content)?;
    Ok(result)
}

/// Stored result format (matches what we write to result.json).
#[derive(Debug, Deserialize)]
struct StoredResult {
    project: String,
    #[serde(default)]
    mode: PromptMode,
    elapsed_secs: f64,
    iterations: u32,
    tokens: StoredTokens,
    #[allow(dead_code)]
    test_results: Option<StoredTestResults>,
}

#[derive(Debug, Deserialize)]
struct StoredTokens {
    input: u64,
    output: u64,
    cache_creation: u64,
    cache_read: u64,
}

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct StoredTestResults {
    passed: u32,
    total: u32,
    pass_rate: f64,
}

/// Serializable result for JSON output.
#[derive(Debug, Serialize)]
struct SerializableResult {
    project: String,
    mode: PromptMode,
    elapsed_secs: f64,
    iterations: u32,
    tokens: SerializableTokens,
    test_results: Option<SerializableTestResults>,
}

#[derive(Debug, Serialize, Deserialize)]
struct SerializableTokens {
    input: u64,
    output: u64,
    cache_creation: u64,
    cache_read: u64,
}

#[derive(Debug, Serialize, Deserialize)]
struct SerializableTestResults {
    passed: u32,
    total: u32,
    pass_rate: f64,
}

/// Serializable multi-trial result for JSON output (EVAL-08).
#[derive(Debug, Serialize, Deserialize)]
struct SerializableMultiTrialResult {
    project: String,
    timestamp: String,
    trial_count: u32,
    trials: Vec<SerializableTrialSummary>,
    statistics: SerializableStatistics,
}

/// Serializable trial summary for JSON output.
#[derive(Debug, Serialize, Deserialize)]
struct SerializableTrialSummary {
    trial_num: u32,
    elapsed_secs: f64,
    iterations: u32,
    tokens: SerializableTokens,
    test_results: Option<SerializableTestResults>,
    workspace_path: String,
}

/// Serializable statistics for JSON output.
#[derive(Debug, Serialize, Deserialize)]
struct SerializableStatistics {
    pass_rate: SerializableStatSummary,
    elapsed_secs: SerializableStatSummary,
    total_input_tokens: SerializableStatSummary,
    total_output_tokens: SerializableStatSummary,
    iterations: SerializableStatSummary,
}

/// Serializable stat summary for JSON output.
#[derive(Debug, Serialize, Deserialize)]
struct SerializableStatSummary {
    mean: f64,
    variance: f64,
    std_dev: f64,
    min: f64,
    max: f64,
    count: usize,
}

/// Save result.json to workspace directory.
fn save_result_json(working_dir: &Path, result: &EvalResult) -> color_eyre::Result<()> {
    let serializable = SerializableResult {
        project: result.project.clone(),
        mode: result.mode,
        elapsed_secs: result.elapsed_secs,
        iterations: result.iterations,
        tokens: SerializableTokens {
            input: result.total_tokens.input_tokens,
            output: result.total_tokens.output_tokens,
            cache_creation: result.total_tokens.cache_creation_input_tokens,
            cache_read: result.total_tokens.cache_read_input_tokens,
        },
        test_results: result
            .test_results
            .as_ref()
            .map(|tr| SerializableTestResults {
                passed: tr.passed,
                total: tr.total,
                pass_rate: tr.pass_rate(),
            }),
    };

    let json = serde_json::to_string_pretty(&serializable)?;
    std::fs::write(working_dir.join("result.json"), json)?;
    Ok(())
}

/// Compute statistics from a slice of trial results.
///
/// Extracts metrics from each trial and computes summary statistics:
/// - Pass rate (0.0 to 1.0)
/// - Elapsed time in seconds
/// - Total input tokens
/// - Total output tokens
/// - Number of build iterations
fn compute_statistics(trials: &[EvalResult]) -> TrialStatistics {
    // Extract pass rates (only from trials with test results)
    let pass_rates: Vec<f64> = trials
        .iter()
        .filter_map(|t| t.test_results.as_ref())
        .map(|tr| tr.pass_rate() / 100.0) // Convert from percentage to 0.0-1.0
        .collect();

    // Extract elapsed time
    let elapsed_secs: Vec<f64> = trials.iter().map(|t| t.elapsed_secs).collect();

    // Extract token counts
    let input_tokens: Vec<f64> = trials
        .iter()
        .map(|t| t.total_tokens.input_tokens as f64)
        .collect();

    let output_tokens: Vec<f64> = trials
        .iter()
        .map(|t| t.total_tokens.output_tokens as f64)
        .collect();

    // Extract iteration counts
    let iterations: Vec<f64> = trials.iter().map(|t| t.iterations as f64).collect();

    TrialStatistics {
        pass_rate: StatSummary::from_values(&pass_rates),
        elapsed_secs: StatSummary::from_values(&elapsed_secs),
        total_input_tokens: StatSummary::from_values(&input_tokens),
        total_output_tokens: StatSummary::from_values(&output_tokens),
        iterations: StatSummary::from_values(&iterations),
    }
}

/// Print statistics summary to stdout.
fn print_statistics(stats: &TrialStatistics, trial_count: u32) {
    println!("\n=== STATISTICAL SUMMARY ({} trials) ===\n", trial_count);

    // Pass Rate (convert back to percentage for display)
    if stats.pass_rate.count > 0 {
        println!(
            "Pass Rate:      Mean: {:.1}%  Std Dev: {:.1}%  Min: {:.1}%  Max: {:.1}%",
            stats.pass_rate.mean * 100.0,
            stats.pass_rate.std_dev() * 100.0,
            stats.pass_rate.min * 100.0,
            stats.pass_rate.max * 100.0,
        );
    } else {
        println!("Pass Rate:      N/A (no test results)");
    }

    // Execution Time
    println!(
        "Execution Time: Mean: {:.1}s  Std Dev: {:.1}s  Min: {:.1}s  Max: {:.1}s",
        stats.elapsed_secs.mean,
        stats.elapsed_secs.std_dev(),
        stats.elapsed_secs.min,
        stats.elapsed_secs.max,
    );

    // Token Usage
    println!(
        "Input Tokens:   Mean: {}  Std Dev: {}  Min: {}  Max: {}",
        format_tokens(stats.total_input_tokens.mean as u64),
        format_tokens(stats.total_input_tokens.std_dev() as u64),
        format_tokens(stats.total_input_tokens.min as u64),
        format_tokens(stats.total_input_tokens.max as u64),
    );
    println!(
        "Output Tokens:  Mean: {}  Std Dev: {}  Min: {}  Max: {}",
        format_tokens(stats.total_output_tokens.mean as u64),
        format_tokens(stats.total_output_tokens.std_dev() as u64),
        format_tokens(stats.total_output_tokens.min as u64),
        format_tokens(stats.total_output_tokens.max as u64),
    );

    // Iterations
    println!(
        "Iterations:     Mean: {:.1}  Std Dev: {:.1}  Min: {}  Max: {}",
        stats.iterations.mean,
        stats.iterations.std_dev(),
        stats.iterations.min as u32,
        stats.iterations.max as u32,
    );
}

/// Save multi-trial results to a JSON file in eval_dir (EVAL-08).
///
/// Creates a timestamped JSON file containing all trial summaries
/// and aggregated statistics for later comparison and analysis.
///
/// # Arguments
///
/// * `eval_dir` - Directory to save the results file
/// * `project` - Project name
/// * `trials` - Trial results to save
/// * `statistics` - Computed statistics across trials
///
/// # Returns
///
/// * `Ok(PathBuf)` - Path to the saved JSON file
/// * `Err(e)` - File write failed
fn save_multi_trial_result(
    eval_dir: &Path,
    project: &str,
    trials: &[EvalResult],
    statistics: &TrialStatistics,
) -> color_eyre::Result<PathBuf> {
    // Generate filename: eval-results-{project}-{YYYY-MM-DD}.json
    let filename = format!(
        "eval-results-{}-{}.json",
        project,
        Utc::now().format("%Y-%m-%d")
    );
    let path = eval_dir.join(&filename);

    // Convert to serializable format
    let serializable = convert_to_serializable(project, trials, statistics);

    // Write pretty-printed JSON
    let json = serde_json::to_string_pretty(&serializable)?;
    std::fs::write(&path, json)?;

    Ok(path)
}

/// Convert multi-trial results to serializable format.
fn convert_to_serializable(
    project: &str,
    trials: &[EvalResult],
    statistics: &TrialStatistics,
) -> SerializableMultiTrialResult {
    SerializableMultiTrialResult {
        project: project.to_string(),
        timestamp: Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string(),
        trial_count: trials.len() as u32,
        trials: trials.iter().map(convert_trial_to_serializable).collect(),
        statistics: convert_statistics_to_serializable(statistics),
    }
}

/// Convert a single trial result to serializable format.
fn convert_trial_to_serializable(trial: &EvalResult) -> SerializableTrialSummary {
    SerializableTrialSummary {
        trial_num: trial.trial_num,
        elapsed_secs: trial.elapsed_secs,
        iterations: trial.iterations,
        tokens: SerializableTokens {
            input: trial.total_tokens.input_tokens,
            output: trial.total_tokens.output_tokens,
            cache_creation: trial.total_tokens.cache_creation_input_tokens,
            cache_read: trial.total_tokens.cache_read_input_tokens,
        },
        test_results: trial
            .test_results
            .as_ref()
            .map(|tr| SerializableTestResults {
                passed: tr.passed,
                total: tr.total,
                pass_rate: tr.pass_rate(),
            }),
        workspace_path: trial
            .workspace_path
            .as_ref()
            .map(|p| p.display().to_string())
            .unwrap_or_default(),
    }
}

/// Convert statistics to serializable format.
fn convert_statistics_to_serializable(stats: &TrialStatistics) -> SerializableStatistics {
    SerializableStatistics {
        pass_rate: convert_stat_summary(&stats.pass_rate),
        elapsed_secs: convert_stat_summary(&stats.elapsed_secs),
        total_input_tokens: convert_stat_summary(&stats.total_input_tokens),
        total_output_tokens: convert_stat_summary(&stats.total_output_tokens),
        iterations: convert_stat_summary(&stats.iterations),
    }
}

/// Convert a StatSummary to serializable format, including std_dev.
fn convert_stat_summary(stat: &StatSummary) -> SerializableStatSummary {
    SerializableStatSummary {
        mean: stat.mean,
        variance: stat.variance,
        std_dev: stat.std_dev(),
        min: stat.min,
        max: stat.max,
        count: stat.count,
    }
}

/// Load a multi-trial result from JSON file.
///
/// # Arguments
///
/// * `path` - Path to the JSON file
///
/// # Returns
///
/// * `Ok(SerializableMultiTrialResult)` - Loaded result
/// * `Err(e)` - File not found or invalid JSON
fn load_multi_trial_result(path: &Path) -> color_eyre::Result<SerializableMultiTrialResult> {
    let content = std::fs::read_to_string(path)
        .map_err(|e| eyre!("Failed to read {}: {}", path.display(), e))?;

    let result: SerializableMultiTrialResult = serde_json::from_str(&content)
        .map_err(|e| eyre!("Invalid JSON in {}: {}", path.display(), e))?;

    Ok(result)
}

/// Run the compare command to compare two eval result files (EVAL-09).
///
/// Loads two multi-trial result JSON files and displays deltas for key metrics:
/// - Pass rate (higher is better)
/// - Execution time (lower is better)
/// - Input tokens (lower is better)
/// - Output tokens (lower is better)
///
/// # Arguments
///
/// * `file1` - Path to baseline result file
/// * `file2` - Path to comparison result file
///
/// # Returns
///
/// * `Ok(())` - Comparison completed successfully
/// * `Err(e)` - File loading or parsing failed
pub fn run_compare_command(file1: PathBuf, file2: PathBuf) -> color_eyre::Result<()> {
    let result1 = load_multi_trial_result(&file1)?;
    let result2 = load_multi_trial_result(&file2)?;

    println!("Comparing results:");
    println!(
        "  Baseline:   {} ({} trials)",
        file1.display(),
        result1.trial_count
    );
    println!(
        "  Comparison: {} ({} trials)",
        file2.display(),
        result2.trial_count
    );
    println!();

    // Print deltas for each metric
    // Pass rate: higher is better
    print_delta(
        "Pass Rate",
        result1.statistics.pass_rate.mean * 100.0,
        result2.statistics.pass_rate.mean * 100.0,
        "%",
        true, // higher is better
    );

    // Execution time: lower is better
    print_delta(
        "Execution Time",
        result1.statistics.elapsed_secs.mean,
        result2.statistics.elapsed_secs.mean,
        "s",
        false, // lower is better
    );

    // Input tokens: lower is better
    print_delta(
        "Input Tokens",
        result1.statistics.total_input_tokens.mean,
        result2.statistics.total_input_tokens.mean,
        "",
        false, // lower is better
    );

    // Output tokens: lower is better
    print_delta(
        "Output Tokens",
        result1.statistics.total_output_tokens.mean,
        result2.statistics.total_output_tokens.mean,
        "",
        false, // lower is better
    );

    Ok(())
}

/// Print a delta comparison between two values.
///
/// Shows: `{name}: {baseline}{unit} -> {comparison}{unit} ({arrow}{delta}{unit}, {percent}%)`
///
/// # Arguments
///
/// * `name` - Metric name
/// * `baseline` - Baseline value
/// * `comparison` - Comparison value
/// * `unit` - Unit suffix (e.g., "%", "s", "")
/// * `higher_is_better` - If true, positive delta shows ^, else shows v
fn print_delta(name: &str, baseline: f64, comparison: f64, unit: &str, higher_is_better: bool) {
    let delta = comparison - baseline;
    let percent = if baseline.abs() > 0.0001 {
        (delta / baseline) * 100.0
    } else {
        0.0
    };

    // Determine arrow based on whether this is an improvement
    // For "higher is better" metrics: positive delta = improvement (^)
    // For "lower is better" metrics: negative delta = improvement (^)
    let is_improvement = if higher_is_better {
        delta > 0.0
    } else {
        delta < 0.0
    };
    let arrow = if is_improvement { "^" } else { "v" };

    // Format the sign for display
    let sign = if delta >= 0.0 { "+" } else { "" };

    println!(
        "{}: {:.1}{} -> {:.1}{} ({}{:.1}{}, {}{}%)",
        name,
        baseline,
        unit,
        comparison,
        unit,
        arrow,
        delta.abs(),
        unit,
        sign,
        percent as i64
    );
}

/// Copy directory contents recursively.
fn copy_dir_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
    if !dst.exists() {
        std::fs::create_dir_all(dst)?;
    }

    for entry in std::fs::read_dir(src)? {
        let entry = entry?;
        let src_path = entry.path();
        let dst_path = dst.join(entry.file_name());

        if src_path.is_dir() {
            // Skip .git directories
            if entry.file_name() == ".git" {
                continue;
            }
            copy_dir_recursive(&src_path, &dst_path)?;
        } else {
            std::fs::copy(&src_path, &dst_path)?;
        }
    }

    Ok(())
}

/// Initialize a git repository in the workspace.
fn init_git_repo(working_dir: &PathBuf) -> std::io::Result<()> {
    use std::process::Command;

    Command::new("git")
        .args(["init"])
        .current_dir(working_dir)
        .output()?;

    Command::new("git")
        .args(["config", "user.email", "eval@rslph.local"])
        .current_dir(working_dir)
        .output()?;

    Command::new("git")
        .args(["config", "user.name", "Eval"])
        .current_dir(working_dir)
        .output()?;

    // Initial commit so we have a clean baseline
    Command::new("git")
        .args(["add", "."])
        .current_dir(working_dir)
        .output()?;

    Command::new("git")
        .args(["commit", "-m", "Initial eval state", "--allow-empty"])
        .current_dir(working_dir)
        .output()?;

    Ok(())
}

/// Detect the eval prompt from the project directory.
///
/// Looks for prompt.txt or README.md in the project root.
fn detect_eval_prompt(working_dir: &Path) -> color_eyre::Result<String> {
    // Priority 1: prompt.txt
    let prompt_file = working_dir.join("prompt.txt");
    if prompt_file.exists() {
        return Ok(std::fs::read_to_string(prompt_file)?);
    }

    // Priority 2: README.md
    let readme_file = working_dir.join("README.md");
    if readme_file.exists() {
        return Ok(std::fs::read_to_string(readme_file)?);
    }

    // Priority 3: PROMPT.md
    let prompt_md = working_dir.join("PROMPT.md");
    if prompt_md.exists() {
        return Ok(std::fs::read_to_string(prompt_md)?);
    }

    Err(color_eyre::eyre::eyre!(
        "No prompt file found. Expected prompt.txt, README.md, or PROMPT.md in project root"
    ))
}

/// Run hidden tests for a built-in project.
///
/// Loads test cases from the embedded project and runs them against
/// the built program, displaying results. Uses Claude to discover
/// how to run the program, falling back to hardcoded detection.
async fn run_project_tests(
    project: &str,
    working_dir: &Path,
    config: &Config,
    cancel_token: CancellationToken,
) -> Option<TestResults> {
    println!("\n=== TEST PHASE ===\n");

    // Get test data from embedded project
    let proj = crate::eval::get_project(project)?;
    let test_content = crate::eval::get_test_data(proj)?;
    let test_cases = load_test_cases(test_content);

    if test_cases.is_empty() {
        println!("Warning: No test cases found in project");
        return None;
    }

    // Try to discover run script using Claude
    let run_script = match discover_run_script(&config.claude_path, working_dir, cancel_token).await
    {
        Ok(script_path) => Some(script_path),
        Err(e) => {
            println!("Discovery failed ({}), trying fallback detection...", e);
            None
        }
    };

    // If discovery succeeded, use script-based runner
    if let Some(script_path) = run_script {
        println!("Testing with script: {}", script_path.display());
        let runner = TestRunner::from_script(script_path, working_dir.to_path_buf());
        let results = runner.run_tests(&test_cases);

        print_test_results(&results);
        return Some(results);
    }

    // Fallback: Find the built program using hardcoded patterns
    let program_path = match find_built_program(working_dir) {
        Some(path) => path,
        None => {
            println!("Warning: Could not find built program to test");
            return None;
        }
    };

    println!("Testing program: {}", program_path.display());

    // Run tests with direct program execution
    let runner = TestRunner::new(program_path);
    let results = runner.run_tests(&test_cases);

    print_test_results(&results);
    Some(results)
}

/// Print test results summary.
fn print_test_results(results: &TestResults) {
    println!(
        "Tests: {}/{} passed ({:.1}%)",
        results.passed,
        results.total,
        results.pass_rate()
    );

    // Print failed tests for debugging
    for case in &results.cases {
        if !case.passed {
            println!(
                "  FAIL: input='{}' expected='{}' got='{}'",
                case.input, case.expected, case.actual
            );
        }
    }
}

/// Attempt to find a runnable program in the workspace.
///
/// Looks for common patterns: Rust target, Python script, shell script.
fn find_built_program(working_dir: &Path) -> Option<PathBuf> {
    // Check for Rust binary in target/debug or target/release
    let cargo_toml = working_dir.join("Cargo.toml");
    if cargo_toml.exists() {
        // Parse Cargo.toml to find package name
        if let Ok(content) = std::fs::read_to_string(&cargo_toml) {
            for line in content.lines() {
                if line.trim().starts_with("name = ") {
                    let name = line.split('"').nth(1)?;
                    let debug_path = working_dir.join("target/debug").join(name);
                    let release_path = working_dir.join("target/release").join(name);
                    if debug_path.exists() {
                        return Some(debug_path);
                    }
                    if release_path.exists() {
                        return Some(release_path);
                    }
                }
            }
        }
    }

    // Check for executable scripts
    for script_name in &["main.py", "main.sh", "calculator", "calc"] {
        let script_path = working_dir.join(script_name);
        if script_path.exists() {
            return Some(script_path);
        }
    }

    None
}

/// Discover how to run the program using Claude.
///
/// Uses Claude to analyze the workspace and generate a shell script
/// that can run the built program. This is language-agnostic and works
/// for any project structure.
///
/// # Arguments
///
/// * `claude_path` - Path to Claude CLI
/// * `working_dir` - Workspace directory containing the built project
/// * `cancel_token` - Token for graceful cancellation
///
/// # Returns
///
/// * `Ok(PathBuf)` - Path to the generated run script
/// * `Err(e)` - Discovery failed
async fn discover_run_script(
    claude_path: &str,
    working_dir: &Path,
    cancel_token: CancellationToken,
) -> color_eyre::Result<PathBuf> {
    println!("Discovering how to run the program...");

    // Build workspace context for Claude
    let context = build_workspace_context(working_dir)?;

    // Prepare Claude args
    let system_prompt = test_discovery_prompt();
    let args = vec![
        "-p".to_string(),
        "--verbose".to_string(),
        "--output-format".to_string(),
        "stream-json".to_string(),
        "--system-prompt".to_string(),
        system_prompt.to_string(),
        context,
    ];

    // Spawn Claude
    let mut runner = ClaudeRunner::spawn(claude_path, &args, working_dir)
        .await
        .map_err(|e| eyre!("Failed to spawn claude for test discovery: {}", e))?;

    // Run with 60 second timeout (discovery should be quick)
    let timeout = Duration::from_secs(60);
    let output = runner
        .run_with_timeout(timeout, cancel_token)
        .await
        .map_err(|e| eyre!("Claude test discovery failed: {}", e))?;

    // Parse response
    let mut stream_response = StreamResponse::new();
    for line in &output {
        if let OutputLine::Stdout(s) = line {
            stream_response.process_line(s);
        }
    }

    let script_content = extract_script(&stream_response.text)?;

    // Write script to workspace
    let script_path = working_dir.join("_run_tests.sh");
    std::fs::write(&script_path, &script_content)?;

    // Make executable
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = std::fs::metadata(&script_path)?.permissions();
        perms.set_mode(0o755);
        std::fs::set_permissions(&script_path, perms)?;
    }

    println!("Generated run script: {}", script_path.display());
    Ok(script_path)
}

/// Build workspace context string for Claude to analyze.
fn build_workspace_context(working_dir: &Path) -> color_eyre::Result<String> {
    let mut context = String::new();

    // Add file listing
    context.push_str("## Project Files\n\n```\n");
    if let Ok(entries) = std::fs::read_dir(working_dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            let name = path.file_name().unwrap_or_default().to_string_lossy();
            // Skip hidden files except config files
            if name.starts_with('.') && !name.starts_with(".python") {
                continue;
            }
            if path.is_dir() {
                context.push_str(&format!("{}/\n", name));
                // List first-level contents of directories
                if let Ok(sub_entries) = std::fs::read_dir(&path) {
                    for sub in sub_entries.flatten().take(10) {
                        let sub_name = sub.file_name().to_string_lossy().to_string();
                        if !sub_name.starts_with('.') {
                            context.push_str(&format!("  {}\n", sub_name));
                        }
                    }
                }
            } else {
                context.push_str(&format!("{}\n", name));
            }
        }
    }
    context.push_str("```\n\n");

    // Add key configuration files
    let config_files = [
        "Cargo.toml",
        "pyproject.toml",
        "setup.py",
        "package.json",
        "go.mod",
        "Makefile",
        "build.zig",
        "CMakeLists.txt",
    ];

    for config_file in config_files {
        let path = working_dir.join(config_file);
        if path.exists() {
            if let Ok(content) = std::fs::read_to_string(&path) {
                context.push_str(&format!("## {}\n\n```\n{}\n```\n\n", config_file, content));
            }
        }
    }

    // Look for main entry point files
    let entry_files = [
        "main.py", "main.rs", "main.go", "index.js", "index.ts", "main.sh",
    ];

    for entry_file in entry_files {
        let path = working_dir.join(entry_file);
        if path.exists() {
            if let Ok(content) = std::fs::read_to_string(&path) {
                // Only include first 50 lines
                let truncated: String = content.lines().take(50).collect::<Vec<_>>().join("\n");
                context.push_str(&format!(
                    "## {} (first 50 lines)\n\n```\n{}\n```\n\n",
                    entry_file, truncated
                ));
            }
        }
    }

    // Look for Python files with __main__ in subdirectories
    if let Ok(entries) = std::fs::read_dir(working_dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                let name = path.file_name().unwrap_or_default().to_string_lossy();
                if name.starts_with('.') || name == "__pycache__" || name == "tests" {
                    continue;
                }
                // Check for Python files in subdirectory
                if let Ok(sub_entries) = std::fs::read_dir(&path) {
                    for sub in sub_entries.flatten() {
                        let sub_path = sub.path();
                        if sub_path.extension().is_some_and(|e| e == "py") {
                            if let Ok(content) = std::fs::read_to_string(&sub_path) {
                                if content.contains("if __name__") || content.contains("def main") {
                                    let truncated: String =
                                        content.lines().take(50).collect::<Vec<_>>().join("\n");
                                    context.push_str(&format!(
                                        "## {}/{} (first 50 lines - has main)\n\n```python\n{}\n```\n\n",
                                        name,
                                        sub_path.file_name().unwrap_or_default().to_string_lossy(),
                                        truncated
                                    ));
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    Ok(context)
}

/// Extract shell script from Claude's response.
fn extract_script(response: &str) -> color_eyre::Result<String> {
    let text = response.trim();

    // If response starts with shebang, use it directly
    if text.starts_with("#!/") {
        return Ok(text.to_string());
    }

    // Try to extract from code fence
    if let Some(start) = text.find("```") {
        let after_fence = &text[start + 3..];
        // Skip language identifier (bash, sh, etc.)
        let content_start = after_fence.find('\n').unwrap_or(0) + 1;
        let content = &after_fence[content_start..];
        if let Some(end) = content.find("```") {
            let script = content[..end].trim();
            if script.starts_with("#!/") {
                return Ok(script.to_string());
            }
            // Add shebang if missing
            return Ok(format!("#!/bin/sh\n{}", script));
        }
    }

    // Fallback: assume the whole response is the script
    if !text.is_empty() {
        if text.starts_with("#!/") {
            return Ok(text.to_string());
        }
        return Ok(format!("#!/bin/sh\n{}", text));
    }

    Err(eyre!("Could not extract script from Claude's response"))
}

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

    #[test]
    fn test_copy_dir_recursive() {
        let src_dir = TempDir::new().expect("src temp dir");
        let dst_dir = TempDir::new().expect("dst temp dir");

        // Create source structure
        std::fs::write(src_dir.path().join("file.txt"), "content").expect("write file");
        std::fs::create_dir(src_dir.path().join("subdir")).expect("create subdir");
        std::fs::write(src_dir.path().join("subdir/nested.txt"), "nested").expect("write nested");

        // Create .git directory that should be skipped
        std::fs::create_dir(src_dir.path().join(".git")).expect("create .git");
        std::fs::write(src_dir.path().join(".git/config"), "git stuff").expect("write git config");

        // Copy
        copy_dir_recursive(src_dir.path(), dst_dir.path()).expect("copy");

        // Verify
        assert!(dst_dir.path().join("file.txt").exists());
        assert!(dst_dir.path().join("subdir/nested.txt").exists());
        assert!(
            !dst_dir.path().join(".git").exists(),
            ".git should be skipped"
        );
    }

    #[test]
    fn test_detect_eval_prompt_priority() {
        let dir = TempDir::new().expect("temp dir");

        // No prompt file
        let result = detect_eval_prompt(dir.path());
        assert!(result.is_err());

        // Add README.md
        std::fs::write(dir.path().join("README.md"), "readme content").expect("write readme");
        let result = detect_eval_prompt(dir.path());
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "readme content");

        // Add prompt.txt (should take priority)
        std::fs::write(dir.path().join("prompt.txt"), "prompt content").expect("write prompt");
        let result = detect_eval_prompt(dir.path());
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "prompt content");
    }

    #[test]
    fn test_init_git_repo() {
        let dir = TempDir::new().expect("temp dir");
        let path = dir.path().to_path_buf();

        init_git_repo(&path).expect("init git");

        assert!(path.join(".git").exists(), ".git directory should exist");
    }

    #[test]
    fn test_detect_eval_prompt_with_prompt_md() {
        let dir = TempDir::new().expect("temp dir");

        // Add PROMPT.md (priority 3)
        std::fs::write(dir.path().join("PROMPT.md"), "prompt md content").expect("write prompt md");
        let result = detect_eval_prompt(dir.path());
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "prompt md content");

        // Add README.md (should take priority over PROMPT.md)
        std::fs::write(dir.path().join("README.md"), "readme content").expect("write readme");
        let result = detect_eval_prompt(dir.path());
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "readme content");
    }

    #[test]
    fn test_copy_dir_recursive_empty_src() {
        let src_dir = TempDir::new().expect("src temp dir");
        let dst_dir = TempDir::new().expect("dst temp dir");

        // Copy empty directory
        copy_dir_recursive(src_dir.path(), dst_dir.path()).expect("copy");

        // Verify destination exists and is empty
        assert!(dst_dir.path().exists());
    }

    #[test]
    fn test_find_built_program_cargo_project() {
        let dir = TempDir::new().expect("temp dir");

        // Create Cargo.toml
        std::fs::write(
            dir.path().join("Cargo.toml"),
            r#"[package]
name = "myapp"
version = "0.1.0"
"#,
        )
        .expect("write Cargo.toml");

        // Create fake binary
        std::fs::create_dir_all(dir.path().join("target/debug")).expect("create target/debug");
        std::fs::write(dir.path().join("target/debug/myapp"), "binary").expect("write binary");

        let result = find_built_program(dir.path());
        assert!(result.is_some(), "Should find Cargo binary");
        assert!(
            result.unwrap().ends_with("myapp"),
            "Path should end with binary name"
        );
    }

    #[test]
    fn test_find_built_program_release_build() {
        let dir = TempDir::new().expect("temp dir");

        // Create Cargo.toml
        std::fs::write(
            dir.path().join("Cargo.toml"),
            r#"[package]
name = "myrelease"
version = "0.1.0"
"#,
        )
        .expect("write Cargo.toml");

        // Create only release binary (no debug)
        std::fs::create_dir_all(dir.path().join("target/release")).expect("create target/release");
        std::fs::write(dir.path().join("target/release/myrelease"), "binary")
            .expect("write binary");

        let result = find_built_program(dir.path());
        assert!(result.is_some(), "Should find release binary");
        assert!(
            result.unwrap().to_str().unwrap().contains("release"),
            "Path should contain 'release'"
        );
    }

    #[test]
    fn test_find_built_program_script() {
        let dir = TempDir::new().expect("temp dir");

        // Create main.py
        std::fs::write(dir.path().join("main.py"), "print('hello')").expect("write main.py");

        let result = find_built_program(dir.path());
        assert!(result.is_some(), "Should find Python script");
        assert!(
            result.unwrap().ends_with("main.py"),
            "Path should end with main.py"
        );
    }

    #[test]
    fn test_find_built_program_shell_script() {
        let dir = TempDir::new().expect("temp dir");

        // Create main.sh
        std::fs::write(dir.path().join("main.sh"), "#!/bin/bash\necho hello")
            .expect("write main.sh");

        let result = find_built_program(dir.path());
        assert!(result.is_some(), "Should find shell script");
        assert!(
            result.unwrap().ends_with("main.sh"),
            "Path should end with main.sh"
        );
    }

    #[test]
    fn test_find_built_program_calculator_name() {
        let dir = TempDir::new().expect("temp dir");

        // Create "calculator" executable
        std::fs::write(dir.path().join("calculator"), "#!/bin/bash").expect("write calculator");

        let result = find_built_program(dir.path());
        assert!(result.is_some(), "Should find calculator");
        assert!(
            result.unwrap().ends_with("calculator"),
            "Path should end with calculator"
        );
    }

    #[test]
    fn test_find_built_program_no_match() {
        let dir = TempDir::new().expect("temp dir");

        // Create a random file that doesn't match any pattern
        std::fs::write(dir.path().join("random.txt"), "content").expect("write");

        let result = find_built_program(dir.path());
        assert!(result.is_none(), "Should not find any program");
    }

    #[test]
    fn test_find_built_program_cargo_debug_over_release() {
        let dir = TempDir::new().expect("temp dir");

        // Create Cargo.toml
        std::fs::write(
            dir.path().join("Cargo.toml"),
            r#"[package]
name = "myapp"
version = "0.1.0"
"#,
        )
        .expect("write Cargo.toml");

        // Create both debug and release binaries
        std::fs::create_dir_all(dir.path().join("target/debug")).expect("create target/debug");
        std::fs::create_dir_all(dir.path().join("target/release")).expect("create target/release");
        std::fs::write(dir.path().join("target/debug/myapp"), "debug").expect("write debug");
        std::fs::write(dir.path().join("target/release/myapp"), "release").expect("write release");

        let result = find_built_program(dir.path());
        assert!(result.is_some(), "Should find binary");
        // Debug should be preferred over release
        assert!(
            result.unwrap().to_str().unwrap().contains("debug"),
            "Debug build should be preferred"
        );
    }

    #[test]
    fn test_builtin_project_detection() {
        // Test that calculator is detected as built-in
        assert!(crate::eval::is_builtin("calculator"));
        assert!(!crate::eval::is_builtin("nonexistent"));
        assert!(!crate::eval::is_builtin("/some/path"));
    }

    #[test]
    fn test_save_result_json() {
        use crate::build::tokens::TokenUsage;
        use crate::eval::TestResults;

        let dir = TempDir::new().expect("temp dir");
        let result = EvalResult {
            project: "test-project".to_string(),
            mode: PromptMode::Basic,
            trial_num: 1,
            elapsed_secs: 123.45,
            total_tokens: TokenUsage {
                input_tokens: 1000,
                output_tokens: 500,
                cache_creation_input_tokens: 100,
                cache_read_input_tokens: 50,
            },
            iterations: 5,
            workspace_path: Some(dir.path().to_path_buf()),
            test_results: Some(TestResults {
                passed: 3,
                total: 5,
                cases: vec![],
            }),
        };

        save_result_json(dir.path(), &result).expect("save result");

        // Verify file was created
        let result_path = dir.path().join("result.json");
        assert!(result_path.exists(), "result.json should exist");

        // Verify JSON content
        let content = std::fs::read_to_string(&result_path).expect("read result.json");
        let json: serde_json::Value = serde_json::from_str(&content).expect("parse json");

        assert_eq!(json["project"], "test-project");
        assert_eq!(json["elapsed_secs"], 123.45);
        assert_eq!(json["iterations"], 5);
        assert_eq!(json["tokens"]["input"], 1000);
        assert_eq!(json["tokens"]["output"], 500);
        assert_eq!(json["test_results"]["passed"], 3);
        assert_eq!(json["test_results"]["total"], 5);
        assert_eq!(json["test_results"]["pass_rate"], 60.0);
    }

    #[test]
    fn test_save_result_json_without_tests() {
        use crate::build::tokens::TokenUsage;

        let dir = TempDir::new().expect("temp dir");
        let result = EvalResult {
            project: "external-project".to_string(),
            mode: PromptMode::Basic,
            trial_num: 1,
            elapsed_secs: 50.0,
            total_tokens: TokenUsage {
                input_tokens: 200,
                output_tokens: 100,
                cache_creation_input_tokens: 0,
                cache_read_input_tokens: 0,
            },
            iterations: 3,
            workspace_path: Some(dir.path().to_path_buf()),
            test_results: None,
        };

        save_result_json(dir.path(), &result).expect("save result");

        let content = std::fs::read_to_string(dir.path().join("result.json")).expect("read");
        let json: serde_json::Value = serde_json::from_str(&content).expect("parse");

        assert_eq!(json["project"], "external-project");
        assert!(json["test_results"].is_null());
    }

    #[test]
    fn test_load_result_json() {
        let dir = TempDir::new().expect("temp dir");
        let result_path = dir.path().join("result.json");

        // Write a sample result.json
        let json = r#"{
            "project": "calculator",
            "elapsed_secs": 123.45,
            "iterations": 5,
            "tokens": {
                "input": 1000,
                "output": 500,
                "cache_creation": 100,
                "cache_read": 50
            },
            "test_results": {
                "passed": 8,
                "total": 10,
                "pass_rate": 80.0
            }
        }"#;
        std::fs::write(&result_path, json).expect("write");

        let loaded = load_result_json(&result_path).expect("load");
        assert_eq!(loaded.project, "calculator");
        assert_eq!(loaded.elapsed_secs, 123.45);
        assert_eq!(loaded.iterations, 5);
        assert_eq!(loaded.tokens.input, 1000);
        assert_eq!(loaded.tokens.output, 500);
    }

    #[test]
    fn test_load_result_json_missing_file() {
        let dir = TempDir::new().expect("temp dir");
        let result_path = dir.path().join("nonexistent.json");

        let result = load_result_json(&result_path);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_retest_missing_workspace() {
        let config = crate::config::Config::default();
        let cancel_token = tokio_util::sync::CancellationToken::new();

        let result = run_retest_command(
            std::path::PathBuf::from("/nonexistent/workspace"),
            &config,
            cancel_token,
        )
        .await;

        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("does not exist"), "Error: {}", err);
    }

    #[tokio::test]
    async fn test_retest_missing_result_json() {
        let dir = TempDir::new().expect("temp dir");
        let config = crate::config::Config::default();
        let cancel_token = tokio_util::sync::CancellationToken::new();

        let result = run_retest_command(dir.path().to_path_buf(), &config, cancel_token).await;

        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("result.json"), "Error: {}", err);
    }

    #[tokio::test]
    async fn test_retest_non_builtin_project() {
        let dir = TempDir::new().expect("temp dir");

        // Write result.json with a non-builtin project
        let json = r#"{
            "project": "my-custom-project",
            "elapsed_secs": 10.0,
            "iterations": 1,
            "tokens": {
                "input": 100,
                "output": 50,
                "cache_creation": 0,
                "cache_read": 0
            },
            "test_results": null
        }"#;
        std::fs::write(dir.path().join("result.json"), json).expect("write");

        let config = crate::config::Config::default();
        let cancel_token = tokio_util::sync::CancellationToken::new();

        let result = run_retest_command(dir.path().to_path_buf(), &config, cancel_token).await;

        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("not a built-in project"),
            "Error should mention not built-in: {}",
            err
        );
    }

    #[test]
    fn test_compute_statistics_with_multiple_trials() {
        use crate::build::tokens::TokenUsage;
        use crate::eval::TestResults;

        let trials = vec![
            EvalResult {
                project: "test".to_string(),
                mode: PromptMode::Basic,
                trial_num: 1,
                elapsed_secs: 10.0,
                total_tokens: TokenUsage {
                    input_tokens: 1000,
                    output_tokens: 500,
                    cache_creation_input_tokens: 0,
                    cache_read_input_tokens: 0,
                },
                iterations: 3,
                workspace_path: None,
                test_results: Some(TestResults {
                    passed: 8,
                    total: 10,
                    cases: vec![],
                }),
            },
            EvalResult {
                project: "test".to_string(),
                mode: PromptMode::Basic,
                trial_num: 2,
                elapsed_secs: 15.0,
                total_tokens: TokenUsage {
                    input_tokens: 1200,
                    output_tokens: 600,
                    cache_creation_input_tokens: 0,
                    cache_read_input_tokens: 0,
                },
                iterations: 4,
                workspace_path: None,
                test_results: Some(TestResults {
                    passed: 10,
                    total: 10,
                    cases: vec![],
                }),
            },
            EvalResult {
                project: "test".to_string(),
                mode: PromptMode::Basic,
                trial_num: 3,
                elapsed_secs: 12.5,
                total_tokens: TokenUsage {
                    input_tokens: 800,
                    output_tokens: 400,
                    cache_creation_input_tokens: 0,
                    cache_read_input_tokens: 0,
                },
                iterations: 2,
                workspace_path: None,
                test_results: Some(TestResults {
                    passed: 9,
                    total: 10,
                    cases: vec![],
                }),
            },
        ];

        let stats = compute_statistics(&trials);

        // Verify pass rate (80%, 100%, 90%) -> mean = 90% = 0.9
        assert_eq!(stats.pass_rate.count, 3);
        assert!((stats.pass_rate.mean - 0.9).abs() < 0.001);
        assert!((stats.pass_rate.min - 0.8).abs() < 0.001);
        assert!((stats.pass_rate.max - 1.0).abs() < 0.001);

        // Verify elapsed time (10, 15, 12.5) -> mean = 12.5
        assert_eq!(stats.elapsed_secs.count, 3);
        assert!((stats.elapsed_secs.mean - 12.5).abs() < 0.001);
        assert!((stats.elapsed_secs.min - 10.0).abs() < 0.001);
        assert!((stats.elapsed_secs.max - 15.0).abs() < 0.001);

        // Verify iterations (3, 4, 2) -> mean = 3.0
        assert_eq!(stats.iterations.count, 3);
        assert!((stats.iterations.mean - 3.0).abs() < 0.001);
        assert!((stats.iterations.min - 2.0).abs() < 0.001);
        assert!((stats.iterations.max - 4.0).abs() < 0.001);
    }

    #[test]
    fn test_compute_statistics_empty_trials() {
        let trials: Vec<EvalResult> = vec![];
        let stats = compute_statistics(&trials);

        assert_eq!(stats.pass_rate.count, 0);
        assert_eq!(stats.elapsed_secs.count, 0);
        assert_eq!(stats.iterations.count, 0);
    }

    #[test]
    fn test_compute_statistics_no_test_results() {
        use crate::build::tokens::TokenUsage;

        // Trials without test results (external projects)
        let trials = vec![EvalResult {
            project: "external".to_string(),
            mode: PromptMode::Basic,
            trial_num: 1,
            elapsed_secs: 10.0,
            total_tokens: TokenUsage {
                input_tokens: 1000,
                output_tokens: 500,
                cache_creation_input_tokens: 0,
                cache_read_input_tokens: 0,
            },
            iterations: 3,
            workspace_path: None,
            test_results: None, // No test results
        }];

        let stats = compute_statistics(&trials);

        // Pass rate should have count 0 since no test results
        assert_eq!(stats.pass_rate.count, 0);
        // Other stats should still work
        assert_eq!(stats.elapsed_secs.count, 1);
        assert!((stats.elapsed_secs.mean - 10.0).abs() < 0.001);
    }

    #[test]
    fn test_save_multi_trial_result() {
        use crate::build::tokens::TokenUsage;
        use crate::eval::TestResults;

        let dir = TempDir::new().expect("temp dir");

        // Create mock trials
        let trials = vec![
            EvalResult {
                project: "calculator".to_string(),
                mode: PromptMode::Basic,
                trial_num: 1,
                elapsed_secs: 10.0,
                total_tokens: TokenUsage {
                    input_tokens: 1000,
                    output_tokens: 500,
                    cache_creation_input_tokens: 100,
                    cache_read_input_tokens: 50,
                },
                iterations: 3,
                workspace_path: Some(PathBuf::from("/tmp/workspace1")),
                test_results: Some(TestResults {
                    passed: 8,
                    total: 10,
                    cases: vec![],
                }),
            },
            EvalResult {
                project: "calculator".to_string(),
                mode: PromptMode::Basic,
                trial_num: 2,
                elapsed_secs: 12.0,
                total_tokens: TokenUsage {
                    input_tokens: 1200,
                    output_tokens: 600,
                    cache_creation_input_tokens: 120,
                    cache_read_input_tokens: 60,
                },
                iterations: 4,
                workspace_path: Some(PathBuf::from("/tmp/workspace2")),
                test_results: Some(TestResults {
                    passed: 10,
                    total: 10,
                    cases: vec![],
                }),
            },
        ];

        // Compute statistics
        let statistics = compute_statistics(&trials);

        // Save to temp directory
        let result_path = save_multi_trial_result(dir.path(), "calculator", &trials, &statistics)
            .expect("save multi-trial result");

        // Verify file exists
        assert!(result_path.exists(), "JSON file should exist");
        assert!(
            result_path
                .file_name()
                .unwrap()
                .to_str()
                .unwrap()
                .starts_with("eval-results-calculator-"),
            "Filename should match pattern"
        );
        assert!(
            result_path.extension().unwrap() == "json",
            "File should have .json extension"
        );

        // Verify JSON content
        let content = std::fs::read_to_string(&result_path).expect("read json");
        let json: serde_json::Value = serde_json::from_str(&content).expect("parse json");

        // Check top-level fields
        assert_eq!(json["project"], "calculator");
        assert_eq!(json["trial_count"], 2);
        assert!(json["timestamp"].as_str().is_some());

        // Check trials array
        let trials_arr = json["trials"].as_array().expect("trials array");
        assert_eq!(trials_arr.len(), 2);
        assert_eq!(trials_arr[0]["trial_num"], 1);
        assert_eq!(trials_arr[0]["elapsed_secs"], 10.0);
        assert_eq!(trials_arr[0]["iterations"], 3);
        assert_eq!(trials_arr[0]["tokens"]["input"], 1000);
        assert_eq!(trials_arr[0]["test_results"]["passed"], 8);
        assert_eq!(trials_arr[1]["trial_num"], 2);

        // Check statistics
        let stats = &json["statistics"];
        assert!(stats["pass_rate"]["mean"].as_f64().is_some());
        assert!(stats["pass_rate"]["std_dev"].as_f64().is_some());
        assert!(stats["elapsed_secs"]["mean"].as_f64().is_some());
        assert!(stats["total_input_tokens"]["mean"].as_f64().is_some());
        assert!(stats["total_output_tokens"]["mean"].as_f64().is_some());
        assert!(stats["iterations"]["mean"].as_f64().is_some());
    }

    #[test]
    fn test_load_multi_trial_result() {
        let dir = TempDir::new().expect("temp dir");
        let result_path = dir.path().join("eval-results.json");

        // Write valid multi-trial JSON
        let json = r#"{
            "project": "calculator",
            "timestamp": "2026-01-22T01:00:00Z",
            "trial_count": 2,
            "trials": [
                {
                    "trial_num": 1,
                    "elapsed_secs": 10.0,
                    "iterations": 3,
                    "tokens": { "input": 1000, "output": 500, "cache_creation": 100, "cache_read": 50 },
                    "test_results": { "passed": 8, "total": 10, "pass_rate": 80.0 },
                    "workspace_path": "/tmp/workspace1"
                },
                {
                    "trial_num": 2,
                    "elapsed_secs": 12.0,
                    "iterations": 4,
                    "tokens": { "input": 1200, "output": 600, "cache_creation": 120, "cache_read": 60 },
                    "test_results": { "passed": 10, "total": 10, "pass_rate": 100.0 },
                    "workspace_path": "/tmp/workspace2"
                }
            ],
            "statistics": {
                "pass_rate": { "mean": 0.9, "variance": 0.01, "std_dev": 0.1, "min": 0.8, "max": 1.0, "count": 2 },
                "elapsed_secs": { "mean": 11.0, "variance": 2.0, "std_dev": 1.414, "min": 10.0, "max": 12.0, "count": 2 },
                "total_input_tokens": { "mean": 1100.0, "variance": 20000.0, "std_dev": 141.4, "min": 1000.0, "max": 1200.0, "count": 2 },
                "total_output_tokens": { "mean": 550.0, "variance": 5000.0, "std_dev": 70.7, "min": 500.0, "max": 600.0, "count": 2 },
                "iterations": { "mean": 3.5, "variance": 0.5, "std_dev": 0.707, "min": 3.0, "max": 4.0, "count": 2 }
            }
        }"#;
        std::fs::write(&result_path, json).expect("write");

        let loaded = load_multi_trial_result(&result_path).expect("load");
        assert_eq!(loaded.project, "calculator");
        assert_eq!(loaded.trial_count, 2);
        assert_eq!(loaded.trials.len(), 2);
        assert_eq!(loaded.trials[0].trial_num, 1);
        assert_eq!(loaded.trials[1].trial_num, 2);
        assert!((loaded.statistics.pass_rate.mean - 0.9).abs() < 0.001);
        assert!((loaded.statistics.elapsed_secs.mean - 11.0).abs() < 0.001);
    }

    #[test]
    fn test_load_multi_trial_result_missing_file() {
        let dir = TempDir::new().expect("temp dir");
        let result_path = dir.path().join("nonexistent.json");

        let result = load_multi_trial_result(&result_path);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Failed to read"), "Error: {}", err);
        assert!(err.contains("nonexistent.json"), "Error: {}", err);
    }

    #[test]
    fn test_load_multi_trial_result_invalid_json() {
        let dir = TempDir::new().expect("temp dir");
        let result_path = dir.path().join("invalid.json");

        // Write invalid JSON
        std::fs::write(&result_path, "{ not valid json }").expect("write");

        let result = load_multi_trial_result(&result_path);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Invalid JSON"), "Error: {}", err);
        assert!(err.contains("invalid.json"), "Error: {}", err);
    }
}