prodigy 0.4.4

Turn ad-hoc Claude sessions into reproducible development pipelines with parallel AI agents
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
//! Tests for MapReduce executor

use crate::cook::execution::interpolation::{InterpolationContext, InterpolationEngine};
use crate::cook::execution::mapreduce::*;
use crate::cook::orchestrator::ExecutionEnvironment;
use crate::cook::workflow::{CaptureOutput, CommandType};
use serde_json::json;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;

#[test]
fn test_agent_status_serialization() {
    let statuses = vec![
        AgentStatus::Pending,
        AgentStatus::Running,
        AgentStatus::Success,
        AgentStatus::Failed("error message".to_string()),
        AgentStatus::Timeout,
        AgentStatus::Retrying(2),
    ];

    for status in statuses {
        let json = serde_json::to_string(&status).unwrap();
        let deserialized: AgentStatus = serde_json::from_str(&json).unwrap();

        // Use pattern matching for comparison
        match (&status, &deserialized) {
            (AgentStatus::Pending, AgentStatus::Pending) => {}
            (AgentStatus::Running, AgentStatus::Running) => {}
            (AgentStatus::Success, AgentStatus::Success) => {}
            (AgentStatus::Failed(a), AgentStatus::Failed(b)) if a == b => {}
            (AgentStatus::Timeout, AgentStatus::Timeout) => {}
            (AgentStatus::Retrying(a), AgentStatus::Retrying(b)) if a == b => {}
            _ => panic!("Deserialization mismatch"),
        }
    }
}

#[test]
fn test_resume_options_defaults() {
    let options = ResumeOptions::default();
    assert!(!options.reprocess_failed);
    assert_eq!(options.max_parallel, None);
    assert!(!options.skip_validation);
}

#[test]
fn test_resume_result_serialization() {
    let result = ResumeResult {
        job_id: "test-job-123".to_string(),
        resumed_from_version: 5,
        total_items: 100,
        already_completed: 75,
        remaining_items: 25,
        final_results: vec![AgentResult {
            item_id: "item_0".to_string(),
            status: AgentStatus::Success,
            output: Some("output".to_string()),
            commits: vec!["abc123".to_string()],
            duration: Duration::from_secs(10),
            error: None,
            worktree_path: None,
            branch_name: None,
            worktree_session_id: None,
            files_modified: vec![],
            json_log_location: None,
            cleanup_status: None,
        }],
    };

    let json = serde_json::to_string(&result).unwrap();
    let deserialized: ResumeResult = serde_json::from_str(&json).unwrap();

    assert_eq!(deserialized.job_id, result.job_id);
    assert_eq!(
        deserialized.resumed_from_version,
        result.resumed_from_version
    );
    assert_eq!(deserialized.total_items, result.total_items);
    assert_eq!(deserialized.already_completed, result.already_completed);
    assert_eq!(deserialized.remaining_items, result.remaining_items);
    assert_eq!(deserialized.final_results.len(), result.final_results.len());
}

#[test]
fn test_mapreduce_config_defaults() {
    let config = MapReduceConfig {
        agent_timeout_secs: None,
        continue_on_failure: false,
        batch_size: None,
        enable_checkpoints: true,
        input: "test.json".to_string(),
        json_path: "$.items[*]".to_string(),
        max_parallel: 5,
        max_items: None,
        offset: None,
    };

    assert_eq!(config.max_parallel, 5);
}

#[test]
fn test_agent_context_creation() {
    let env = ExecutionEnvironment {
        working_dir: Arc::new(PathBuf::from("/test/worktree")),
        project_dir: Arc::new(PathBuf::from("/test/project")),
        worktree_name: Some(Arc::from("test-worktree")),
        session_id: Arc::from("test-session"),
    };

    let context = AgentContext::new(
        "item-1".to_string(),
        PathBuf::from("/test/worktree"),
        "test-worktree".to_string(),
        env,
    );

    assert_eq!(context.item_id, "item-1");
    assert_eq!(context.worktree_path, PathBuf::from("/test/worktree"));
    assert_eq!(context.worktree_name, "test-worktree");
    assert_eq!(context.retry_count, 0);
    assert!(context.shell_output.is_none());
    assert!(context.variables.is_empty());
}

#[test]
fn test_agent_context_update_with_output() {
    let env = ExecutionEnvironment {
        working_dir: Arc::new(PathBuf::from("/test/worktree")),
        project_dir: Arc::new(PathBuf::from("/test/project")),
        worktree_name: Some(Arc::from("test-worktree")),
        session_id: Arc::from("test-session"),
    };

    let mut context = AgentContext::new(
        "item-1".to_string(),
        PathBuf::from("/test/worktree"),
        "test-worktree".to_string(),
        env,
    );

    // Update with output
    context.update_with_output(Some("test output".to_string()));

    assert_eq!(context.shell_output, Some("test output".to_string()));
    assert_eq!(
        context.variables.get("shell.output"),
        Some(&"test output".to_string())
    );
    assert_eq!(
        context.variables.get("shell.last_output"),
        Some(&"test output".to_string())
    );
}

#[test]
fn test_agent_context_to_interpolation_context() {
    let env = ExecutionEnvironment {
        working_dir: Arc::new(PathBuf::from("/test/worktree")),
        project_dir: Arc::new(PathBuf::from("/test/project")),
        worktree_name: Some(Arc::from("test-worktree")),
        session_id: Arc::from("test-session"),
    };

    let mut context = AgentContext::new(
        "item-1".to_string(),
        PathBuf::from("/test/worktree"),
        "test-worktree".to_string(),
        env,
    );

    // Add some test data
    context
        .variables
        .insert("key1".to_string(), "value1".to_string());
    context.shell_output = Some("shell output".to_string());
    context
        .captured_outputs
        .insert("capture1".to_string(), "captured".to_string());
    context
        .iteration_vars
        .insert("iter1".to_string(), "iteration".to_string());

    let interp_context = context.to_interpolation_context();

    // Verify the interpolation context contains all the data by using interpolation
    let mut engine = InterpolationEngine::new(false);
    assert_eq!(
        engine.interpolate("${key1}", &interp_context).unwrap(),
        "value1"
    );
    assert_eq!(
        engine.interpolate("${capture1}", &interp_context).unwrap(),
        "captured"
    );
    assert_eq!(
        engine.interpolate("${iter1}", &interp_context).unwrap(),
        "iteration"
    );
    assert_eq!(
        engine
            .interpolate("${shell.output}", &interp_context)
            .unwrap(),
        "shell output"
    );
}

#[test]
fn test_agent_result_serialization() {
    let result = AgentResult {
        item_id: "test_item".to_string(),
        status: AgentStatus::Success,
        output: Some("test output".to_string()),
        commits: vec!["abc123".to_string(), "def456".to_string()],
        duration: Duration::from_secs(10),
        error: None,
        worktree_path: Some(PathBuf::from("<test-worktree-path>")),
        branch_name: Some("prodigy-agent-123-test_item".to_string()),
        worktree_session_id: Some("prodigy-session-123".to_string()),
        files_modified: vec!["src/main.rs".to_string()],
        json_log_location: None,
        cleanup_status: None,
    };

    let json = serde_json::to_string(&result).unwrap();
    let deserialized: AgentResult = serde_json::from_str(&json).unwrap();

    assert_eq!(deserialized.item_id, result.item_id);
    assert_eq!(deserialized.output, result.output);
    assert_eq!(deserialized.commits, result.commits);
    assert_eq!(deserialized.duration, result.duration);
    assert_eq!(deserialized.error, result.error);
    assert_eq!(deserialized.worktree_path, result.worktree_path);
}

