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
//! Git commit tracking and verification
//!
//! This module provides comprehensive commit tracking functionality for workflows,
//! including automatic commit creation, metadata collection, and verification.

use crate::abstractions::GitOperations;
use anyhow::{anyhow, Result};
use chrono::{DateTime, Utc};
use glob::Pattern;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Configuration for commit creation and tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommitConfig {
    /// Commit message template
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message_template: Option<String>,

    /// Commit message validation regex
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message_pattern: Option<String>,

    /// Whether to sign commits
    #[serde(default)]
    pub sign: bool,

    /// Author override
    #[serde(skip_serializing_if = "Option::is_none")]
    pub author: Option<String>,

    /// Files to include (glob patterns)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_files: Option<Vec<String>>,

    /// Files to exclude (glob patterns)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exclude_files: Option<Vec<String>>,

    /// Squash commits at end of workflow
    #[serde(default)]
    pub squash: bool,
}

/// Metadata for a tracked commit
#[derive(Debug, Clone, Serialize)]
pub struct TrackedCommit {
    /// The commit hash
    pub hash: String,

    /// The commit message
    pub message: String,

    /// The commit author
    pub author: String,

    /// The commit timestamp
    pub timestamp: DateTime<Utc>,

    /// Files changed in this commit
    pub files_changed: Vec<PathBuf>,

    /// Number of insertions
    pub insertions: usize,

    /// Number of deletions
    pub deletions: usize,

    /// The step name that created this commit
    pub step_name: String,

    /// The agent ID if this was created by a MapReduce agent
    pub agent_id: Option<String>,
}

/// Strategy for staging files before commit
#[derive(Debug, Clone, PartialEq)]
enum StagingStrategy {
    /// Stage all changes
    All,
    /// Stage only files matching the patterns
    Selective,
}

/// Tracks commits created during workflow execution
pub struct CommitTracker {
    /// Git operations interface
    git_ops: Arc<dyn GitOperations>,

    /// Working directory for git operations
    working_dir: PathBuf,

    /// Initial HEAD commit when tracking started
    initial_head: Option<String>,

    /// All tracked commits
    tracked_commits: Arc<RwLock<Vec<TrackedCommit>>>,
}

