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
//! Core MapReduce execution coordinator
//!
//! This module coordinates the execution of MapReduce jobs,
//! managing phases and resource allocation.

use super::command_executor::CommandExecutor;
use crate::cook::execution::claude::ClaudeExecutorImpl;
use crate::cook::execution::data_pipeline::DataPipeline;
use crate::cook::execution::dlq::DeadLetterQueue;
use crate::cook::execution::errors::{MapReduceError, MapReduceResult};
use crate::cook::execution::input_source::InputSource;
use crate::cook::execution::mapreduce::{
    agent::{AgentConfig, AgentLifecycleManager, AgentResult, AgentStatus},
    aggregation::{AggregationSummary, CollectionStrategy, ResultCollector},
    dlq_integration,
    event::{EventLogger, MapReduceEvent},
    merge_queue::MergeQueue,
    resources::git::GitOperations,
    retry_tracking,
    state::StateManager,
    timeout::{TimeoutConfig, TimeoutEnforcer},
    types::{MapPhase, ReducePhase, SetupPhase},
};
use crate::cook::execution::runner::RealCommandRunner;
use crate::cook::execution::ClaudeExecutor;
use crate::cook::interaction::UserInteraction;
use crate::cook::orchestrator::ExecutionEnvironment;
use crate::cook::session::SessionManager;
use crate::cook::workflow::{OnFailureConfig, WorkflowStep};
use crate::subprocess::SubprocessManager;
use chrono::{DateTime, Utc};
use serde_json::Value;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{Mutex, Semaphore};
use tracing::{debug, info, warn};

/// Information about an orphaned worktree from cleanup failure
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct OrphanedWorktree {
    /// Path to the worktree directory
    pub path: PathBuf,
    /// Agent ID that created the worktree
    pub agent_id: String,
    /// Work item ID that was being processed
    pub item_id: String,
    /// Timestamp when the cleanup failure occurred
    pub failed_at: DateTime<Utc>,
    /// Error message from the cleanup failure
    pub error: String,
}

/// Main coordinator for MapReduce execution
pub struct MapReduceCoordinator {
    /// Agent lifecycle manager
    agent_manager: Arc<dyn AgentLifecycleManager>,
    /// State manager for job state
    _state_manager: Arc<StateManager>,
    /// User interaction handler
    user_interaction: Arc<dyn UserInteraction>,
    /// Result collector
    result_collector: Arc<ResultCollector>,
    /// Subprocess manager for command execution
    subprocess: Arc<SubprocessManager>,
    /// Project root directory
    project_root: PathBuf,
    /// Event logger for tracking execution
    event_logger: Arc<EventLogger>,
    /// Job ID for tracking
    job_id: String,
    /// Claude executor for Claude commands (used via command_executor)
    #[allow(dead_code)]
    pub(crate) claude_executor: Arc<dyn ClaudeExecutor>,
    /// Session manager
    _session_manager: Arc<dyn SessionManager>,
    /// Execution mode (normal or dry-run)
    execution_mode: crate::cook::execution::mapreduce::dry_run::ExecutionMode,
    /// Timeout enforcer for agent execution
    timeout_enforcer: Arc<Mutex<Option<Arc<TimeoutEnforcer>>>>,
    /// Merge queue for serializing agent merges
    merge_queue: Arc<MergeQueue>,
    /// Registry of orphaned worktrees from cleanup failures
    orphaned_worktrees: Arc<Mutex<Vec<OrphanedWorktree>>>,
    /// Dead Letter Queue for failed items
    dlq: Arc<DeadLetterQueue>,
    /// Retry counts for work items (for accurate DLQ attempt tracking)
    retry_counts: Arc<tokio::sync::RwLock<HashMap<String, u32>>>,
    /// Command executor for running workflow steps
    command_executor: CommandExecutor,
}

impl MapReduceCoordinator {
    /// Create a new coordinator
    pub fn new(
        agent_manager: Arc<dyn AgentLifecycleManager>,
        state_manager: Arc<StateManager>,
        user_interaction: Arc<dyn UserInteraction>,
        subprocess: Arc<SubprocessManager>,
        project_root: PathBuf,
    ) -> Self {
        Self::with_mode(
            agent_manager,
            state_manager,
            user_interaction,
            subprocess,
            project_root,
            crate::cook::execution::mapreduce::dry_run::ExecutionMode::Normal,
            0, // Default verbosity
        )
    }

    /// Create a new coordinator with execution mode
    pub fn with_mode(
        agent_manager: Arc<dyn AgentLifecycleManager>,
        state_manager: Arc<StateManager>,
        user_interaction: Arc<dyn UserInteraction>,
        subprocess: Arc<SubprocessManager>,
        project_root: PathBuf,
        execution_mode: crate::cook::execution::mapreduce::dry_run::ExecutionMode,
        verbosity: u8,
    ) -> Self {
        let result_collector = Arc::new(ResultCollector::new(CollectionStrategy::InMemory));
        let job_id = format!("mapreduce-{}", chrono::Utc::now().format("%Y%m%d_%H%M%S"));
        let event_logger = Arc::new(EventLogger::new(
            project_root.clone(),
            job_id.clone(),
            None,
            verbosity,
        ));

        // Create claude executor using the real implementation
        let command_runner = RealCommandRunner::new();
        let claude_executor: Arc<dyn ClaudeExecutor> =
            Arc::new(ClaudeExecutorImpl::new(command_runner));

        // Create session manager - not used but required for struct
        let session_manager = Arc::new(DummySessionManager);

        // Create merge queue for serializing agent merges with Claude support
        let git_ops = Arc::new(GitOperations::new());
        let merge_queue = Arc::new(MergeQueue::new_with_claude(
            git_ops,
            Some(claude_executor.clone()),
            verbosity,
        ));

        // Initialize DLQ for failed items tracking
        let dlq = tokio::task::block_in_place(|| {
            tokio::runtime::Handle::current().block_on(async {
                crate::storage::create_global_dlq(&project_root, &job_id, None)
                    .await
                    .unwrap_or_else(|e| {
                        warn!("Failed to create global DLQ: {}, using fallback", e);
                        // Create fallback DLQ with temp path
                        let temp_path = std::env::temp_dir().join("prodigy_dlq");
                        tokio::task::block_in_place(|| {
                            tokio::runtime::Handle::current().block_on(async {
                                DeadLetterQueue::new(job_id.clone(), temp_path, 1000, 30, None)
                                    .await
                                    .expect("Failed to create fallback DLQ")
                            })
                        })
                    })
            })
        });

        // Create command executor
        let command_executor = CommandExecutor::new(claude_executor.clone(), subprocess.clone());

        Self {
            agent_manager,
            _state_manager: state_manager,
            user_interaction,
            result_collector,
            subprocess,
            project_root,
            event_logger,
            job_id,
            claude_executor,
            _session_manager: session_manager,
            execution_mode,
            timeout_enforcer: Arc::new(Mutex::new(None)),
            merge_queue,
            orphaned_worktrees: Arc::new(Mutex::new(Vec::new())),
            dlq: Arc::new(dlq),
            retry_counts: Arc::new(tokio::sync::RwLock::new(HashMap::new())),
            command_executor,
        }
    }