#[test]
fn test_map_phase_configuration() {
    use crate::cook::workflow::WorkflowStep;

    let map_phase = MapPhase {
        config: MapReduceConfig {
            agent_timeout_secs: None,
            continue_on_failure: false,
            batch_size: None,
            enable_checkpoints: true,
            input: "items.json".to_string(),
            json_path: "$.items[*]".to_string(),
            max_parallel: 20,
            max_items: None,
            offset: None,
        },
        json_path: Some("$.items[*]".to_string()),
        agent_template: vec![WorkflowStep {
            name: None,
            claude: Some("/fix-issue ${item.description}".to_string()),
            shell: None,
            test: None,
            foreach: None,
            write_file: None,
            command: None,
            handler: None,
            capture_output: CaptureOutput::Disabled,
            capture: None,
            capture_format: None,
            capture_streams: Default::default(),
            auto_commit: false,
            commit_config: None,
            output_file: None,
            timeout: None,
            working_dir: None,
            env: HashMap::new(),
            on_failure: None,
            retry: None,
            on_success: None,
            on_exit_code: HashMap::new(),
            commit_required: true,
            validate: None,
            step_validate: None,
            skip_validation: false,
            validation_timeout: None,
            ignore_validation_failure: false,
            when: None,
        }],
        filter: Some("severity == 'high'".to_string()),
        sort_by: Some("priority".to_string()),
        max_items: None,
        distinct: None,
        timeout_config: None,
        workflow_env: HashMap::new(),
    };

    assert_eq!(map_phase.config.max_parallel, 20);
    assert_eq!(map_phase.agent_template.len(), 1);
    assert_eq!(map_phase.filter, Some("severity == 'high'".to_string()));
    assert_eq!(map_phase.sort_by, Some("priority".to_string()));
}

#[test]
fn test_reduce_phase_configuration() {
    use crate::cook::workflow::WorkflowStep;

    let reduce_phase = ReducePhase {
        commands: vec![
            WorkflowStep {
                name: None,
                claude: Some("/summarize-results".to_string()),
                shell: None,
                test: None,
                foreach: None,
                write_file: None,
                command: None,
                handler: None,
                capture_output: CaptureOutput::Disabled,
                capture: None,
                capture_format: None,
                capture_streams: Default::default(),
                output_file: None,
                timeout: None,
                working_dir: None,
                env: HashMap::new(),
                on_failure: None,
                retry: None,
                on_success: None,
                on_exit_code: HashMap::new(),
                commit_required: false,
                auto_commit: false,
                commit_config: None,
                validate: None,
                step_validate: None,
                skip_validation: false,
                validation_timeout: None,
                ignore_validation_failure: false,
                when: None,
            },
            WorkflowStep {
                name: None,
                claude: None,
                shell: Some("git merge --no-ff agent-*".to_string()),
                test: None,
                foreach: None,
                write_file: None,
                command: None,
                handler: None,
                capture_output: CaptureOutput::Disabled,
                capture: None,
                capture_format: None,
                capture_streams: Default::default(),
                output_file: None,
                timeout: None,
                working_dir: None,
                env: HashMap::new(),
                on_failure: None,
                retry: None,
                on_success: None,
                on_exit_code: HashMap::new(),
                commit_required: true,
                auto_commit: false,
                commit_config: None,
                validate: None,
                step_validate: None,
                skip_validation: false,
                validation_timeout: None,
                ignore_validation_failure: false,
                when: None,
            },
        ],
        timeout_secs: None,
    };

    assert_eq!(reduce_phase.commands.len(), 2);
    assert!(reduce_phase.commands[0].claude.is_some());
    assert!(reduce_phase.commands[1].shell.is_some());
}

#[test]
fn test_reduce_phase_variable_substitution() {
    // Test that map results are properly available as variables in reduce phase
    let env = ExecutionEnvironment {
        working_dir: Arc::new(PathBuf::from("/test/worktree")),
        project_dir: Arc::new(PathBuf::from("/test/project")),
        worktree_name: Some(Arc::from("test-worktree")),
        session_id: Arc::from("test-session"),
    };

    let mut reduce_context = AgentContext::new(
        "reduce".to_string(),
        PathBuf::from("/test/worktree"),
        "test-worktree".to_string(),
        env,
    );

    // Simulate adding map results to reduce context (this is what was missing!)
    reduce_context
        .variables
        .insert("map.successful".to_string(), "3".to_string());
    reduce_context
        .variables
        .insert("map.failed".to_string(), "1".to_string());
    reduce_context
        .variables
        .insert("map.total".to_string(), "4".to_string());

    // Convert to interpolation context
    let interp_context = reduce_context.to_interpolation_context();

    // Test that variables are accessible
    assert_eq!(
        interp_context
            .variables
            .get("map.successful")
            .and_then(|v| v.as_str()),
        Some("3")
    );
    assert_eq!(
        interp_context
            .variables
            .get("map.failed")
            .and_then(|v| v.as_str()),
        Some("1")
    );
    assert_eq!(
        interp_context
            .variables
            .get("map.total")
            .and_then(|v| v.as_str()),
        Some("4")
    );
}

#[test]
fn test_reduce_phase_complex_variable_substitution() {
    // Test complex variable substitution including claude output
    let env = ExecutionEnvironment {
        working_dir: Arc::new(PathBuf::from("/test/worktree")),
        project_dir: Arc::new(PathBuf::from("/test/project")),
        worktree_name: Some(Arc::from("test-worktree")),
        session_id: Arc::from("test-session"),
    };

    let mut reduce_context = AgentContext::new(
        "reduce".to_string(),
        PathBuf::from("/test/worktree"),
        "test-worktree".to_string(),
        env,
    );

    // Add map statistics
    reduce_context
        .variables
        .insert("map.successful".to_string(), "5".to_string());
    reduce_context
        .variables
        .insert("map.failed".to_string(), "2".to_string());
    reduce_context
        .variables
        .insert("map.total".to_string(), "7".to_string());

    // Add claude output from a previous command (stored in captured_outputs)
    reduce_context.captured_outputs.insert(
        "claude.output".to_string(),
        "Debt reduction analysis: 30% improvement".to_string(),
    );

    // Add individual result data
    reduce_context
        .variables
        .insert("result.0.item_id".to_string(), "debt-item-1".to_string());
    reduce_context
        .variables
        .insert("result.0.status".to_string(), "success".to_string());
    reduce_context
        .variables
        .insert("result.1.item_id".to_string(), "debt-item-2".to_string());
    reduce_context
        .variables
        .insert("result.1.status".to_string(), "failed".to_string());

    let interp_context = reduce_context.to_interpolation_context();

    // Verify all variables are accessible
    assert_eq!(
        interp_context
            .variables
            .get("map.successful")
            .and_then(|v| v.as_str()),
        Some("5")
    );
    assert_eq!(
        interp_context
            .variables
            .get("claude.output")
            .and_then(|v| v.as_str()),
        Some("Debt reduction analysis: 30% improvement")
    );
    assert_eq!(
        interp_context
            .variables
            .get("result.0.item_id")
            .and_then(|v| v.as_str()),
        Some("debt-item-1")
    );
}