impl CommitTracker {
    /// Create a new commit tracker
    pub fn new(git_ops: Arc<dyn GitOperations>, working_dir: PathBuf) -> Self {
        Self {
            git_ops,
            working_dir,
            initial_head: None,
            tracked_commits: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Initialize tracking by recording the current HEAD
    pub async fn initialize(&mut self) -> Result<()> {
        let output = self
            .git_ops
            .git_command_in_dir(&["rev-parse", "HEAD"], "get HEAD", &self.working_dir)
            .await?;

        self.initial_head = Some(String::from_utf8_lossy(&output.stdout).trim().to_string());
        Ok(())
    }

    /// Get the current HEAD commit
    pub async fn get_current_head(&self) -> Result<String> {
        let output = self
            .git_ops
            .git_command_in_dir(&["rev-parse", "HEAD"], "get HEAD", &self.working_dir)
            .await?;

        Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
    }

    /// Check if there are uncommitted changes
    pub async fn has_changes(&self) -> Result<bool> {
        let output = self
            .git_ops
            .git_command_in_dir(
                &["status", "--porcelain"],
                "check status",
                &self.working_dir,
            )
            .await?;

        Ok(!output.stdout.is_empty())
    }

    /// Get commits between two refs
    pub async fn get_commits_between(&self, from: &str, to: &str) -> Result<Vec<TrackedCommit>> {
        let output = self
            .git_ops
            .git_command_in_dir(
                &[
                    "log",
                    &format!("{from}..{to}"),
                    "--pretty=format:%H|%s|%an|%aI",
                    "--name-only",
                ],
                "get commit log",
                &self.working_dir,
            )
            .await?;

        let stdout = String::from_utf8_lossy(&output.stdout);
        let mut commits = Vec::new();
        let mut current_commit: Option<TrackedCommit> = None;

        for line in stdout.lines() {
            if line.contains('|') {
                // This is a commit header line
                if let Some(commit) = current_commit.take() {
                    commits.push(commit);
                }

                let parts: Vec<&str> = line.split('|').collect();
                if parts.len() >= 4 {
                    current_commit = Some(TrackedCommit {
                        hash: parts[0].to_string(),
                        message: parts[1].to_string(),
                        author: parts[2].to_string(),
                        timestamp: parts[3]
                            .parse::<DateTime<Utc>>()
                            .unwrap_or_else(|_| Utc::now()),
                        files_changed: Vec::new(),
                        insertions: 0,
                        deletions: 0,
                        step_name: String::new(),
                        agent_id: None,
                    });
                }
            } else if !line.is_empty() {
                // This is a file name
                if let Some(ref mut commit) = current_commit {
                    commit.files_changed.push(PathBuf::from(line));
                }
            }
        }

        if let Some(commit) = current_commit {
            commits.push(commit);
        }

        // Get diff stats for each commit
        for commit in &mut commits {
            if let Ok(output) = self
                .git_ops
                .git_command_in_dir(
                    &[
                        "diff",
                        "--shortstat",
                        &format!("{}^", commit.hash),
                        &commit.hash,
                    ],
                    "get diff stats",
                    &self.working_dir,
                )
                .await
            {
                let stats = String::from_utf8_lossy(&output.stdout);
                // Parse stats like "2 files changed, 10 insertions(+), 3 deletions(-)"
                if let Some(insertions) = stats
                    .split_whitespace()
                    .position(|w| w.starts_with("insertions(+)") || w.starts_with("insertion(+)"))
                    .and_then(|i| stats.split_whitespace().nth(i.saturating_sub(1)))
                    .and_then(|s| s.parse::<usize>().ok())
                {
                    commit.insertions = insertions;
                }

                if let Some(deletions) = stats
                    .split_whitespace()
                    .position(|w| w.starts_with("deletions(-)") || w.starts_with("deletion(-)"))
                    .and_then(|i| stats.split_whitespace().nth(i.saturating_sub(1)))
                    .and_then(|s| s.parse::<usize>().ok())
                {
                    commit.deletions = deletions;
                }
            }
        }

        Ok(commits)
    }

    /// Check if GPG signing is properly configured
    async fn check_gpg_config(&self) -> Result<bool> {
        // Check if GPG signing is configured in git
        let output = self
            .git_ops
            .git_command_in_dir(
                &["config", "--get", "commit.gpgsign"],
                "check GPG config",
                &self.working_dir,
            )
            .await
            .ok();

        if let Some(output) = output {
            let stdout = String::from_utf8_lossy(&output.stdout);
            if stdout.trim() == "true" {
                // Check if a signing key is configured
                let key_output = self
                    .git_ops
                    .git_command_in_dir(
                        &["config", "--get", "user.signingkey"],
                        "check signing key",
                        &self.working_dir,
                    )
                    .await
                    .ok();

                if let Some(key_output) = key_output {
                    let key_stdout = String::from_utf8_lossy(&key_output.stdout);
                    if !key_stdout.trim().is_empty() {
                        // Verify GPG is available and the key exists
                        let gpg_check = self
                            .git_ops
                            .git_command_in_dir(
                                &["config", "--get", "gpg.program"],
                                "check GPG program",
                                &self.working_dir,
                            )
                            .await
                            .ok();

                        let gpg_program = if let Some(gpg_output) = gpg_check {
                            String::from_utf8_lossy(&gpg_output.stdout)
                                .trim()
                                .to_string()
                        } else {
                            "gpg".to_string()
                        };

                        // Try to list the key to verify it exists
                        let key = key_stdout.trim();
                        let check_key_cmd = format!("{} --list-secret-keys {}", gpg_program, key);

                        // Run the GPG check using shell command
                        let key_exists = std::process::Command::new("sh")
                            .arg("-c")
                            .arg(&check_key_cmd)
                            .output()
                            .map(|o| o.status.success())
                            .unwrap_or(false);

                        return Ok(key_exists);
                    }
                }
            }
        }

        Ok(false)
    }

    /// Parse a git status line to extract the filename
    ///
    /// Returns Some(filename) if the line is valid (length > 3), None otherwise
    fn parse_git_status_line(line: &str) -> Option<String> {
        if line.len() > 3 {
            Some(line[3..].trim().to_string())
        } else {
            None
        }
    }

    /// Check if a file should be included based on include patterns
    ///
    /// Returns false if include_patterns is empty (no patterns = exclude all)
    /// Returns true if any pattern matches the file
    /// Handles invalid patterns gracefully by skipping them
    fn should_include_file(file: &str, include_patterns: &[String]) -> bool {
        if include_patterns.is_empty() {
            return false;
        }

        for pattern_str in include_patterns {
            if let Ok(pattern) = Pattern::new(pattern_str) {
                if pattern.matches(file) {
                    return true;
                }
            }
        }

        false
    }

    /// Check if a file should be excluded based on exclude patterns
    ///
    /// Returns false if exclude_patterns is empty (no patterns = exclude nothing)
    /// Returns true if any pattern matches the file
    /// Handles invalid patterns gracefully by skipping them
    fn should_exclude_file(file: &str, exclude_patterns: &[String]) -> bool {
        if exclude_patterns.is_empty() {
            return false;
        }

        for pattern_str in exclude_patterns {
            if let Ok(pattern) = Pattern::new(pattern_str) {
                if pattern.matches(file) {
                    return true;
                }
            }
        }

        false
    }

    /// Determine if a file should be staged based on commit configuration
    ///
    /// Returns true if the file should be staged, considering both include and exclude patterns
    /// - If config is None, returns true (stage all files)
    /// - Otherwise, checks include patterns first, then exclude patterns
    fn should_stage_file(file: &str, config: Option<&CommitConfig>) -> bool {
        match config {
            None => true, // No config means stage all files
            Some(cfg) => {
                // Check include patterns
                let passes_include = match &cfg.include_files {
                    Some(patterns) => Self::should_include_file(file, patterns),
                    None => true, // No include patterns means include all
                };

                // If file doesn't pass include check, exclude it
                if !passes_include {
                    return false;
                }

                // Check exclude patterns
                match &cfg.exclude_files {
                    Some(patterns) => !Self::should_exclude_file(file, patterns),
                    None => true, // No exclude patterns means exclude none
                }
            }
        }
    }

    /// Filter files based on include/exclude patterns
    async fn get_files_to_stage(
        &self,
        commit_config: Option<&CommitConfig>,
    ) -> Result<Vec<String>> {
        // Get all changed files
        let output = self
            .git_ops
            .git_command_in_dir(&["status", "--porcelain"], "get status", &self.working_dir)
            .await?;

        let stdout = String::from_utf8_lossy(&output.stdout);
        let mut files = Vec::new();
        for line in stdout.lines() {
            if let Some(file) = Self::parse_git_status_line(line) {
                if Self::should_stage_file(&file, commit_config) {
                    files.push(file);
                }
            }
        }

        Ok(files)
    }

    /// Determine the staging strategy based on commit configuration (pure function)
    ///
    /// Returns `StagingStrategy::Selective` if include or exclude patterns are specified,
    /// otherwise returns `StagingStrategy::All` for default behavior.
    fn determine_staging_strategy(commit_config: Option<&CommitConfig>) -> StagingStrategy {
        match commit_config {
            Some(config) if config.include_files.is_some() || config.exclude_files.is_some() => {
                StagingStrategy::Selective
            }
            _ => StagingStrategy::All,
        }
    }

    /// Stage files based on the staging strategy
    ///
    /// For `StagingStrategy::All`, stages all changes with `git add .`.
    /// For `StagingStrategy::Selective`, stages only files matching include/exclude patterns.
    async fn stage_files(
        &self,
        strategy: StagingStrategy,
        commit_config: Option<&CommitConfig>,
    ) -> Result<()> {
        match strategy {
            StagingStrategy::All => {
                self.git_ops
                    .git_command_in_dir(&["add", "."], "stage all changes", &self.working_dir)
                    .await?;
            }
            StagingStrategy::Selective => {
                let files_to_stage = self.get_files_to_stage(commit_config).await?;

                if files_to_stage.is_empty() {
                    return Err(anyhow!("No files match the specified patterns"));
                }

                for file in files_to_stage {
                    self.git_ops
                        .git_command_in_dir(&["add", &file], "stage file", &self.working_dir)
                        .await?;
                }
            }
        }

        Ok(())
    }

    /// Generate commit message from template or step name (pure function)
    ///
    /// Interpolates variables in the template using the provided step name and variables map.
    /// If no template is provided, returns a default message format.
    fn generate_commit_message(
        step_name: &str,
        template: Option<&str>,
        variables: &HashMap<String, String>,
    ) -> String {
        match template {
            Some(tmpl) => {
                let mut message = tmpl.to_string();

                // Replace ${step.name}
                message = message.replace("${step.name}", step_name);

                // Replace other variables
                for (key, value) in variables {
                    message = message.replace(&format!("${{{key}}}"), value);
                    message = message.replace(&format!("${key}"), value);
                }

                message
            }
            None => format!("Auto-commit: {step_name}"),
        }
    }

    /// Validate commit message against a regex pattern (pure function)
    ///
    /// Returns Ok(()) if the message matches the pattern, or an error with details if it doesn't.
    fn validate_commit_message(message: &str, pattern: &str) -> Result<()> {
        let re = regex::Regex::new(pattern).map_err(|e| anyhow!("Invalid message pattern: {e}"))?;

        if !re.is_match(message) {
            return Err(anyhow!(
                "Commit message '{}' does not match required pattern '{}'",
                message,
                pattern
            ));
        }

        Ok(())
    }

    /// Prepare and validate commit message (combines generation and validation)
    ///
    /// Generates the message from template/step name, then validates it against the pattern
    /// if one is configured in commit_config.
    fn prepare_commit_message(
        step_name: &str,
        template: Option<&str>,
        variables: &HashMap<String, String>,
        commit_config: Option<&CommitConfig>,
    ) -> Result<String> {
        let message = Self::generate_commit_message(step_name, template, variables);

        if let Some(config) = commit_config {
            if let Some(pattern) = &config.message_pattern {
                Self::validate_commit_message(&message, pattern)?;
            }
        }

        Ok(message)
    }

    /// Build git commit arguments (pure function)
    ///
    /// Constructs the argument vector for git commit command, including message,
    /// optional author override, and optional GPG signing flag.
    fn build_commit_args(
        message: &str,
        commit_config: Option<&CommitConfig>,
        gpg_configured: bool,
    ) -> Vec<String> {
        let mut args = vec!["commit".to_string(), "-m".to_string(), message.to_string()];

        if let Some(config) = commit_config {
            if let Some(author) = &config.author {
                args.push(format!("--author={}", author));
            }

            if config.sign && gpg_configured {
                args.push("-S".to_string());
            }
        }

        args
    }

    /// Track a newly created commit
    ///
    /// Retrieves the commit details for the specified HEAD ref, sets the step name,
    /// and adds it to the tracked commits list.
    async fn track_new_commit(&self, step_name: &str, new_head: &str) -> Result<TrackedCommit> {
        let mut commits = self
            .get_commits_between(&format!("{new_head}^"), new_head)
            .await?;

        let mut commit = commits
            .pop()
            .ok_or_else(|| anyhow!("Failed to retrieve created commit"))?;

        commit.step_name = step_name.to_string();

        let mut tracked = self.tracked_commits.write().await;
        tracked.push(commit.clone());

        Ok(commit)
    }

    /// Create an auto-commit with the given configuration
    pub async fn create_auto_commit(
        &self,
        step_name: &str,
        message_template: Option<&str>,
        variables: &HashMap<String, String>,
        commit_config: Option<&CommitConfig>,
    ) -> Result<TrackedCommit> {
        // Check for changes
        if !self.has_changes().await? {
            return Err(anyhow!("No changes to commit"));
        }

        // Determine staging strategy and stage files
        let strategy = Self::determine_staging_strategy(commit_config);
        self.stage_files(strategy, commit_config).await?;

        // Prepare and validate commit message
        let message =
            Self::prepare_commit_message(step_name, message_template, variables, commit_config)?;

        // Check GPG configuration if signing is requested
        let gpg_configured = if let Some(config) = commit_config {
            if config.sign {
                let configured = self.check_gpg_config().await?;
                if !configured {
                    log::warn!(
                        "GPG signing requested but not properly configured, skipping signing"
                    );
                }
                configured
            } else {
                false
            }
        } else {
            false
        };

        // Build commit arguments
        let commit_args = Self::build_commit_args(&message, commit_config, gpg_configured);
        let commit_args_refs: Vec<&str> = commit_args.iter().map(String::as_str).collect();

        self.git_ops
            .git_command_in_dir(&commit_args_refs, "create commit", &self.working_dir)
            .await?;

        // Get the new HEAD and track the commit
        let new_head = self.get_current_head().await?;
        self.track_new_commit(step_name, &new_head).await
    }

    /// Track commits created during step execution
    pub async fn track_step_commits(
        &self,
        step_name: &str,
        before_head: &str,
        after_head: &str,
    ) -> Result<Vec<TrackedCommit>> {
        if before_head == after_head {
            return Ok(Vec::new());
        }

        let mut commits = self.get_commits_between(before_head, after_head).await?;

        // Set the step name for all commits
        for commit in &mut commits {
            commit.step_name = step_name.to_string();
        }

        // Add to tracked commits
        let mut tracked = self.tracked_commits.write().await;
        tracked.extend(commits.clone());

        Ok(commits)
    }

    /// Get all tracked commits
    pub async fn get_all_commits(&self) -> Vec<TrackedCommit> {
        self.tracked_commits.read().await.clone()
    }

    /// Interpolate variables in a message template (delegates to pure function)
    ///
    /// This method exists for backward compatibility with tests.
    /// New code should use `generate_commit_message` directly.
    #[cfg(test)]
    fn interpolate_template(
        &self,
        template: &str,
        step_name: &str,
        variables: &HashMap<String, String>,
    ) -> Result<String> {
        Ok(Self::generate_commit_message(
            step_name,
            Some(template),
            variables,
        ))
    }

    /// Validate a commit message against a pattern (delegates to pure function)
    pub fn validate_message(&self, message: &str, pattern: &str) -> Result<()> {
        Self::validate_commit_message(message, pattern)
    }

    /// Squash commits into a single commit
    pub async fn squash_commits(&self, commits: &[TrackedCommit], message: &str) -> Result<String> {
        if commits.is_empty() {
            return Err(anyhow!("No commits to squash"));
        }

        // Get the parent of the first commit
        let first_hash = &commits[0].hash;
        let parent_output = self
            .git_ops
            .git_command_in_dir(
                &["rev-parse", &format!("{first_hash}^")],
                "get parent",
                &self.working_dir,
            )
            .await?;

        let parent = String::from_utf8_lossy(&parent_output.stdout)
            .trim()
            .to_string();

        // Reset to parent
        self.git_ops
            .git_command_in_dir(
                &["reset", "--soft", &parent],
                "reset for squash",
                &self.working_dir,
            )
            .await?;

        // Create new squashed commit
        self.git_ops
            .git_command_in_dir(
                &["commit", "-m", message],
                "create squashed commit",
                &self.working_dir,
            )
            .await?;

        // Get the new commit hash
        self.get_current_head().await
    }
}

/// Result of commit tracking for a step
#[derive(Debug, Clone, Serialize)]
pub struct CommitTrackingResult {
    /// Commits created during the step
    pub commits: Vec<TrackedCommit>,

    /// Total files modified across all commits
    pub total_files_changed: usize,

    /// Total insertions across all commits
    pub total_insertions: usize,

    /// Total deletions across all commits
    pub total_deletions: usize,
}

impl CommitTrackingResult {
    /// Create from a list of commits
    pub fn from_commits(commits: Vec<TrackedCommit>) -> Self {
        let total_files_changed = commits
            .iter()
            .flat_map(|c| &c.files_changed)
            .collect::<std::collections::HashSet<_>>()
            .len();

        let total_insertions = commits.iter().map(|c| c.insertions).sum();
        let total_deletions = commits.iter().map(|c| c.deletions).sum();

        Self {
            commits,
            total_files_changed,
            total_insertions,
            total_deletions,
        }
    }
}

#[cfg(test)]
#[path = "commit_tracker_tests.rs"]
mod commit_tracker_tests;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::abstractions::MockGitOperations;

    #[tokio::test]
    async fn test_has_changes() {
        let mock_git = Arc::new(MockGitOperations::new());
        mock_git.add_success_response("M  src/main.rs\n").await;

        let tracker = CommitTracker::new(mock_git.clone(), PathBuf::from("/test"));
        assert!(tracker.has_changes().await.unwrap());

        // Test with no changes
        mock_git.add_success_response("").await;
        assert!(!tracker.has_changes().await.unwrap());
    }

    #[tokio::test]
    async fn test_get_current_head() {
        let mock_git = Arc::new(MockGitOperations::new());
        mock_git.add_success_response("abc123def456\n").await;

        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));
        let head = tracker.get_current_head().await.unwrap();
        assert_eq!(head, "abc123def456");
    }

    #[tokio::test]
    async fn test_interpolate_template() {
        let mock_git = Arc::new(MockGitOperations::new());
        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));

        let mut variables = HashMap::new();
        variables.insert("item".to_string(), "user.py".to_string());

        let result = tracker
            .interpolate_template(
                "feat: modernize ${item} in ${step.name}",
                "refactor-step",
                &variables,
            )
            .unwrap();

        assert_eq!(result, "feat: modernize user.py in refactor-step");
    }

    #[tokio::test]
    async fn test_validate_message() {
        let mock_git = Arc::new(MockGitOperations::new());
        let tracker = CommitTracker::new(mock_git, PathBuf::from("/test"));

        // Valid conventional commit
        tracker
            .validate_message(
                "feat: add new feature",
                r"^(feat|fix|docs|style|refactor|test|chore):",
            )
            .unwrap();

        // Invalid message
        let result = tracker.validate_message(
            "bad message",
            r"^(feat|fix|docs|style|refactor|test|chore):",
        );
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_commit_tracking_result() {
        let commits = vec![
            TrackedCommit {
                hash: "abc123".to_string(),
                message: "commit 1".to_string(),
                author: "test".to_string(),
                timestamp: Utc::now(),
                files_changed: vec![PathBuf::from("file1.rs"), PathBuf::from("file2.rs")],
                insertions: 10,
                deletions: 5,
                step_name: "step1".to_string(),
                agent_id: None,
            },
            TrackedCommit {
                hash: "def456".to_string(),
                message: "commit 2".to_string(),
                author: "test".to_string(),
                timestamp: Utc::now(),
                files_changed: vec![PathBuf::from("file2.rs"), PathBuf::from("file3.rs")],
                insertions: 20,
                deletions: 3,
                step_name: "step2".to_string(),
                agent_id: None,
            },
        ];

        let result = CommitTrackingResult::from_commits(commits);
        assert_eq!(result.total_files_changed, 3);
        assert_eq!(result.total_insertions, 30);
        assert_eq!(result.total_deletions, 8);
    }

    #[tokio::test]
    async fn test_step_commits_variable_format() {
        // Create test commits with known values
        let timestamp = DateTime::parse_from_rfc3339("2024-01-01T12:00:00Z")
            .unwrap()
            .with_timezone(&Utc);
        let commits = vec![
            TrackedCommit {
                hash: "abc123def456789".to_string(),
                message: "feat: implement new feature".to_string(),
                author: "Test Author <test@example.com>".to_string(),
                timestamp,
                files_changed: vec![PathBuf::from("src/main.rs"), PathBuf::from("src/lib.rs")],
                insertions: 42,
                deletions: 17,
                step_name: "implement-feature".to_string(),
                agent_id: Some("agent-001".to_string()),
            },
            TrackedCommit {
                hash: "fedcba987654321".to_string(),
                message: "fix: resolve bug in parser".to_string(),
                author: "Bug Fixer <fix@example.com>".to_string(),
                timestamp: timestamp + chrono::Duration::minutes(30),
                files_changed: vec![PathBuf::from("src/parser.rs")],
                insertions: 5,
                deletions: 3,
                step_name: "fix-bug".to_string(),
                agent_id: None,
            },
        ];

        // Serialize to JSON (mimicking what executor.rs does)
        let json_str = serde_json::to_string(&commits).unwrap();

        // Parse back to verify structure
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();

        // Verify it's an array
        assert!(parsed.is_array());
        let commits_array = parsed.as_array().unwrap();
        assert_eq!(commits_array.len(), 2);

        // Verify first commit structure
        let first_commit = &commits_array[0];
        assert_eq!(first_commit["hash"], "abc123def456789");
        assert_eq!(first_commit["message"], "feat: implement new feature");
        assert_eq!(first_commit["author"], "Test Author <test@example.com>");
        assert_eq!(first_commit["step_name"], "implement-feature");
        assert_eq!(first_commit["agent_id"], "agent-001");
        assert_eq!(first_commit["insertions"], 42);
        assert_eq!(first_commit["deletions"], 17);

        // Verify files_changed is an array
        assert!(first_commit["files_changed"].is_array());
        let files = first_commit["files_changed"].as_array().unwrap();
        assert_eq!(files.len(), 2);

        // Verify timestamp is ISO 8601 format
        assert!(first_commit["timestamp"].is_string());
        let timestamp_str = first_commit["timestamp"].as_str().unwrap();
        assert!(timestamp_str.contains("2024-01-01"));
        assert!(timestamp_str.contains("T"));
        assert!(timestamp_str.ends_with("Z"));

        // Verify second commit has null agent_id
        assert!(commits_array[1]["agent_id"].is_null());

        // Verify the format can be used in variable interpolation
        // This is what would be available as ${step.commits}
        assert!(json_str.contains("hash"));
        assert!(json_str.contains("message"));
        assert!(json_str.contains("files_changed"));
        assert!(json_str.contains("insertions"));
        assert!(json_str.contains("deletions"));
    }
}