    /// Get the list of orphaned worktrees
    pub async fn get_orphaned_worktrees(&self) -> Vec<OrphanedWorktree> {
        self.orphaned_worktrees.lock().await.clone()
    }

    /// Register an orphaned worktree when cleanup fails
    pub async fn register_orphaned_worktree(&self, orphaned: OrphanedWorktree) {
        warn!(
            "Registered orphaned worktree: {} (agent: {}, item: {})",
            orphaned.path.display(),
            orphaned.agent_id,
            orphaned.item_id
        );
        let mut registry = self.orphaned_worktrees.lock().await;
        registry.push(orphaned);
    }

    /// Execute a complete MapReduce job
    pub async fn execute_job(
        &self,
        setup: Option<SetupPhase>,
        map_phase: MapPhase,
        reduce: Option<ReducePhase>,
        env: &ExecutionEnvironment,
    ) -> MapReduceResult<Vec<AgentResult>> {
        info!("Starting MapReduce job execution");

        // Initialize timeout enforcer if configured
        if let Some(timeout_secs) = map_phase.config.agent_timeout_secs {
            let timeout_config =
                map_phase
                    .timeout_config
                    .clone()
                    .unwrap_or_else(|| TimeoutConfig {
                        agent_timeout_secs: Some(timeout_secs),
                        ..TimeoutConfig::default()
                    });
            let mut enforcer = self.timeout_enforcer.lock().await;
            *enforcer = Some(Arc::new(TimeoutEnforcer::new(timeout_config)));
            info!("Timeout enforcement enabled with {}s timeout", timeout_secs);
        } else if let Some(timeout_config) = map_phase.timeout_config.clone() {
            let mut enforcer = self.timeout_enforcer.lock().await;
            *enforcer = Some(Arc::new(TimeoutEnforcer::new(timeout_config)));
            info!("Timeout enforcement enabled with custom configuration");
        }

        // Check if we're in dry-run mode
        if let crate::cook::execution::mapreduce::dry_run::ExecutionMode::DryRun(ref config) =
            self.execution_mode
        {
            return self
                .execute_dry_run(setup.as_ref(), &map_phase, reduce.as_ref(), config)
                .await;
        }

        // Execute setup phase if present
        if let Some(setup_phase) = setup {
            self.execute_setup_phase(setup_phase, env, &map_phase.workflow_env)
                .await?;
        }

        // Load work items
        let work_items = self.load_work_items(&map_phase).await?;

        if work_items.is_empty() {
            warn!("No work items to process");
            return Ok(Vec::new());
        }

        info!("Processing {} work items", work_items.len());

        // Execute map phase
        let map_results = self
            .execute_map_phase_internal(map_phase, work_items, env)
            .await?;

        // Execute reduce phase if present
        if let Some(reduce_phase) = reduce {
            self.execute_reduce_phase(reduce_phase, &map_results, env)
                .await?;
        }

        // SPEC 134: Merge to original branch is handled by orchestrator cleanup with user confirmation
        // No automatic merge happens here. The orchestrator will prompt the user to merge the parent
        // worktree back to the original branch after the workflow completes.
        tracing::info!("MapReduce reduce phase completed. Changes are in parent worktree.");

        Ok(map_results)
    }

    /// Execute in dry-run mode
    async fn execute_dry_run(
        &self,
        setup: Option<&SetupPhase>,
        map_phase: &MapPhase,
        reduce: Option<&ReducePhase>,
        _config: &crate::cook::execution::mapreduce::dry_run::DryRunConfig,
    ) -> MapReduceResult<Vec<AgentResult>> {
        use crate::cook::execution::mapreduce::dry_run::{DryRunValidator, OutputFormatter};

        info!("Executing MapReduce job in dry-run mode");

        // Create validator
        let validator = DryRunValidator::new();

        // Validate the workflow
        match validator
            .validate_workflow_phases(setup.cloned(), map_phase.clone(), reduce.cloned())
            .await
        {
            Ok(report) => {
                // Display the validation report
                let formatter = OutputFormatter::new();
                let output = formatter.format_human(&report);

                // Use user interaction to display the output
                self.user_interaction.display_info(&output);

                if report.errors.is_empty() {
                    self.user_interaction.display_success(
                        "Dry-run validation successful! Workflow is ready to execute.",
                    );
                    Ok(Vec::new()) // Return empty results for dry-run
                } else {
                    self.user_interaction.display_error(&format!(
                        "Dry-run validation failed with {} error(s)",
                        report.errors.len()
                    ));
                    Err(MapReduceError::General {
                        message: format!(
                            "Dry-run validation failed with {} errors",
                            report.errors.len()
                        ),
                        source: None,
                    })
                }
            }
            Err(e) => {
                self.user_interaction
                    .display_error(&format!("Dry-run validation failed: {}", e));
                Err(MapReduceError::General {
                    message: format!("Dry-run validation failed: {}", e),
                    source: None,
                })
            }
        }
    }

    /// Classify failure reason from error message
    fn classify_failure_reason(
        error: &MapReduceError,
    ) -> crate::cook::execution::mapreduce::event::FailureReason {
        use crate::cook::execution::mapreduce::event::FailureReason;

        let error_msg = error.to_string();
        let msg_lower = error_msg.to_lowercase();

        if msg_lower.contains("commit required") || msg_lower.contains("commit validation") {
            // Extract command from error message if possible
            let command = error_msg
                .lines()
                .find(|line| line.contains("Command:"))
                .and_then(|line| line.split("Command:").nth(1))
                .map(|s| s.trim().to_string())
                .unwrap_or_else(|| "unknown".to_string());
            FailureReason::CommitValidationFailed { command }
        } else if msg_lower.contains("timeout") || msg_lower.contains("timed out") {
            FailureReason::Timeout
        } else if msg_lower.contains("merge") || msg_lower.contains("conflict") {
            FailureReason::MergeConflict
        } else if msg_lower.contains("worktree") {
            FailureReason::WorktreeError
        } else if let Some(exit_code) = Self::extract_exit_code(&error_msg) {
            FailureReason::CommandFailed { exit_code }
        } else {
            FailureReason::Unknown
        }
    }

    /// Extract exit code from error message if present
    fn extract_exit_code(error_msg: &str) -> Option<i32> {
        // Try to find patterns like "exit code: 1" or "exited with code 127"
        let patterns = [
            regex::Regex::new(r"exit code:?\s*(\d+)").ok()?,
            regex::Regex::new(r"exited with code\s*(\d+)").ok()?,
            regex::Regex::new(r"status code:?\s*(\d+)").ok()?,
        ];

        for pattern in &patterns {
            if let Some(captures) = pattern.captures(error_msg) {
                if let Some(code_str) = captures.get(1) {
                    if let Ok(code) = code_str.as_str().parse::<i32>() {
                        return Some(code);
                    }
                }
            }
        }

        None
    }

    /// Get a displayable name for a workflow step
    #[cfg_attr(test, allow(dead_code))]
    pub(crate) fn get_step_display_name(step: &WorkflowStep) -> String {
        CommandExecutor::get_step_display_name(step)
    }