#[test]
fn test_reduce_context_has_map_variables() {
    // This test verifies the FIX: that reduce context gets map result variables
    // Before the fix, these variables were NOT added to the reduce context

    let env = ExecutionEnvironment {
        working_dir: Arc::new(PathBuf::from("/test/worktree")),
        project_dir: Arc::new(PathBuf::from("/test/project")),
        worktree_name: Some(Arc::from("test-worktree")),
        session_id: Arc::from("test-session"),
    };

    // Simulate what execute_reduce_phase does after our fix
    let mut reduce_context = AgentContext::new(
        "reduce".to_string(),
        (*env.working_dir).clone(),
        "test-worktree".to_string(),
        env.clone(),
    );

    // This is the CRITICAL FIX - adding map statistics to reduce context
    let successful_count = 3;
    let failed_count = 1;
    let total = 4;

    reduce_context
        .variables
        .insert("map.successful".to_string(), successful_count.to_string());
    reduce_context
        .variables
        .insert("map.failed".to_string(), failed_count.to_string());
    reduce_context
        .variables
        .insert("map.total".to_string(), total.to_string());

    // Verify the variables are available for interpolation
    let interp = reduce_context.to_interpolation_context();

    // These assertions would FAIL before our fix because the variables weren't added
    assert!(interp.variables.contains_key("map.successful"));
    assert!(interp.variables.contains_key("map.failed"));
    assert!(interp.variables.contains_key("map.total"));

    // Test interpolation of a shell command that uses these variables
    let _test_command = "echo 'Processed ${map.successful} of ${map.total} items'";

    // Before the fix, this would result in bad substitution error
    // After the fix, the variables are available for substitution
    assert_eq!(
        interp
            .variables
            .get("map.successful")
            .and_then(|v| v.as_str()),
        Some("3")
    );
}

#[test]
fn test_custom_capture_output_variables() {
    use crate::cook::workflow::CaptureOutput;

    // Test CaptureOutput enum functionality
    assert!(!CaptureOutput::Disabled.is_enabled());
    assert!(CaptureOutput::Default.is_enabled());
    assert!(CaptureOutput::Variable("my_output".to_string()).is_enabled());

    // Test variable name generation
    let claude_cmd = CommandType::Claude("test".to_string());
    let shell_cmd = CommandType::Shell("echo test".to_string());

    assert_eq!(CaptureOutput::Disabled.get_variable_name(&claude_cmd), None);

    assert_eq!(
        CaptureOutput::Default.get_variable_name(&claude_cmd),
        Some("claude.output".to_string())
    );

    assert_eq!(
        CaptureOutput::Default.get_variable_name(&shell_cmd),
        Some("shell.output".to_string())
    );

    assert_eq!(
        CaptureOutput::Variable("custom_var".to_string()).get_variable_name(&claude_cmd),
        Some("custom_var".to_string())
    );

    assert_eq!(
        CaptureOutput::Variable("my.special.output".to_string()).get_variable_name(&shell_cmd),
        Some("my.special.output".to_string())
    );
}

#[test]
fn test_interpolation_with_work_item() {
    let mut engine = InterpolationEngine::new(false);
    let mut context = InterpolationContext::new();

    // Add a work item to context
    let item = json!({
        "id": 123,
        "description": "Fix memory leak in parser",
        "priority": "high",
        "location": {
            "file": "src/parser.rs",
            "line": 45
        }
    });

    context.set("item", item);

    // Test various interpolation patterns
    let tests = vec![
        ("Task ${item.id}", "Task 123"),
        ("Fix: ${item.description}", "Fix: Fix memory leak in parser"),
        ("Priority: ${item.priority}", "Priority: high"),
        (
            "File: ${item.location.file}:${item.location.line}",
            "File: src/parser.rs:45",
        ),
    ];

    for (template, expected) in tests {
        let result = engine.interpolate(template, &context).unwrap();
        assert_eq!(result, expected, "Failed for template: {}", template);
    }
}

#[test]
fn test_interpolation_with_map_results() {
    let mut engine = InterpolationEngine::new(false);
    let mut context = InterpolationContext::new();

    // Add map results to context as a nested object
    context.set(
        "map",
        json!({
            "successful": 8,
            "failed": 2,
            "total": 10
        }),
    );

    let template =
        "Processed ${map.total} items: ${map.successful} successful, ${map.failed} failed";
    let result = engine.interpolate(template, &context).unwrap();
    assert_eq!(result, "Processed 10 items: 8 successful, 2 failed");
}

#[test]
fn test_interpolation_with_shell_output() {
    let mut engine = InterpolationEngine::new(false);
    let mut context = InterpolationContext::new();

    // Simulate shell output from previous step as a nested object
    context.set(
        "shell",
        json!({
            "output": "All tests passed",
            "last_output": "Coverage: 85%"
        }),
    );

    let template = "Previous output: ${shell.output}. ${shell.last_output}";
    let result = engine.interpolate(template, &context).unwrap();
    assert_eq!(result, "Previous output: All tests passed. Coverage: 85%");
}

#[test]
fn test_interpolation_with_defaults() {
    let mut engine = InterpolationEngine::new(false);
    let context = InterpolationContext::new();

    // Test default values for undefined variables
    let tests = vec![
        ("Timeout: ${timeout:-600}s", "Timeout: 600s"),
        ("Workers: ${workers:-10}", "Workers: 10"),
        ("Mode: ${mode:-parallel}", "Mode: parallel"),
    ];

    for (template, expected) in tests {
        let result = engine.interpolate(template, &context).unwrap();
        assert_eq!(result, expected, "Failed for template: {}", template);
    }
}

#[test]
fn test_interpolation_context_hierarchy() {
    let mut engine = InterpolationEngine::new(false);

    // Create parent context
    let mut parent = InterpolationContext::new();
    parent.set("global_setting", json!("production"));
    parent.set("max_workers", json!(20));

    // Create child context
    let mut child = parent.child();
    child.set("local_setting", json!("debug"));
    child.set("max_workers", json!(5)); // Override parent value

    // Test resolution
    let tests = vec![
        ("Mode: ${global_setting}", "Mode: production"),
        ("Debug: ${local_setting}", "Debug: debug"),
        ("Workers: ${max_workers}", "Workers: 5"), // Should use child's value
    ];

    for (template, expected) in tests {
        let result = engine.interpolate(template, &child).unwrap();
        assert_eq!(result, expected, "Failed for template: {}", template);
    }
}

#[test]
fn test_interpolation_with_arrays() {
    let mut engine = InterpolationEngine::new(false);
    let mut context = InterpolationContext::new();

    // Add array data
    let results = json!([
        {"id": "item1", "status": "success"},
        {"id": "item2", "status": "failed"},
        {"id": "item3", "status": "success"}
    ]);

    context.set("results", results);

    // Test array access
    let tests = vec![
        ("First: ${results[0].id}", "First: item1"),
        (
            "Second status: ${results[1].status}",
            "Second status: failed",
        ),
        ("Third: ${results[2].id}", "Third: item3"),
    ];

    for (template, expected) in tests {
        let result = engine.interpolate(template, &context).unwrap();
        assert_eq!(result, expected, "Failed for template: {}", template);
    }
}

#[test]
fn test_interpolation_strict_mode() {
    let mut engine = InterpolationEngine::new(true); // strict mode
    let context = InterpolationContext::new();

    // Should fail on undefined variable in strict mode
    let result = engine.interpolate("Value: ${undefined}", &context);
    assert!(result.is_err());

    // Should work with default value even in strict mode
    let result = engine.interpolate("Value: ${undefined:-default}", &context);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), "Value: default");
}

// TODO: Add test for json_path_extraction once mock types are properly configured
// The interpolation functionality is tested comprehensively in the tests above

#[cfg(test)]
mod command_type_tests {
    use super::*;
    use crate::cook::workflow::{CaptureOutput, WorkflowStep};