    /// Interpolate a workflow step with environment variables
    fn interpolate_step_with_env(
        &self,
        step: &WorkflowStep,
        workflow_env: &HashMap<String, String>,
    ) -> MapReduceResult<WorkflowStep> {
        use crate::cook::execution::interpolation::{InterpolationContext, InterpolationEngine};

        // Build interpolation context from workflow env
        let mut context = InterpolationContext::new();
        for (key, value) in workflow_env {
            context.set(key.clone(), serde_json::Value::String(value.clone()));
        }

        // Create interpolation engine
        let mut engine = InterpolationEngine::new(false); // non-strict mode

        // Interpolate the step
        let mut interpolated = step.clone();

        if let Some(shell) = &step.shell {
            interpolated.shell = Some(engine.interpolate(shell, &context).map_err(|e| {
                MapReduceError::ProcessingError(format!(
                    "Failed to interpolate shell command: {}",
                    e
                ))
            })?);
        }

        if let Some(claude) = &step.claude {
            interpolated.claude = Some(engine.interpolate(claude, &context).map_err(|e| {
                MapReduceError::ProcessingError(format!(
                    "Failed to interpolate claude command: {}",
                    e
                ))
            })?);
        }

        Ok(interpolated)
    }

    /// Execute the setup phase
    #[cfg_attr(test, allow(dead_code))]
    pub(crate) async fn execute_setup_phase(
        &self,
        setup_phase: SetupPhase,
        env: &ExecutionEnvironment,
        workflow_env: &HashMap<String, String>,
    ) -> MapReduceResult<()> {
        info!("Executing setup phase");
        info!(
            "Setup phase executing in directory: {}",
            env.working_dir.display()
        );

        for (index, step) in setup_phase.commands.iter().enumerate() {
            // Get step display name for logging
            let step_name = Self::get_step_display_name(step);

            // Display user-facing progress with the actual command
            self.user_interaction.display_progress(&format!(
                "Setup [{}/{}]: {}",
                index + 1,
                setup_phase.commands.len(),
                step_name
            ));

            info!(
                "Executing setup step {}/{}: {}",
                index + 1,
                setup_phase.commands.len(),
                step_name
            );

            // Log execution context at DEBUG level
            debug!("=== Step Execution Context ===");
            debug!("Step: {:?}", step);
            debug!("Working Directory: {}", env.working_dir.display());
            debug!("Project Directory: {}", self.project_root.display());
            debug!("Worktree: {:?}", env.worktree_name);
            debug!("Session ID: {}", env.session_id);

            // Set environment variables
            let mut env_vars = HashMap::new();
            env_vars.insert("PRODIGY_AUTOMATION".to_string(), "true".to_string());

            // Add workflow environment variables (includes interpolated positional args)
            for (key, value) in workflow_env {
                env_vars.insert(key.clone(), value.clone());
            }

            debug!("Environment Variables:");
            for (key, value) in &env_vars {
                debug!("  {} = {}", key, value);
            }
            debug!("Actual execution directory: {}", env.working_dir.display());
            debug!("==============================");

            // Interpolate the step with workflow environment variables
            info!("🔍 DEBUG: Original step shell command: {:?}", step.shell);
            info!("🔍 DEBUG: Workflow env vars: {:?}", workflow_env);
            let interpolated_step = self.interpolate_step_with_env(step, workflow_env)?;
            info!(
                "🔍 DEBUG: Interpolated step shell command: {:?}",
                interpolated_step.shell
            );

            // Execute the interpolated step
            let result = self
                .command_executor
                .execute_setup_step(&interpolated_step, env, env_vars)
                .await
                .map_err(|e| {
                    MapReduceError::ProcessingError(format!(
                        "Setup step {} ({}) failed: {}",
                        index + 1,
                        step_name,
                        e
                    ))
                })?;

            // Display completion
            if result.success {
                self.user_interaction.display_success(&format!(
                    "✓ Setup [{}/{}]: {} completed",
                    index + 1,
                    setup_phase.commands.len(),
                    step_name
                ));
            }

            if !result.success {
                // Handle on_failure if configured
                if let Some(on_failure) = &step.on_failure {
                    self.user_interaction.display_warning(&format!(
                        "Setup step {} failed, executing on_failure handler",
                        index + 1
                    ));

                    // Create empty variables for on_failure (setup phase has no item context)
                    let variables = HashMap::new();

                    let handler_result = Self::handle_on_failure(
                        on_failure,
                        &env.working_dir,
                        &variables,
                        &self.command_executor,
                        &self.user_interaction,
                    )
                    .await?;

                    if !handler_result {
                        return Err(MapReduceError::ProcessingError(format!(
                            "Setup step {} failed and on_failure handler failed",
                            index + 1
                        )));
                    }

                    // on_failure handler succeeded, continue to next step
                    continue;
                }

                // No on_failure handler, build error message and fail
                let mut error_msg = format!("Setup step {} ({}) failed", index + 1, step_name);

                // Add exit code if available
                if let Some(code) = result.exit_code {
                    error_msg.push_str(&format!(" (exit code: {})", code));
                }

                // Add stderr if available
                if !result.stderr.trim().is_empty() {
                    error_msg.push_str(&format!("\nstderr: {}", result.stderr.trim()));
                }

                // Add stdout if available and stderr is empty
                if result.stderr.trim().is_empty() && !result.stdout.trim().is_empty() {
                    error_msg.push_str(&format!("\nstdout: {}", result.stdout.trim()));
                }

                // Add Claude JSON log location if available (most direct path to debugging)
                if let Some(json_log) = &result.json_log_location {
                    error_msg.push_str(&format!("\n\n📝 Claude log: {}", json_log));
                } else if step.claude.is_some() {
                    // Fallback: If Claude command but no direct log, show event logs location
                    if let Ok(repo_name) = crate::storage::extract_repo_name(&self.project_root) {
                        let log_hint = format!(
                            "\n\n💡 Check Claude logs at: ~/.prodigy/events/{}/{}/*.jsonl",
                            repo_name, self.job_id
                        );
                        error_msg.push_str(&log_hint);
                    }
                }

                return Err(MapReduceError::ProcessingError(error_msg));
            }
        }

        info!("Setup phase completed");
        Ok(())
    }

    /// Load work items from input source
    async fn load_work_items(&self, map_phase: &MapPhase) -> MapReduceResult<Vec<Value>> {
        info!("Loading work items from: {}", map_phase.config.input);

        // Create input source
        let input_source =
            InputSource::detect_with_base(&map_phase.config.input, &self.project_root);

        // Load data based on input source type
        let json_data = match input_source {
            InputSource::JsonFile(path) => {
                InputSource::load_json_file(&path, &self.project_root).await?
            }
            InputSource::Command(cmd) => {
                let items =
                    InputSource::execute_command(&cmd, Duration::from_secs(300), &self.subprocess)
                        .await?;
                serde_json::Value::Array(items)
            }
        };

        // Create data pipeline from configuration
        let pipeline = DataPipeline::from_config(
            map_phase.json_path.clone(),
            map_phase.filter.clone(),
            map_phase.sort_by.clone(),
            map_phase.max_items,
        )
        .map_err(|e| MapReduceError::InvalidConfiguration {
            reason: format!("Failed to build data pipeline: {}", e),
            field: "configuration".to_string(),
            value: "configuration".to_string(),
        })?;

        // Process the data through the pipeline
        let items =
            pipeline
                .process(&json_data)
                .map_err(|e| MapReduceError::InvalidConfiguration {
                    reason: format!("Failed to process work items: {}", e),
                    field: "input".to_string(),
                    value: map_phase.config.input.clone(),
                })?;

        debug!("Loaded {} work items", items.len());
        Ok(items)
    }

    /// Convert execution result to AgentResult with appropriate event logging
    ///
    /// This helper function handles the conversion of agent execution results,
    /// logging appropriate events (completed or failed) and structuring the
    /// AgentResult consistently.
    async fn convert_execution_result_to_agent_result(
        result: MapReduceResult<AgentResult>,
        agent_id: String,
        item_id: String,
        duration: Duration,
        event_logger: Arc<EventLogger>,
    ) -> MapReduceResult<AgentResult> {
        match result {
            Ok(agent_result) => {
                event_logger
                    .log_event(MapReduceEvent::agent_completed(
                        agent_id.clone(),
                        item_id.clone(),
                        chrono::Duration::from_std(duration)
                            .unwrap_or(chrono::Duration::seconds(0)),
                        None,
                        agent_result.commits.clone(),
                        agent_result.json_log_location.clone(),
                    ))
                    .await
                    .map_err(|e| MapReduceError::ProcessingError(e.to_string()))?;

                Ok(agent_result)
            }
            Err(e) => {
                // Classify failure reason from error message
                let failure_reason = Self::classify_failure_reason(&e);

                event_logger
                    .log_event(MapReduceEvent::agent_failed(
                        agent_id.clone(),
                        item_id.clone(),
                        e.to_string(),
                        failure_reason,
                        None,
                    ))
                    .await
                    .map_err(|e| MapReduceError::ProcessingError(e.to_string()))?;

                Ok(AgentResult {
                    item_id: item_id.clone(),
                    status: AgentStatus::Failed(e.to_string()),
                    output: None,
                    commits: vec![],
                    duration: std::time::Duration::from_secs(0),
                    error: Some(e.to_string()),
                    worktree_path: None,
                    branch_name: None,
                    worktree_session_id: Some(agent_id),
                    files_modified: vec![],
                    json_log_location: None,
                    cleanup_status: None,
                })
            }
        }
    }

    /// Handle Dead Letter Queue integration for failed items
    ///
    /// This helper function manages DLQ operations for failed work items,
    /// including retry tracking, error logging, and state updates.
    async fn handle_dlq_for_failed_item(
        agent_result: &AgentResult,
        item: &Value,
        item_id: &str,
        dlq: Arc<DeadLetterQueue>,
        retry_counts: Arc<tokio::sync::RwLock<HashMap<String, u32>>>,
    ) {
        // Get current attempt number for this item
        let retry_counts_read = retry_counts.read().await;
        let attempt_number = retry_tracking::get_item_attempt_number(item_id, &retry_counts_read);
        drop(retry_counts_read); // Release read lock

        if let Some(dlq_item) =
            dlq_integration::agent_result_to_dlq_item(agent_result, item, attempt_number)
        {
            if let Err(e) = dlq.add(dlq_item).await {
                warn!(
                    "Failed to add item {} to DLQ: {}. Item tracking may be incomplete.",
                    agent_result.item_id, e
                );
            } else {
                info!(
                    "Added failed item {} to DLQ (attempt {})",
                    agent_result.item_id, attempt_number
                );

                // Increment retry count in state
                let mut retry_counts_write = retry_counts.write().await;
                *retry_counts_write =
                    retry_tracking::increment_retry_count(item_id, retry_counts_write.clone());
            }
        }
    }

    /// Process a single work item with complete agent lifecycle management
    ///
    /// This helper function orchestrates the complete execution flow for a single work item,
    /// including semaphore acquisition, agent execution, result conversion, and DLQ integration.
    #[allow(clippy::too_many_arguments)]
    async fn process_single_work_item(
        index: usize,
        item: Value,
        job_id: String,
        map_phase: MapPhase,
        env: ExecutionEnvironment,
        semaphore: Arc<Semaphore>,
        agent_manager: Arc<dyn AgentLifecycleManager>,
        merge_queue: Arc<MergeQueue>,
        event_logger: Arc<EventLogger>,
        result_collector: Arc<ResultCollector>,
        user_interaction: Arc<dyn UserInteraction>,
        command_executor: CommandExecutor,
        dlq: Arc<DeadLetterQueue>,
        retry_counts: Arc<tokio::sync::RwLock<HashMap<String, u32>>>,
        timeout_enforcer: Option<Arc<TimeoutEnforcer>>,
        total_items: usize,
    ) -> MapReduceResult<AgentResult> {
        // Acquire semaphore permit
        let _permit = semaphore.acquire().await.map_err(|e| {
            MapReduceError::ProcessingError(format!("Failed to acquire semaphore: {}", e))
        })?;

        let item_id = format!("item_{}", index);
        let agent_id = format!("{}_agent_{}", job_id, index);

        // Log agent start
        event_logger
            .log_event(MapReduceEvent::agent_started(
                agent_id.clone(),
                item_id.clone(),
            ))
            .await
            .map_err(|e| MapReduceError::ProcessingError(e.to_string()))?;

        let start_time = Instant::now();

        // Clone item for DLQ tracking in case of failure
        let item_for_dlq = item.clone();

        // Execute agent with item
        let result = Self::execute_agent_for_item(
            &agent_manager,
            &merge_queue,
            &agent_id,
            &item_id,
            item,
            &map_phase,
            &env,
            &user_interaction,
            &command_executor,
            timeout_enforcer.as_ref(),
            index,
            total_items,
        )
        .await;

        let duration = start_time.elapsed();

        // Convert result to AgentResult
        let agent_result = Self::convert_execution_result_to_agent_result(
            result,
            agent_id.clone(),
            item_id.clone(),
            duration,
            event_logger.clone(),
        )
        .await?;

        // Add result to collector
        result_collector.add_result(agent_result.clone()).await;

        // Add failed items to DLQ (graceful failure - don't break workflow)
        Self::handle_dlq_for_failed_item(
            &agent_result,
            &item_for_dlq,
            &item_id,
            dlq.clone(),
            retry_counts.clone(),
        )
        .await;

        Ok(agent_result)
    }

    /// Collect results from agent futures, handling errors gracefully
    ///
    /// This helper function waits for all agent futures to complete,
    /// collecting successful results and logging failures without breaking the workflow.
    async fn collect_agent_results(
        agent_futures: Vec<tokio::task::JoinHandle<MapReduceResult<AgentResult>>>,
    ) -> Vec<AgentResult> {
        let mut results = Vec::new();
        for future in agent_futures {
            match future.await {
                Ok(Ok(result)) => results.push(result),
                Ok(Err(e)) => {
                    warn!("Agent execution failed: {}", e);
                    // Continue processing other agents
                }
                Err(e) => {
                    warn!("Agent task panicked: {}", e);
                    // Continue processing other agents
                }
            }
        }
        results
    }