    #[test]
    fn test_collect_command_types_single_claude() {
        let step = WorkflowStep {
            claude: Some("/test-command".to_string()),
            name: None,
            shell: None,
            test: None,
            foreach: None,
            write_file: None,
            command: None,
            handler: None,
            capture_output: CaptureOutput::Disabled,
            capture: None,
            capture_format: None,
            capture_streams: Default::default(),
            output_file: None,
            on_failure: None,
            retry: None,
            on_success: None,
            timeout: None,
            working_dir: None,
            env: HashMap::new(),
            on_exit_code: HashMap::new(),
            commit_required: true,
            auto_commit: false,
            commit_config: None,
            validate: None,
            step_validate: None,
            skip_validation: false,
            validation_timeout: None,
            ignore_validation_failure: false,
            when: None,
        };

        let commands = crate::cook::execution::mapreduce::command::collect_command_types(&step);
        assert_eq!(commands.len(), 1);
        matches!(commands[0], crate::cook::workflow::CommandType::Claude(_));
    }

    #[test]
    fn test_collect_command_types_single_shell() {
        let step = WorkflowStep {
            shell: Some("echo test".to_string()),
            name: None,
            claude: None,
            test: None,
            foreach: None,
            write_file: None,
            command: None,
            handler: None,
            capture_output: CaptureOutput::Disabled,
            capture: None,
            capture_format: None,
            capture_streams: Default::default(),
            output_file: None,
            on_failure: None,
            retry: None,
            on_success: None,
            timeout: None,
            working_dir: None,
            env: HashMap::new(),
            on_exit_code: HashMap::new(),
            commit_required: true,
            auto_commit: false,
            commit_config: None,
            validate: None,
            step_validate: None,
            skip_validation: false,
            validation_timeout: None,
            ignore_validation_failure: false,
            when: None,
        };

        let commands = crate::cook::execution::mapreduce::command::collect_command_types(&step);
        assert_eq!(commands.len(), 1);
        matches!(commands[0], crate::cook::workflow::CommandType::Shell(_));
    }

    #[test]
    fn test_collect_command_types_multiple() {
        let step = WorkflowStep {
            claude: Some("/test-command".to_string()),
            shell: Some("echo test".to_string()),
            name: None,
            test: None,
            foreach: None,
            write_file: None,
            command: None,
            handler: None,
            capture_output: CaptureOutput::Disabled,
            capture: None,
            capture_format: None,
            capture_streams: Default::default(),
            output_file: None,
            on_failure: None,
            retry: None,
            on_success: None,
            timeout: None,
            working_dir: None,
            env: HashMap::new(),
            on_exit_code: HashMap::new(),
            commit_required: true,
            auto_commit: false,
            commit_config: None,
            validate: None,
            step_validate: None,
            skip_validation: false,
            validation_timeout: None,
            ignore_validation_failure: false,
            when: None,
        };

        let commands = crate::cook::execution::mapreduce::command::collect_command_types(&step);
        assert_eq!(commands.len(), 2);
    }

    #[test]
    fn test_collect_command_types_legacy_with_slash() {
        let step = WorkflowStep {
            name: Some("/legacy-command".to_string()),
            claude: None,
            shell: None,
            test: None,
            foreach: None,
            write_file: None,
            command: None,
            handler: None,
            capture_output: CaptureOutput::Disabled,
            capture: None,
            capture_format: None,
            capture_streams: Default::default(),
            output_file: None,
            on_failure: None,
            retry: None,
            on_success: None,
            timeout: None,
            working_dir: None,
            env: HashMap::new(),
            on_exit_code: HashMap::new(),
            commit_required: true,
            auto_commit: false,
            commit_config: None,
            validate: None,
            step_validate: None,
            skip_validation: false,
            validation_timeout: None,
            ignore_validation_failure: false,
            when: None,
        };

        let commands = crate::cook::execution::mapreduce::command::collect_command_types(&step);
        assert_eq!(commands.len(), 1);
        if let CommandType::Legacy(cmd) = &commands[0] {
            assert_eq!(cmd, "/legacy-command");
        } else {
            panic!("Expected Legacy command type");
        }
    }

    #[test]
    fn test_collect_command_types_legacy_without_slash() {
        let step = WorkflowStep {
            name: Some("legacy-command".to_string()),
            claude: None,
            shell: None,
            test: None,
            foreach: None,
            write_file: None,
            command: None,
            handler: None,
            capture_output: CaptureOutput::Disabled,
            capture: None,
            capture_format: None,
            capture_streams: Default::default(),
            output_file: None,
            on_failure: None,
            retry: None,
            on_success: None,
            timeout: None,
            working_dir: None,
            env: HashMap::new(),
            on_exit_code: HashMap::new(),
            commit_required: true,
            auto_commit: false,
            commit_config: None,
            validate: None,
            step_validate: None,
            skip_validation: false,
            validation_timeout: None,
            ignore_validation_failure: false,
            when: None,
        };

        let commands = crate::cook::execution::mapreduce::command::collect_command_types(&step);
        assert_eq!(commands.len(), 1);
        if let CommandType::Legacy(cmd) = &commands[0] {
            assert_eq!(cmd, "/legacy-command");
        } else {
            panic!("Expected Legacy command type");
        }
    }

    #[test]
    fn test_validate_command_count_empty() {
        let commands = vec![];
        let result = crate::cook::execution::mapreduce::command::validate_command_count(&commands);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("No command specified"));
    }

    #[test]
    fn test_validate_command_count_single() {
        let commands = vec![CommandType::Shell("echo test".to_string())];
        let result = crate::cook::execution::mapreduce::command::validate_command_count(&commands);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_command_count_multiple() {
        let commands = vec![
            CommandType::Shell("echo test".to_string()),
            CommandType::Claude("/test".to_string()),
        ];
        let result = crate::cook::execution::mapreduce::command::validate_command_count(&commands);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Multiple command types specified"));
    }

    // Removed format_legacy_command tests as this method no longer exists in the refactored module
}

#[cfg(test)]
mod merge_history_tests {
    use crate::cook::commit_tracker::TrackedCommit;
    use chrono::Utc;
    use std::path::PathBuf;

    #[test]
    fn test_merge_preserves_commit_structure() {
        // Test that commit structure is preserved through merge operations
        // This verifies that TrackedCommit maintains all fields properly

        let timestamp = Utc::now();
        let commit = TrackedCommit {
            hash: "abc123def456".to_string(),
            message: "feat: add new feature".to_string(),
            author: "Test Author".to_string(),
            timestamp,
            files_changed: vec![PathBuf::from("src/main.rs"), PathBuf::from("src/lib.rs")],
            insertions: 50,
            deletions: 10,
            step_name: "map-agent-1".to_string(),
            agent_id: Some("agent-001".to_string()),
        };

        // Serialize and deserialize to ensure structure is preserved
        let json = serde_json::to_string(&commit).unwrap();
        let deserialized: serde_json::Value = serde_json::from_str(&json).unwrap();

        // Verify all fields are present and correct
        assert_eq!(deserialized["hash"], "abc123def456");
        assert_eq!(deserialized["message"], "feat: add new feature");
        assert_eq!(deserialized["author"], "Test Author");
        assert_eq!(deserialized["insertions"], 50);
        assert_eq!(deserialized["deletions"], 10);
        assert_eq!(deserialized["step_name"], "map-agent-1");
        assert_eq!(deserialized["agent_id"], "agent-001");
        assert!(deserialized["files_changed"].is_array());
        assert_eq!(deserialized["files_changed"].as_array().unwrap().len(), 2);
    }