    /// Execute the map phase with parallel work item processing
    ///
    /// This orchestrates the map phase execution flow:
    /// 1. Initialize semaphore for concurrency control
    /// 2. Spawn parallel agents for each work item via `process_single_work_item`
    /// 3. Collect and aggregate results via `collect_agent_results`
    /// 4. Log summary metrics
    ///
    /// The function has been refactored to reduce complexity by extracting:
    /// - Result conversion logic to `convert_execution_result_to_agent_result`
    /// - DLQ integration to `handle_dlq_for_failed_item`
    /// - Single item processing to `process_single_work_item`
    /// - Future collection to `collect_agent_results`
    async fn execute_map_phase_internal(
        &self,
        map_phase: MapPhase,
        work_items: Vec<Value>,
        env: &ExecutionEnvironment,
    ) -> MapReduceResult<Vec<AgentResult>> {
        info!("Executing map phase with {} items", work_items.len());

        let total_items = work_items.len();
        let max_parallel = map_phase.config.max_parallel.min(total_items);

        self.user_interaction.display_progress(&format!(
            "Processing {} items with {} parallel agents",
            total_items, max_parallel
        ));

        // Log map phase start
        self.event_logger
            .log_event(MapReduceEvent::map_phase_started(total_items))
            .await
            .map_err(|e| MapReduceError::ProcessingError(e.to_string()))?;

        // Create semaphore for parallel control
        let semaphore = Arc::new(Semaphore::new(max_parallel));

        // Get the timeout enforcer if configured
        let timeout_enforcer = self.timeout_enforcer.lock().await.clone();

        // Spawn parallel agents for each work item
        let agent_futures: Vec<_> = work_items
            .into_iter()
            .enumerate()
            .map(|(index, item)| {
                let sem = Arc::clone(&semaphore);
                let agent_manager = Arc::clone(&self.agent_manager);
                let merge_queue = Arc::clone(&self.merge_queue);
                let event_logger = Arc::clone(&self.event_logger);
                let result_collector = Arc::clone(&self.result_collector);
                let user_interaction = Arc::clone(&self.user_interaction);
                let command_executor = self.command_executor.clone();
                let dlq = Arc::clone(&self.dlq);
                let retry_counts = Arc::clone(&self.retry_counts);
                let map_phase = map_phase.clone();
                let env = env.clone();
                let job_id = self.job_id.clone();
                let timeout_enforcer = timeout_enforcer.clone();

                tokio::spawn(Self::process_single_work_item(
                    index,
                    item,
                    job_id,
                    map_phase,
                    env,
                    sem,
                    agent_manager,
                    merge_queue,
                    event_logger,
                    result_collector,
                    user_interaction,
                    command_executor,
                    dlq,
                    retry_counts,
                    timeout_enforcer,
                    total_items,
                ))
            })
            .collect();

        // Wait for all agents to complete
        let results = Self::collect_agent_results(agent_futures).await;

        // Log map phase completion
        let summary = AggregationSummary::from_results(&results);
        self.event_logger
            .log_event(MapReduceEvent::map_phase_completed(
                summary.successful,
                summary.failed,
            ))
            .await
            .map_err(|e| MapReduceError::ProcessingError(e.to_string()))?;

        self.display_map_summary(&summary);

        Ok(results)
    }

    /// Register agent timeout with the enforcer
    ///
    /// Attempts to register a timeout for the agent with the enforcer.
    /// Returns None if enforcer is not available or registration fails.
    ///
    /// # Arguments
    /// * `enforcer` - Optional timeout enforcer
    /// * `agent_id` - ID of the agent to register
    /// * `item_id` - ID of the work item being processed
    /// * `commands` - Commands that will be executed by the agent
    ///
    /// # Returns
    /// Optional timeout handle if registration succeeds
    async fn register_agent_timeout(
        enforcer: Option<&Arc<TimeoutEnforcer>>,
        agent_id: &str,
        item_id: &str,
        commands: &[WorkflowStep],
    ) -> Option<crate::cook::execution::mapreduce::timeout::TimeoutHandle> {
        if let Some(enforcer) = enforcer {
            match enforcer
                .register_agent_timeout(agent_id.to_string(), item_id.to_string(), commands)
                .await
            {
                Ok(handle) => Some(handle),
                Err(e) => {
                    warn!("Failed to register timeout for agent {}: {}", agent_id, e);
                    None
                }
            }
        } else {
            None
        }
    }

    /// Register command lifecycle events with the enforcer
    ///
    /// Notifies the timeout enforcer of command start or completion.
    /// Logs warnings if notification fails but doesn't propagate errors.
    ///
    /// # Arguments
    /// * `enforcer` - Optional timeout enforcer
    /// * `agent_id` - ID of the agent executing the command
    /// * `index` - Index of the command in the workflow
    /// * `elapsed` - Optional elapsed time (for completion events)
    async fn register_command_lifecycle(
        enforcer: Option<&Arc<TimeoutEnforcer>>,
        agent_id: &str,
        index: usize,
        elapsed: Option<Duration>,
    ) -> MapReduceResult<()> {
        if let Some(enforcer) = enforcer {
            if let Some(duration) = elapsed {
                // Command completion
                let _ = enforcer
                    .register_command_completion(&agent_id.to_string(), index, duration)
                    .await;
            } else {
                // Command start
                let _ = enforcer
                    .register_command_start(&agent_id.to_string(), index)
                    .await;
            }
        }
        Ok(())
    }

    /// Unregister agent timeout with the enforcer
    ///
    /// Removes the timeout registration for a completed agent.
    ///
    /// # Arguments
    /// * `enforcer` - Optional timeout enforcer
    /// * `agent_id` - ID of the agent to unregister
    async fn unregister_agent_timeout(
        enforcer: Option<&Arc<TimeoutEnforcer>>,
        agent_id: &str,
    ) -> MapReduceResult<()> {
        if let Some(enforcer) = enforcer {
            let _ = enforcer
                .unregister_agent_timeout(&agent_id.to_string())
                .await;
        }
        Ok(())
    }

    /// Merge agent changes and cleanup worktree
    ///
    /// Handles merging agent changes back to parent worktree and cleaning up
    /// the agent's worktree. Returns whether merge was successful.
    ///
    /// # Arguments
    /// * `agent_manager` - Agent lifecycle manager
    /// * `merge_queue` - Merge queue for serialized merges
    /// * `handle` - Agent handle with worktree information
    /// * `config` - Agent configuration
    /// * `agent_result` - Result from agent execution
    /// * `env` - Execution environment
    /// * `agent_id` - ID of the agent
    /// * `item_id` - ID of the work item
    ///
    /// # Returns
    /// True if merge was successful, false otherwise
    #[allow(clippy::too_many_arguments)]
    async fn merge_and_cleanup_agent(
        agent_manager: &Arc<dyn AgentLifecycleManager>,
        merge_queue: &Arc<MergeQueue>,
        handle: crate::cook::execution::mapreduce::agent::AgentHandle,
        config: &AgentConfig,
        agent_result: &AgentResult,
        env: &ExecutionEnvironment,
        agent_id: &str,
        item_id: &str,
    ) -> MapReduceResult<bool> {
        if !agent_result.commits.is_empty() {
            // Create branch for the agent
            agent_manager
                .create_agent_branch(handle.worktree_path(), &config.branch_name)
                .await
                .map_err(|e| {
                    MapReduceError::ProcessingError(format!("Failed to create agent branch: {}", e))
                })?;

            // Submit merge to queue (serialized processing)
            match merge_queue
                .submit_merge(
                    agent_id.to_string(),
                    config.branch_name.clone(),
                    item_id.to_string(),
                    env.clone(),
                )
                .await
            {
                Ok(()) => {
                    info!("Successfully merged agent {} (item {})", agent_id, item_id);
                    // Cleanup the worktree after successful merge
                    // Note: Cleanup failures are non-critical since the work was successfully merged
                    if let Err(e) = agent_manager.cleanup_agent(handle).await {
                        warn!(
                            "Failed to cleanup agent {} after successful merge: {}. \
                             Work was successfully merged, worktree may need manual cleanup.",
                            agent_id, e
                        );
                    }
                    Ok(true)
                }
                Err(e) => {
                    warn!(
                        "Failed to merge agent {} (item {}): {}",
                        agent_id, item_id, e
                    );
                    // Still cleanup the worktree even if merge failed
                    let _ = agent_manager.cleanup_agent(handle).await;
                    Ok(false)
                }
            }
        } else {
            // No commits, skip merge and just cleanup the worktree
            info!(
                "Agent {} (item {}) completed with no commits, skipping merge",
                agent_id, item_id
            );
            // Note: Cleanup failures are non-critical, log but don't fail the agent
            if let Err(e) = agent_manager.cleanup_agent(handle).await {
                warn!(
                    "Failed to cleanup agent {} (no commits made): {}. \
                     Worktree may need manual cleanup.",
                    agent_id, e
                );
            }
            Ok(false)
        }
    }