    #[test]
    fn test_multiple_commits_history_format() {
        // Test that multiple commits maintain proper order and structure
        let timestamp = Utc::now();

        let commits = vec![
            TrackedCommit {
                hash: "commit1".to_string(),
                message: "First commit".to_string(),
                author: "Author 1".to_string(),
                timestamp,
                files_changed: vec![PathBuf::from("file1.rs")],
                insertions: 10,
                deletions: 5,
                step_name: "step1".to_string(),
                agent_id: Some("agent-1".to_string()),
            },
            TrackedCommit {
                hash: "commit2".to_string(),
                message: "Second commit".to_string(),
                author: "Author 2".to_string(),
                timestamp: timestamp + chrono::Duration::minutes(5),
                files_changed: vec![PathBuf::from("file2.rs")],
                insertions: 20,
                deletions: 3,
                step_name: "step2".to_string(),
                agent_id: Some("agent-2".to_string()),
            },
            TrackedCommit {
                hash: "commit3".to_string(),
                message: "Third commit".to_string(),
                author: "Author 3".to_string(),
                timestamp: timestamp + chrono::Duration::minutes(10),
                files_changed: vec![PathBuf::from("file3.rs")],
                insertions: 15,
                deletions: 8,
                step_name: "step3".to_string(),
                agent_id: None,
            },
        ];

        // Verify commits maintain order
        for (i, commit) in commits.iter().enumerate() {
            assert_eq!(commit.hash, format!("commit{}", i + 1));
            assert!(commit
                .message
                .contains(&["First", "Second", "Third"][i].to_string()));
        }

        // Verify serialization preserves all commits
        let json = serde_json::to_string(&commits).unwrap();
        let parsed: Vec<serde_json::Value> = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.len(), 3);

        // Verify agent_id handling (some have it, some don't)
        assert!(parsed[0]["agent_id"].is_string());
        assert!(parsed[1]["agent_id"].is_string());
        assert!(parsed[2]["agent_id"].is_null());
    }

    #[test]
    fn test_worktree_merge_scenario() {
        // Simulate a typical worktree merge scenario with commits
        let base_time = Utc::now();

        // Agent commits in worktree
        let agent_commits = vec![
            TrackedCommit {
                hash: "agent1hash".to_string(),
                message: "feat: implement feature A".to_string(),
                author: "Agent".to_string(),
                timestamp: base_time,
                files_changed: vec![PathBuf::from("feature_a.rs")],
                insertions: 100,
                deletions: 0,
                step_name: "map-phase".to_string(),
                agent_id: Some("worker-1".to_string()),
            },
            TrackedCommit {
                hash: "agent2hash".to_string(),
                message: "test: add tests for feature A".to_string(),
                author: "Agent".to_string(),
                timestamp: base_time + chrono::Duration::minutes(1),
                files_changed: vec![PathBuf::from("tests/feature_a_test.rs")],
                insertions: 50,
                deletions: 0,
                step_name: "map-phase".to_string(),
                agent_id: Some("worker-1".to_string()),
            },
        ];

        // Verify commit history would be preserved (all fields intact)
        for commit in &agent_commits {
            assert!(!commit.hash.is_empty());
            assert!(!commit.message.is_empty());
            assert!(commit.agent_id.is_some());
            assert_eq!(commit.step_name, "map-phase");
        }

        // Simulate merged history (parent + agent commits)
        let mut full_history = Vec::new();
        full_history.push(TrackedCommit {
            hash: "parenthash".to_string(),
            message: "Previous work on main branch".to_string(),
            author: "Developer".to_string(),
            timestamp: base_time - chrono::Duration::hours(1),
            files_changed: vec![PathBuf::from("main.rs")],
            insertions: 30,
            deletions: 10,
            step_name: "initial".to_string(),
            agent_id: None,
        });
        full_history.extend(agent_commits);

        // Verify combined history maintains chronological order
        assert_eq!(full_history.len(), 3);

        // Verify each commit retains its identity
        assert!(full_history[0].agent_id.is_none()); // Parent commit
        assert!(full_history[1].agent_id.is_some()); // Agent commit 1
        assert!(full_history[2].agent_id.is_some()); // Agent commit 2
    }
}

// ============================================================================
// Mock Implementations for Testing
// ============================================================================

#[cfg(test)]
pub mod test_mocks {
    use super::*;
    use crate::cook::execution::events::MapReduceEvent;
    use crate::worktree::WorktreeSession;
    use std::collections::VecDeque;
    use std::sync::{Arc, Mutex as StdMutex};

    /// Mock WorktreeManager for testing parallel execution
    pub struct MockWorktreeManager {
        pub sessions: Arc<StdMutex<Vec<WorktreeSession>>>,
        pub fail_on_create: bool,
        pub fail_on_cleanup: bool,
        pub create_delay_ms: u64,
    }

    impl Default for MockWorktreeManager {
        fn default() -> Self {
            Self::new()
        }
    }

    impl MockWorktreeManager {
        pub fn new() -> Self {
            Self {
                sessions: Arc::new(StdMutex::new(Vec::new())),
                fail_on_create: false,
                fail_on_cleanup: false,
                create_delay_ms: 0,
            }
        }

        pub async fn create_worktree(&self, branch_name: &str) -> anyhow::Result<WorktreeSession> {
            if self.create_delay_ms > 0 {
                tokio::time::sleep(Duration::from_millis(self.create_delay_ms)).await;
            }

            if self.fail_on_create {
                anyhow::bail!("Mock worktree creation failed");
            }

            let session = WorktreeSession {
                name: format!("mock-worktree-{}", branch_name),
                path: PathBuf::from(format!("/mock/worktree/{}", branch_name)),
                branch: branch_name.to_string(),
                created_at: chrono::Utc::now(),
            };

            self.sessions.lock().unwrap().push(session.clone());
            Ok(session)
        }

        pub async fn cleanup_worktree(&self, _name: &str) -> anyhow::Result<()> {
            if self.fail_on_cleanup {
                anyhow::bail!("Mock worktree cleanup failed");
            }
            Ok(())
        }
    }

    /// Mock CommandExecutor for testing command execution
    pub struct MockCommandExecutor {
        pub responses: Arc<StdMutex<VecDeque<(bool, String)>>>,
        pub executed_commands: Arc<StdMutex<Vec<String>>>,
        pub fail_on_command: Option<String>,
        pub execution_delay_ms: u64,
    }

    impl Default for MockCommandExecutor {
        fn default() -> Self {
            Self::new()
        }
    }

    impl MockCommandExecutor {
        pub fn new() -> Self {
            Self {
                responses: Arc::new(StdMutex::new(VecDeque::new())),
                executed_commands: Arc::new(StdMutex::new(Vec::new())),
                fail_on_command: None,
                execution_delay_ms: 0,
            }
        }

        pub fn add_response(&self, success: bool, output: String) {
            self.responses.lock().unwrap().push_back((success, output));
        }
    }

    /// Mock EventLogger for testing event logging
    pub struct MockEventLogger {
        pub events: Arc<StdMutex<Vec<MapReduceEvent>>>,
        pub fail_on_log: bool,
    }

    impl Default for MockEventLogger {
        fn default() -> Self {
            Self::new()
        }
    }

    impl MockEventLogger {
        pub fn new() -> Self {
            Self {
                events: Arc::new(StdMutex::new(Vec::new())),
                fail_on_log: false,
            }
        }

        pub fn get_events(&self) -> Vec<MapReduceEvent> {
            self.events.lock().unwrap().clone()
        }

        pub async fn log_event(&self, event: MapReduceEvent) -> anyhow::Result<()> {
            if self.fail_on_log {
                anyhow::bail!("Mock event logging failed");
            }
            self.events.lock().unwrap().push(event);
            Ok(())
        }
    }
}

// ============================================================================
// Concurrency Tests for Parallel Agent Execution
// ============================================================================

#[cfg(test)]
mod concurrency_tests {
    use super::test_mocks::*;
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use tokio::sync::Semaphore;