    /// Execute agent commands in the worktree
    ///
    /// Executes all commands for an agent, handling failures and collecting results.
    ///
    /// # Arguments
    /// * `handle` - Agent handle with worktree information
    /// * `commands` - Commands to execute
    /// * `item` - Work item data
    /// * `item_id` - ID of the work item
    /// * `agent_id` - ID of the agent
    /// * `env` - Execution environment
    /// * `command_executor` - Command executor for running steps
    /// * `timeout_enforcer` - Optional timeout enforcer
    /// * `user_interaction` - User interaction handler
    ///
    /// # Returns
    /// Tuple of (output, commits, files_modified)
    #[allow(clippy::too_many_arguments)]
    async fn execute_agent_commands(
        handle: &crate::cook::execution::mapreduce::agent::AgentHandle,
        commands: &[WorkflowStep],
        item: &Value,
        item_id: &str,
        agent_id: &str,
        _env: &ExecutionEnvironment,
        command_executor: &CommandExecutor,
        timeout_enforcer: Option<&Arc<TimeoutEnforcer>>,
        user_interaction: &Arc<dyn UserInteraction>,
        workflow_env: &HashMap<String, String>,
    ) -> MapReduceResult<(String, Vec<String>, Vec<String>)> {
        let mut output = String::new();
        let mut all_commits = Vec::new();
        let mut all_files_modified = Vec::new();

        for (index, step) in commands.iter().enumerate() {
            user_interaction.display_progress(&format!(
                "Agent {}: Executing step {}/{}",
                agent_id,
                index + 1,
                commands.len()
            ));

            // Notify timeout enforcer of command start
            Self::register_command_lifecycle(timeout_enforcer, agent_id, index, None).await?;

            let cmd_start = Instant::now();

            // Create variables for interpolation (includes workflow env)
            let variables = Self::build_item_variables(item, item_id, workflow_env);

            // Execute the step in the agent's worktree
            let step_result = command_executor
                .execute_step_in_worktree(
                    handle.worktree_path(),
                    step,
                    &variables,
                    None, // Map phase doesn't need full context
                )
                .await?;

            // Notify timeout enforcer of command completion
            Self::register_command_lifecycle(
                timeout_enforcer,
                agent_id,
                index,
                Some(cmd_start.elapsed()),
            )
            .await?;

            if !step_result.success {
                // Handle on_failure if configured
                if let Some(on_failure) = &step.on_failure {
                    user_interaction.display_warning(&format!(
                        "Agent {}: Step {} failed, executing on_failure handler",
                        agent_id,
                        index + 1
                    ));

                    // Rebuild variables for on_failure handler (should include workflow env)
                    let failure_variables = Self::build_item_variables(item, item_id, workflow_env);

                    // Execute on_failure handler
                    let handler_result = Self::handle_on_failure(
                        on_failure,
                        handle.worktree_path(),
                        &failure_variables,
                        command_executor,
                        user_interaction,
                    )
                    .await?;

                    if !handler_result {
                        return Err(MapReduceError::ProcessingError(format!(
                            "Agent {} step {} failed and on_failure handler failed",
                            agent_id,
                            index + 1
                        )));
                    }
                }
            }

            // Capture output
            if !step_result.stdout.is_empty() {
                output.push_str(&step_result.stdout);
                output.push('\n');
            }

            // Track commits if required
            if step.commit_required {
                // Get actual commits from git in the worktree
                let commits = Self::get_worktree_commits(handle.worktree_path()).await;
                all_commits.extend(commits);

                // Get files modified
                let files = Self::get_worktree_modified_files(handle.worktree_path()).await;
                all_files_modified.extend(files);
            }
        }

        Ok((output, all_commits, all_files_modified))
    }

    /// Build variables for item interpolation
    ///
    /// Extracts fields from a JSON item and creates a HashMap of variables
    /// for template interpolation. Handles different JSON value types and
    /// provides both flattened field access (item.field) and full JSON (item_json).
    ///
    /// # Arguments
    /// * `item` - The JSON item to extract variables from
    /// * `item_id` - The ID of the item being processed
    ///
    /// # Returns
    /// HashMap of variable names to string values for interpolation
    fn build_item_variables(
        item: &Value,
        item_id: &str,
        workflow_env: &HashMap<String, String>,
    ) -> HashMap<String, String> {
        let mut variables = HashMap::new();

        // Log workflow environment being used (debug level)
        debug!(
            item_id = %item_id,
            workflow_env = ?workflow_env,
            "Building agent environment from workflow env"
        );

        // Start with workflow environment variables (lowest precedence)
        variables.extend(workflow_env.clone());

        // Store the item fields as flattened variables for interpolation (override workflow env)
        if let Value::Object(map) = item {
            for (key, value) in map {
                let var_key = format!("item.{}", key);
                let var_value = match value {
                    Value::String(s) => s.clone(),
                    Value::Number(n) => n.to_string(),
                    Value::Bool(b) => b.to_string(),
                    Value::Null => "null".to_string(),
                    _ => value.to_string(),
                };
                variables.insert(var_key, var_value);
            }
        }

        // Also store the entire item as JSON for complex cases
        variables.insert(
            "item_json".to_string(),
            serde_json::to_string(item).unwrap_or_default(),
        );
        variables.insert("item_id".to_string(), item_id.to_string());

        // Log final variable set for agent (debug level)
        debug!(
            item_id = %item_id,
            variables = ?variables,
            "Agent environment variables built"
        );

        variables
    }