    #[tokio::test]
    async fn test_parallel_agent_execution_limits() {
        // Test that max_parallel is respected
        let concurrent_count = Arc::new(AtomicUsize::new(0));
        let max_concurrent = Arc::new(AtomicUsize::new(0));

        let config = MapReduceConfig {
            agent_timeout_secs: None,
            continue_on_failure: false,
            batch_size: None,
            enable_checkpoints: true,
            input: "test.json".to_string(),
            json_path: "$.items[*]".to_string(),
            max_parallel: 3,
            max_items: Some(10),
            offset: None,
        };

        // Simulate 10 work items with max_parallel=3
        let semaphore = Arc::new(Semaphore::new(config.max_parallel));
        let mut handles = vec![];

        for i in 0..10 {
            let sem = semaphore.clone();
            let concurrent = concurrent_count.clone();
            let max_con = max_concurrent.clone();

            let handle = tokio::spawn(async move {
                let _permit = sem.acquire().await.unwrap();

                // Increment concurrent count
                let current = concurrent.fetch_add(1, Ordering::SeqCst) + 1;

                // Update max if needed
                loop {
                    let max = max_con.load(Ordering::SeqCst);
                    if current <= max {
                        break;
                    }
                    if max_con
                        .compare_exchange(max, current, Ordering::SeqCst, Ordering::SeqCst)
                        .is_ok()
                    {
                        break;
                    }
                }

                // Simulate work
                tokio::time::sleep(Duration::from_millis(50)).await;

                // Decrement concurrent count
                concurrent.fetch_sub(1, Ordering::SeqCst);

                format!("Agent {} completed", i)
            });

            handles.push(handle);
        }

        // Wait for all agents
        for handle in handles {
            let _ = handle.await;
        }

        // Verify max_parallel was respected
        let max_observed = max_concurrent.load(Ordering::SeqCst);
        assert!(
            max_observed <= 3,
            "Max concurrent agents ({}) exceeded limit of 3",
            max_observed
        );
    }

    #[tokio::test]
    async fn test_agent_timeout_handling() {
        // Test that agent timeouts are properly enforced
        let _config = MapReduceConfig {
            agent_timeout_secs: None,
            continue_on_failure: false,
            batch_size: None,
            enable_checkpoints: true,
            input: "test.json".to_string(),
            json_path: "$.items[*]".to_string(),
            max_parallel: 2,
            max_items: None,
            offset: None,
        };

        let start = std::time::Instant::now();

        // Simulate agent that takes too long
        let result = tokio::time::timeout(Duration::from_secs(1), async {
            // Use 1 second timeout for test
            tokio::time::sleep(Duration::from_secs(5)).await;
            "Should timeout"
        })
        .await;

        assert!(result.is_err(), "Agent should have timed out");
        assert!(
            start.elapsed() < Duration::from_secs(2),
            "Timeout took too long"
        );
    }

    #[tokio::test]
    async fn test_concurrent_worktree_creation() {
        // Test multiple agents creating worktrees concurrently
        let manager = Arc::new(MockWorktreeManager::new());
        let mut handles = vec![];

        for i in 0..5 {
            let mgr = manager.clone();
            let handle =
                tokio::spawn(async move { mgr.create_worktree(&format!("agent-{}", i)).await });
            handles.push(handle);
        }

        // Wait for all to complete
        let mut results = vec![];
        for handle in handles {
            results.push(handle.await.unwrap());
        }

        // All should succeed
        assert_eq!(results.iter().filter(|r| r.is_ok()).count(), 5);

        // Check all worktrees were created
        let sessions = manager.sessions.lock().unwrap();
        assert_eq!(sessions.len(), 5);
    }

    #[tokio::test]
    async fn test_agent_race_conditions() {
        // Test for race conditions in agent result collection
        let results = Arc::new(Mutex::new(Vec::<AgentResult>::new()));
        let mut handles = vec![];

        // Spawn multiple agents that write results concurrently
        for i in 0..20 {
            let res = results.clone();
            let handle = tokio::spawn(async move {
                // Random delay to increase chance of race conditions
                tokio::time::sleep(Duration::from_millis(i as u64 % 10)).await;

                let result = AgentResult {
                    item_id: format!("item-{}", i),
                    status: AgentStatus::Success,
                    output: Some(format!("Output {}", i)),
                    commits: vec![],
                    duration: Duration::from_millis(i as u64),
                    error: None,
                    worktree_path: None,
                    branch_name: None,
                    worktree_session_id: None,
                    files_modified: vec![],
                    json_log_location: None,
                    cleanup_status: None,
                };

                res.lock().await.push(result);
            });
            handles.push(handle);
        }

        // Wait for all
        for handle in handles {
            handle.await.unwrap();
        }

        // Verify all results were collected
        let final_results = results.lock().await;
        assert_eq!(final_results.len(), 20);

        // Verify no duplicates
        let mut ids: Vec<String> = final_results.iter().map(|r| r.item_id.clone()).collect();
        ids.sort();
        ids.dedup();
        assert_eq!(ids.len(), 20);
    }
}

// ============================================================================
// Dead Letter Queue (DLQ) Tests
// ============================================================================

#[cfg(test)]
mod dlq_tests {
    use super::*;
    use crate::cook::execution::dlq::{ErrorType, FailureDetail};
    use chrono::Utc;
    use serde_json::Value;
    use std::collections::HashMap;

    #[test]
    fn test_dlq_failure_detail_serialization() {
        // Test that FailureDetail can be properly serialized/deserialized
        let failure_detail = FailureDetail {
            attempt_number: 2,
            timestamp: Utc::now(),
            error_type: ErrorType::CommandFailed { exit_code: 1 },
            error_message: "Command execution failed".to_string(),
            error_context: None,
            stack_trace: Some("Stack trace here".to_string()),
            agent_id: "agent-123".to_string(),
            step_failed: "map-phase".to_string(),
            duration_ms: 1500,
            json_log_location: None,
        };

        let json = serde_json::to_value(&failure_detail).unwrap();
        let deserialized: FailureDetail = serde_json::from_value(json).unwrap();

        assert_eq!(deserialized.error_message, failure_detail.error_message);
        assert_eq!(deserialized.attempt_number, 2);
    }

    #[test]
    fn test_dlq_error_type_categorization() {
        // Test different error types
        let error_types = vec![
            ErrorType::Timeout,
            ErrorType::CommandFailed { exit_code: 1 },
            ErrorType::WorktreeError,
            ErrorType::ValidationFailed,
            ErrorType::Unknown,
        ];

        for err_type in error_types {
            let detail = FailureDetail {
                attempt_number: 1,
                timestamp: Utc::now(),
                error_type: err_type.clone(),
                error_message: "Test message".to_string(),
                error_context: None,
                stack_trace: None,
                agent_id: "test-agent".to_string(),
                step_failed: "test-step".to_string(),
                duration_ms: 1000,
                json_log_location: None,
            };

            let json = serde_json::to_value(&detail).unwrap();
            assert!(json.is_object());
        }
    }

    #[tokio::test]
    async fn test_dlq_failed_item_tracking() {
        // Simulate DLQ behavior with a simple in-memory store
        let mut failed_items: HashMap<String, Value> = HashMap::new();

        // Add failed items
        for i in 0..5 {
            let item_id = format!("item-{}", i);
            let item_data = json!({
                "id": item_id.clone(),
                "failure": {
                    "error_type": if i % 2 == 0 { "Timeout" } else { "CommandExecution" },
                    "message": format!("Failed item {}", i),
                    "retry_count": i,
                }
            });

            failed_items.insert(item_id, item_data);
        }

        assert_eq!(failed_items.len(), 5);

        // Simulate retry - remove successfully processed items
        failed_items.remove("item-0");
        failed_items.remove("item-2");
        failed_items.remove("item-4");

        // Verify only failed items remain
        assert_eq!(failed_items.len(), 2);
        assert!(failed_items.contains_key("item-1"));
        assert!(failed_items.contains_key("item-3"));
    }