    /// Execute a single agent for a work item
    #[allow(clippy::too_many_arguments)]
    async fn execute_agent_for_item(
        agent_manager: &Arc<dyn AgentLifecycleManager>,
        merge_queue: &Arc<MergeQueue>,
        agent_id: &str,
        item_id: &str,
        item: Value,
        map_phase: &MapPhase,
        env: &ExecutionEnvironment,
        user_interaction: &Arc<dyn UserInteraction>,
        command_executor: &CommandExecutor,
        timeout_enforcer: Option<&Arc<TimeoutEnforcer>>,
        agent_index: usize,
        total_items: usize,
    ) -> MapReduceResult<AgentResult> {
        info!("Starting agent {} for item {}", agent_id, item_id);

        // Create agent config
        let config = AgentConfig {
            id: agent_id.to_string(),
            item_id: item_id.to_string(),
            branch_name: format!("agent-{}-{}", agent_id, item_id),
            max_retries: 3,
            timeout: Duration::from_secs(600),
            agent_index,
            total_items,
        };

        // Convert agent template to WorkflowSteps
        let commands = map_phase.agent_template.clone();

        // Create the agent with its worktree
        let handle = agent_manager
            .create_agent(config.clone(), commands.clone())
            .await
            .map_err(|e| {
                MapReduceError::ProcessingError(format!("Failed to create agent: {}", e))
            })?;

        // Register timeout if enforcer is available
        let _timeout_handle =
            Self::register_agent_timeout(timeout_enforcer, agent_id, item_id, &commands).await;

        // Execute commands with timeout monitoring
        let agent_result = {
            let start_time = Instant::now();

            // Execute all commands
            let (output, all_commits, all_files_modified) = Self::execute_agent_commands(
                &handle,
                &commands,
                &item,
                item_id,
                agent_id,
                env,
                command_executor,
                timeout_enforcer,
                user_interaction,
                &map_phase.workflow_env,
            )
            .await?;

            // Calculate total duration
            let total_duration = start_time.elapsed();

            // Build the result
            AgentResult {
                item_id: item_id.to_string(),
                status: AgentStatus::Success,
                output: Some(output),
                commits: all_commits.clone(),
                duration: total_duration,
                error: None,
                worktree_path: Some(handle.worktree_path().to_path_buf()),
                branch_name: Some(handle.worktree_session.branch.clone()),
                worktree_session_id: Some(agent_id.to_string()),
                files_modified: all_files_modified.clone(),
                json_log_location: None,
                cleanup_status: None,
            }
        };

        // Unregister timeout (agent completed)
        Self::unregister_agent_timeout(timeout_enforcer, agent_id).await?;

        // Merge and cleanup agent if successful
        let merge_successful = Self::merge_and_cleanup_agent(
            agent_manager,
            merge_queue,
            handle,
            &config,
            &agent_result,
            env,
            agent_id,
            item_id,
        )
        .await?;

        // Update agent status based on merge outcome
        if !merge_successful && !agent_result.commits.is_empty() {
            warn!("Agent {} completed successfully but merge failed", agent_id);
            // Mark agent as failed since merge is part of successful completion
            let mut final_result = agent_result;
            final_result.status = AgentStatus::Failed(
                "Agent execution succeeded but merge to parent worktree failed".to_string(),
            );
            final_result.error =
                Some("Merge to parent worktree failed - changes not integrated".to_string());
            return Ok(final_result);
        }

        Ok(agent_result)
    }

    /// Handle on_failure configuration
    #[cfg_attr(test, allow(dead_code))]
    pub(crate) async fn handle_on_failure(
        on_failure: &OnFailureConfig,
        worktree_path: &Path,
        variables: &HashMap<String, String>,
        command_executor: &CommandExecutor,
        user_interaction: &Arc<dyn UserInteraction>,
    ) -> MapReduceResult<bool> {
        // Extract commands based on OnFailureConfig variant
        let (claude_cmd, shell_cmd) = match on_failure {
            OnFailureConfig::Advanced { claude, shell, .. } => {
                (claude.as_deref(), shell.as_deref())
            }
            OnFailureConfig::SingleCommand(cmd) => {
                if cmd.starts_with("/") {
                    (Some(cmd.as_str()), None)
                } else {
                    (None, Some(cmd.as_str()))
                }
            }
            _ => (None, None),
        };

        // Execute Claude command if present
        if let Some(cmd) = claude_cmd {
            user_interaction
                .display_progress(&format!("on_failure: Executing Claude command: {}", cmd));
            info!("Executing on_failure Claude command: {}", cmd);

            let step = WorkflowStep {
                claude: Some(cmd.to_string()),
                ..Default::default()
            };

            let result = command_executor
                .execute_step_in_worktree(worktree_path, &step, variables, None)
                .await?;

            return Ok(result.success);
        }

        // Execute shell command if present
        if let Some(cmd) = shell_cmd {
            user_interaction
                .display_progress(&format!("on_failure: Executing shell command: {}", cmd));
            info!("Executing on_failure shell command: {}", cmd);

            let step = WorkflowStep {
                shell: Some(cmd.to_string()),
                ..Default::default()
            };

            let result = command_executor
                .execute_step_in_worktree(worktree_path, &step, variables, None)
                .await?;

            return Ok(result.success);
        }

        // No handler to execute
        Ok(true)
    }

    /// Get commits from a worktree
    async fn get_worktree_commits(worktree_path: &Path) -> Vec<String> {
        use crate::cook::execution::mapreduce::resources::git_operations::{
            GitOperationsConfig, GitOperationsService, GitResultExt,
        };

        let mut service = GitOperationsService::new(GitOperationsConfig::default());
        match service
            .get_worktree_commits(worktree_path, None, None)
            .await
        {
            Ok(commits) => commits.to_string_list(),
            Err(e) => {
                warn!("Failed to get worktree commits: {}", e);
                vec![]
            }
        }
    }

    /// Get modified files from a worktree
    async fn get_worktree_modified_files(worktree_path: &Path) -> Vec<String> {
        use crate::cook::execution::mapreduce::resources::git_operations::{
            GitOperationsConfig, GitOperationsService, GitResultExt,
        };

        let mut service = GitOperationsService::new(GitOperationsConfig::default());
        match service
            .get_worktree_modified_files(worktree_path, None)
            .await
        {
            Ok(files) => files.to_string_list(),
            Err(e) => {
                warn!("Failed to get modified files: {}", e);
                vec![]
            }
        }
    }

    /// Build full interpolation context for reduce phase
    ///
    /// Creates an InterpolationContext with all reduce phase variables including:
    /// - Scalar summary values (map.successful, map.failed, map.total)
    /// - Full map.results array (for write_file commands)
    #[cfg_attr(test, allow(dead_code))]
    pub(crate) fn build_reduce_interpolation_context(
        map_results: &[AgentResult],
        summary: &AggregationSummary,
    ) -> MapReduceResult<crate::cook::execution::interpolation::InterpolationContext> {
        use crate::cook::execution::interpolation::InterpolationContext;

        let mut context = InterpolationContext::new();

        // Add scalar summary values
        context.set("map.successful", serde_json::json!(summary.successful));
        context.set("map.failed", serde_json::json!(summary.failed));
        context.set("map.total", serde_json::json!(summary.total));

        // Add full results as JSON value (for write_file interpolation)
        // This can be >1MB with many agents, so it's excluded from env vars
        // but available for interpolation in write_file commands
        let results_value = serde_json::to_value(map_results).map_err(|e| {
            MapReduceError::ProcessingError(format!("Failed to serialize map results: {}", e))
        })?;
        context.set("map.results", results_value);

        Ok(context)
    }

    /// Execute the reduce phase
    async fn execute_reduce_phase(
        &self,
        reduce: ReducePhase,
        map_results: &[AgentResult],
        env: &ExecutionEnvironment,
    ) -> MapReduceResult<()> {
        info!("Executing reduce phase");

        self.user_interaction
            .display_progress("Starting reduce phase...");

        let summary = AggregationSummary::from_results(map_results);
        self.display_reduce_summary(&summary);

        // Log reduce phase start
        self.event_logger
            .log_event(MapReduceEvent::reduce_phase_started())
            .await
            .map_err(|e| MapReduceError::ProcessingError(e.to_string()))?;

        // Create LIMITED variables for environment (prevent E2BIG errors)
        // map.results excluded because it can be >1MB with many agents
        let mut variables = HashMap::new();
        variables.insert("map.successful".to_string(), summary.successful.to_string());
        variables.insert("map.failed".to_string(), summary.failed.to_string());
        variables.insert("map.total".to_string(), summary.total.to_string());

        // Create FULL interpolation context for write_file commands
        // Includes map.results since interpolation doesn't use env vars
        let full_context = Self::build_reduce_interpolation_context(map_results, &summary)?;

        // Execute reduce commands
        for (index, step) in reduce.commands.iter().enumerate() {
            self.user_interaction.display_progress(&format!(
                "Reduce phase: Executing step {}/{}",
                index + 1,
                reduce.commands.len()
            ));

            let step_result = self
                .command_executor
                .execute_step_in_worktree(
                    &env.working_dir,
                    step,
                    &variables,
                    Some(&full_context), // Reduce phase provides full context
                )
                .await?;

            if !step_result.success {
                // Handle on_failure if configured
                if let Some(on_failure) = &step.on_failure {
                    self.user_interaction.display_warning(&format!(
                        "Reduce step {} failed, executing on_failure handler",
                        index + 1
                    ));

                    let handler_result = Self::handle_on_failure(
                        on_failure,
                        &env.working_dir,
                        &variables,
                        &self.command_executor,
                        &self.user_interaction,
                    )
                    .await?;

                    if !handler_result {
                        return Err(MapReduceError::ProcessingError(format!(
                            "Reduce step {} failed and on_failure handler failed",
                            index + 1
                        )));
                    }
                }
            }
        }

        // Log reduce phase completion
        self.event_logger
            .log_event(MapReduceEvent::reduce_phase_completed())
            .await
            .map_err(|e| MapReduceError::ProcessingError(e.to_string()))?;

        self.user_interaction
            .display_success("Reduce phase completed");
        Ok(())
    }

    /// Display map phase summary
    fn display_map_summary(&self, summary: &AggregationSummary) {
        let message = format!(
            "Map phase completed: {} successful, {} failed (total: {})",
            summary.successful, summary.failed, summary.total
        );

        if summary.failed > 0 {
            self.user_interaction.display_warning(&message);
        } else {
            self.user_interaction.display_success(&message);
        }
    }

    /// Display reduce phase summary
    fn display_reduce_summary(&self, summary: &AggregationSummary) {
        self.user_interaction.display_info(&format!(
            "Reduce phase input: {} items ({} successful, {} failed)",
            summary.total, summary.successful, summary.failed
        ));
    }

    /// Get collected results
    pub async fn get_results(&self) -> Vec<AgentResult> {
        self.result_collector.get_results().await
    }

    /// Clear collected results
    pub async fn clear_results(&self) {
        self.result_collector.clear().await;
    }
}

// Dummy session manager
struct DummySessionManager;

#[async_trait::async_trait]
impl SessionManager for DummySessionManager {
    async fn start_session(&self, _session_id: &str) -> anyhow::Result<()> {
        Ok(())
    }

    async fn update_session(
        &self,
        _update: crate::cook::session::SessionUpdate,
    ) -> anyhow::Result<()> {
        Ok(())
    }

    async fn complete_session(&self) -> anyhow::Result<crate::cook::session::SessionSummary> {
        Ok(crate::cook::session::SessionSummary {
            iterations: 0,
            files_changed: 0,
        })
    }

    fn get_state(&self) -> anyhow::Result<crate::cook::session::SessionState> {
        Ok(crate::cook::session::SessionState::new(
            "dummy".to_string(),
            std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
        ))
    }

    async fn save_state(&self, _path: &Path) -> anyhow::Result<()> {
        Ok(())
    }

    async fn load_state(&self, _path: &Path) -> anyhow::Result<()> {
        Ok(())
    }

    async fn load_session(
        &self,
        _session_id: &str,
    ) -> anyhow::Result<crate::cook::session::SessionState> {
        Ok(crate::cook::session::SessionState::new(
            "dummy".to_string(),
            std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
        ))
    }

    async fn save_checkpoint(
        &self,
        _state: &crate::cook::session::SessionState,
    ) -> anyhow::Result<()> {
        Ok(())
    }

    async fn list_resumable(&self) -> anyhow::Result<Vec<crate::cook::session::SessionInfo>> {
        Ok(vec![])
    }

    async fn get_last_interrupted(&self) -> anyhow::Result<Option<String>> {
        Ok(None)
    }
}

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

    #[test]
    fn test_build_item_variables_with_simple_types() {
        let item = json!({
            "name": "test-item",
            "count": 42,
            "enabled": true,
            "optional": null
        });

        let workflow_env = HashMap::new();
        let variables =
            MapReduceCoordinator::build_item_variables(&item, "item-123", &workflow_env);

        assert_eq!(variables.get("item.name"), Some(&"test-item".to_string()));
        assert_eq!(variables.get("item.count"), Some(&"42".to_string()));
        assert_eq!(variables.get("item.enabled"), Some(&"true".to_string()));
        assert_eq!(variables.get("item.optional"), Some(&"null".to_string()));
        assert_eq!(variables.get("item_id"), Some(&"item-123".to_string()));
        assert!(variables.contains_key("item_json"));
    }

    #[test]
    fn test_build_item_variables_with_nested_objects() {
        let item = json!({
            "name": "test",
            "metadata": {
                "key": "value"
            }
        });

        let workflow_env = HashMap::new();
        let variables =
            MapReduceCoordinator::build_item_variables(&item, "item-456", &workflow_env);

        assert_eq!(variables.get("item.name"), Some(&"test".to_string()));
        // Nested objects should be serialized to JSON
        assert!(variables.get("item.metadata").unwrap().contains("key"));
        assert_eq!(variables.get("item_id"), Some(&"item-456".to_string()));
    }

    #[test]
    fn test_build_item_variables_with_empty_object() {
        let item = json!({});

        let workflow_env = HashMap::new();
        let variables =
            MapReduceCoordinator::build_item_variables(&item, "item-789", &workflow_env);

        // Should still have item_json and item_id
        assert_eq!(variables.get("item_id"), Some(&"item-789".to_string()));
        assert_eq!(variables.get("item_json"), Some(&"{}".to_string()));
        assert_eq!(variables.len(), 2); // Only item_id and item_json
    }

    #[test]
    fn test_build_item_variables_with_non_object() {
        let item = json!("just a string");

        let workflow_env = HashMap::new();
        let variables =
            MapReduceCoordinator::build_item_variables(&item, "item-999", &workflow_env);

        // Should still have item_json and item_id
        assert_eq!(variables.get("item_id"), Some(&"item-999".to_string()));
        assert_eq!(
            variables.get("item_json"),
            Some(&"\"just a string\"".to_string())
        );
        assert_eq!(variables.len(), 2); // Only item_id and item_json
    }
}