    #[tokio::test]
    async fn test_dlq_persistence_simulation() {
        use std::fs;
        use tempfile::TempDir;

        // Test DLQ persistence to filesystem
        let temp_dir = TempDir::new().unwrap();
        let dlq_path = temp_dir.path().join("dlq");
        fs::create_dir_all(&dlq_path).unwrap();

        // Write failed item to disk
        let item_data = json!({
            "item_id": "test-item",
            "error": "Test failure",
            "retry_count": 1
        });

        let item_file = dlq_path.join("test-item.json");
        fs::write(&item_file, item_data.to_string()).unwrap();

        // Read back and verify
        let content = fs::read_to_string(&item_file).unwrap();
        let parsed: Value = serde_json::from_str(&content).unwrap();

        assert_eq!(parsed["item_id"], "test-item");
        assert_eq!(parsed["retry_count"], 1);
    }
}

// ============================================================================
// Job Recovery Tests (simplified for new structure)
// ============================================================================

#[cfg(test)]
mod recovery_tests {
    use super::*;
    use std::sync::atomic::{AtomicBool, Ordering};

    #[tokio::test]
    async fn test_sigint_handling_simulation() {
        // Test graceful shutdown on SIGINT
        let shutdown = Arc::new(AtomicBool::new(false));
        let shutdown_clone = shutdown.clone();

        // Simulate work with shutdown check
        let handle = tokio::spawn(async move {
            for i in 0..100 {
                if shutdown_clone.load(Ordering::Relaxed) {
                    // Save state and exit gracefully
                    return Ok::<_, String>(format!("Interrupted at item {}", i));
                }

                // Simulate work
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
            Ok("Completed all items".to_string())
        });

        // Simulate SIGINT after some time
        tokio::time::sleep(Duration::from_millis(50)).await;
        shutdown.store(true, Ordering::Relaxed);

        let result = handle.await.unwrap().unwrap();
        assert!(result.contains("Interrupted"));
    }

    #[tokio::test]
    async fn test_checkpoint_data_integrity() {
        // Test that checkpoint data maintains integrity during serialization
        use serde_json::Value;

        let checkpoint_data = json!({
            "job_id": "test-job-123",
            "work_items": [
                {"id": 1, "data": "item1"},
                {"id": 2, "data": "item2"}
            ],
            "completed_agents": ["agent-1", "agent-2"],
            "checkpoint_version": 1
        });

        // Serialize and deserialize
        let serialized = serde_json::to_string(&checkpoint_data).unwrap();
        let deserialized: Value = serde_json::from_str(&serialized).unwrap();

        // Verify integrity
        assert_eq!(deserialized["job_id"], "test-job-123");
        assert_eq!(deserialized["work_items"].as_array().unwrap().len(), 2);
        assert_eq!(
            deserialized["completed_agents"].as_array().unwrap().len(),
            2
        );
        assert_eq!(deserialized["checkpoint_version"], 1);
    }

    #[tokio::test]
    #[allow(clippy::unnecessary_to_owned)]
    async fn test_recovery_with_partial_results() {
        // Test recovery scenarios with partially completed work
        let mut completed_items = std::collections::HashSet::new();
        completed_items.insert("item-1".to_string());
        completed_items.insert("item-3".to_string());
        completed_items.insert("item-5".to_string());

        let total_items = ["item-1", "item-2", "item-3", "item-4", "item-5"];

        // Find items that still need processing
        let remaining: Vec<_> = total_items
            .iter()
            .filter(|item| !completed_items.contains(&item.to_string()))
            .collect();

        assert_eq!(remaining.len(), 2);
        assert!(remaining.contains(&&"item-2"));
        assert!(remaining.contains(&&"item-4"));
    }
}

// ============================================================================
// Additional tests for improving MapReduce coverage
// ============================================================================

#[cfg(test)]
mod additional_coverage_tests {
    use super::*;
    use crate::cook::workflow::WorkflowStep;

    #[test]
    fn test_setup_phase_edge_cases() {
        // Test SetupPhase with multiple configurations
        let setup_phase = SetupPhase {
            commands: vec![WorkflowStep {
                shell: Some("echo 'setup'".to_string()),
                claude: None,
                name: None,
                test: None,
                foreach: None,
                write_file: None,
                command: None,
                handler: None,
                capture_output: CaptureOutput::Variable("setup_output".to_string()),
                capture: None,
                capture_format: None,
                capture_streams: Default::default(),
                output_file: None,
                timeout: Some(30),
                working_dir: None,
                env: HashMap::new(),
                on_failure: None,
                retry: None,
                on_success: None,
                on_exit_code: HashMap::new(),
                commit_required: false,
                auto_commit: false,
                commit_config: None,
                validate: None,
                step_validate: None,
                skip_validation: false,
                validation_timeout: None,
                ignore_validation_failure: false,
                when: None,
            }],
            timeout: Some(60),
            capture_outputs: HashMap::from([(
                "setup_output".to_string(),
                crate::cook::execution::variable_capture::CaptureConfig::Simple(0),
            )]),
        };

        // Test serialization
        let serialized = serde_json::to_string(&setup_phase).unwrap();
        let deserialized: SetupPhase = serde_json::from_str(&serialized).unwrap();

        assert_eq!(deserialized.commands.len(), 1);
        assert_eq!(deserialized.timeout, Some(60));
        assert_eq!(deserialized.capture_outputs.len(), 1);
    }

    #[test]
    fn test_map_phase_with_all_options() {
        let map_phase = MapPhase {
            config: MapReduceConfig {
                agent_timeout_secs: None,
                continue_on_failure: false,
                batch_size: None,
                enable_checkpoints: true,
                input: "data.json".to_string(),
                json_path: "$.items[*]".to_string(),
                max_parallel: 50,
                max_items: Some(100),
                offset: Some(10),
            },
            agent_template: vec![WorkflowStep {
                claude: Some("/process ${item}".to_string()),
                shell: None,
                name: None,
                test: None,
                foreach: None,
                write_file: None,
                command: None,
                handler: None,
                capture_output: CaptureOutput::Default,
                capture: None,
                capture_format: None,
                capture_streams: Default::default(),
                output_file: None,
                timeout: None,
                working_dir: None,
                env: HashMap::new(),
                on_failure: None,
                retry: None,
                on_success: None,
                on_exit_code: HashMap::new(),
                commit_required: false,
                auto_commit: false,
                commit_config: None,
                validate: None,
                step_validate: None,
                skip_validation: false,
                validation_timeout: None,
                ignore_validation_failure: false,
                when: None,
            }],
            json_path: Some("$.items[*]".to_string()),
            filter: Some("item.priority == 'high'".to_string()),
            sort_by: Some("item.created_at DESC".to_string()),
            max_items: Some(100),
            distinct: Some("item.id".to_string()),
            timeout_config: None,
            workflow_env: HashMap::new(),
        };

        assert_eq!(map_phase.config.max_parallel, 50);
        assert_eq!(map_phase.config.max_items, Some(100));
        assert_eq!(map_phase.config.offset, Some(10));
        assert!(map_phase.filter.is_some());
        assert!(map_phase.sort_by.is_some());
        assert!(map_phase.distinct.is_some());

        // Verify configuration without problematic serialization
        // (CaptureOutput enum has serialization issues)
    }

    #[test]
    fn test_resume_options_edge_cases() {
        let options = vec![
            ResumeOptions::default(),
            ResumeOptions {
                reprocess_failed: true,
                max_parallel: Some(5),
                skip_validation: true,
                agent_timeout_secs: Some(300),
            },
            ResumeOptions {
                reprocess_failed: false,
                max_parallel: None,
                skip_validation: false,
                agent_timeout_secs: None,
            },
        ];

        for opt in options {
            let serialized = serde_json::to_string(&opt).unwrap();
            let deserialized: ResumeOptions = serde_json::from_str(&serialized).unwrap();

            assert_eq!(deserialized.reprocess_failed, opt.reprocess_failed);
            assert_eq!(deserialized.max_parallel, opt.max_parallel);
            assert_eq!(deserialized.skip_validation, opt.skip_validation);
            assert_eq!(deserialized.agent_timeout_secs, opt.agent_timeout_secs);
        }
    }

    #[test]
    fn test_agent_result_edge_cases() {
        // Test with minimal fields
        let minimal_result = AgentResult {
            item_id: "minimal".to_string(),
            status: AgentStatus::Pending,
            output: None,
            commits: vec![],
            duration: Duration::from_secs(0),
            error: None,
            worktree_path: None,
            branch_name: None,
            worktree_session_id: None,
            files_modified: vec![],
            json_log_location: None,
            cleanup_status: None,
        };

        assert_eq!(minimal_result.item_id, "minimal");
        assert!(matches!(minimal_result.status, AgentStatus::Pending));
        assert!(minimal_result.output.is_none());
        assert!(minimal_result.commits.is_empty());

        // Test with error status
        let error_result = AgentResult {
            item_id: "error-item".to_string(),
            status: AgentStatus::Failed("Test failure".to_string()),
            output: None,
            commits: vec![],
            duration: Duration::from_secs(5),
            error: Some("Detailed error message".to_string()),
            worktree_path: None,
            branch_name: None,
            worktree_session_id: None,
            files_modified: vec![],
            json_log_location: None,
            cleanup_status: None,
        };

        assert!(matches!(error_result.status, AgentStatus::Failed(_)));
        assert!(error_result.error.is_some());

        // Test serialization
        let serialized = serde_json::to_string(&error_result).unwrap();
        let deserialized: AgentResult = serde_json::from_str(&serialized).unwrap();

        assert_eq!(deserialized.item_id, error_result.item_id);
        assert_eq!(deserialized.error, error_result.error);
    }

    #[test]
    fn test_large_agent_result_collection() {
        // Test handling of many agent results
        let mut results = Vec::new();
        for i in 0..50 {
            results.push(AgentResult {
                item_id: format!("item-{}", i),
                status: if i % 5 == 0 {
                    AgentStatus::Failed(format!("Failed item {}", i))
                } else if i % 3 == 0 {
                    AgentStatus::Retrying(1)
                } else {
                    AgentStatus::Success
                },
                output: Some(format!("Output for item {}", i)),
                commits: if i % 2 == 0 {
                    vec![format!("commit-{}", i)]
                } else {
                    vec![]
                },
                duration: Duration::from_secs(i as u64),
                error: if i % 5 == 0 {
                    Some(format!("Error on item {}", i))
                } else {
                    None
                },
                worktree_path: None,
                branch_name: None,
                worktree_session_id: None,
                files_modified: vec![],
                json_log_location: None,
                cleanup_status: None,
            });
        }

        assert_eq!(results.len(), 50);

        // Count different statuses
        let success_count = results
            .iter()
            .filter(|r| matches!(r.status, AgentStatus::Success))
            .count();
        let failed_count = results
            .iter()
            .filter(|r| matches!(r.status, AgentStatus::Failed(_)))
            .count();
        let retrying_count = results
            .iter()
            .filter(|r| matches!(r.status, AgentStatus::Retrying(_)))
            .count();

        assert!(success_count > 0);
        assert!(failed_count > 0);
        assert!(retrying_count > 0);

        // Test serialization of the collection
        let serialized = serde_json::to_string(&results).unwrap();
        let deserialized: Vec<AgentResult> = serde_json::from_str(&serialized).unwrap();

        assert_eq!(deserialized.len(), 50);
    }
}

// ============================================================================
// Integration Tests for MapReduce Executor
// ============================================================================

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

    #[tokio::test]
    async fn test_end_to_end_mapreduce_flow() {
        // Test complete flow: setup -> map -> reduce
        let temp_dir = TempDir::new().unwrap();
        let work_dir = temp_dir.path();

        // Create test input file
        let input_file = work_dir.join("test_items.json");
        let items = json!({
            "items": [
                {"id": 1, "name": "task1", "priority": "high"},
                {"id": 2, "name": "task2", "priority": "low"},
                {"id": 3, "name": "task3", "priority": "high"},
            ]
        });
        std::fs::write(&input_file, items.to_string()).unwrap();

        // Create map phase configuration
        let map_config = MapReduceConfig {
            agent_timeout_secs: None,
            continue_on_failure: false,
            batch_size: None,
            enable_checkpoints: true,
            input: input_file.to_string_lossy().to_string(),
            json_path: "$.items[*]".to_string(),
            max_parallel: 2,
            max_items: None,
            offset: None,
        };

        // Verify configuration
        assert_eq!(map_config.max_parallel, 2);

        // Test JSON path extraction
        let json_content = std::fs::read_to_string(&input_file).unwrap();
        let parsed: Value = serde_json::from_str(&json_content).unwrap();

        // Simple JSONPath simulation for testing
        if let Some(items_array) = parsed.get("items").and_then(|v| v.as_array()) {
            assert_eq!(items_array.len(), 3);
            assert_eq!(items_array[0]["priority"], "high");
        }
    }

    #[tokio::test]
    async fn test_filter_and_sort_operations() {
        // Test filtering and sorting of work items
        let items = [
            json!({"id": 1, "priority": 3, "status": "active"}),
            json!({"id": 2, "priority": 1, "status": "inactive"}),
            json!({"id": 3, "priority": 2, "status": "active"}),
            json!({"id": 4, "priority": 1, "status": "active"}),
        ];

        // Simulate filter: status == "active"
        let filtered: Vec<_> = items
            .iter()
            .filter(|item| item["status"] == "active")
            .cloned()
            .collect();
        assert_eq!(filtered.len(), 3);

        // Simulate sort by priority
        let mut sorted = filtered.clone();
        sorted.sort_by_key(|item| item["priority"].as_u64().unwrap_or(0));
        assert_eq!(sorted[0]["id"], 4);
        assert_eq!(sorted[1]["id"], 3);
        assert_eq!(sorted[2]["id"], 1);
    }

    #[tokio::test]
    async fn test_offset_and_limit_processing() {
        // Test max_items and offset parameters
        let items: Vec<Value> = (0..100)
            .map(|i| json!({"id": i, "value": format!("item_{}", i)}))
            .collect();

        let config = MapReduceConfig {
            agent_timeout_secs: None,
            continue_on_failure: false,
            batch_size: None,
            enable_checkpoints: true,
            input: "dummy".to_string(),
            json_path: "$[*]".to_string(),
            max_parallel: 5,
            max_items: Some(20),
            offset: Some(10),
        };

        // Simulate offset and limit
        let start = config.offset.unwrap_or(0);
        let processed: Vec<_> = items
            .iter()
            .skip(start)
            .take(config.max_items.unwrap_or(items.len()))
            .collect();

        assert_eq!(processed.len(), 20);
        assert_eq!(processed[0]["id"], 10);
        assert_eq!(processed[19]["id"], 29);
    }
}