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
//! Git context tracking for workflows
//!
//! This module provides automatic tracking of git changes during workflow execution,
//! exposing file change information as interpolatable variables.
//!
//! # Module Structure
//!
//! The git context functionality is organized into focused modules:
//!
//! - [`git_context`](self) - Domain logic for change tracking and variable resolution
//! - `git_context_tests` - Comprehensive test suite (24 tests across 3 phases)
//! - `git_utils` - Pure utility functions for file classification and list operations
//!
//! # Architecture
//!
//! The module follows functional programming principles with:
//! - **Pure helper functions** for parsing and formatting (all under 20 lines)
//! - **Separated I/O and logic** - Git operations isolated from business logic
//! - **Function composition** - Complex operations built from simple, testable units
//! - **Immutable data flow** - Changes are calculated, not mutated
//!
//! # Responsibilities
//!
//! This module handles:
//! - Git repository interaction and change detection (committed and uncommitted)
//! - Step-by-step change tracking during workflow execution
//! - Variable resolution for git context (e.g., `${step.files_added}`, `${workflow.commits:json}`)
//! - Format support: space-separated, newline, JSON array, comma-separated
//! - Glob pattern filtering (e.g., `${step.files_added:*.rs}`)
//! - Aggregation of changes across workflow steps
//!
//! # Variable Resolution
//!
//! Variables support multiple formats and patterns:
//!
//! ```text
//! ${step.files_added}          # Space-separated list
//! ${step.files_added:json}     # JSON array format
//! ${step.files_added:*.rs}     # Filter by glob pattern
//! ${workflow.commit_count}     # Scalar values
//! ```
//!
//! # Example
//!
//! ```rust,no_run
//! use prodigy::cook::workflow::GitChangeTracker;
//! use std::path::Path;
//!
//! # fn main() -> anyhow::Result<()> {
//! let mut tracker = GitChangeTracker::new(Path::new("."))?;
//!
//! // Track changes for a step
//! tracker.begin_step("step1")?;
//! // ... perform operations ...
//! let changes = tracker.complete_step()?;
//!
//! println!("Files added: {:?}", changes.files_added);
//! println!("Files modified: {:?}", changes.files_modified);
//!
//! // Resolve variables with format and pattern support
//! let json_files = tracker.resolve_variable("step.files_added:json")?;
//! let rust_files = tracker.resolve_variable("step.files_added:*.rs")?;
//! # Ok(())
//! # }
//! ```
//!
//! # Test Organization
//!
//! Tests are organized in `git_context_tests.rs` into three phases:
//! - **Phase 1**: Uncommitted changes detection (8 tests)
//! - **Phase 2**: Commit history walking (6 tests)
//! - **Phase 3**: Diff statistics and file changes (10 tests)

use anyhow::{Context, Result};
use git2::{DiffOptions, Oid, Repository, StatusOptions};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use tracing::{debug, warn};

use super::git_utils::{
    add_unique_file, classify_delta_status, classify_file_status, extract_file_path,
    normalize_file_lists, FileChangeType,
};

/// Git change tracker for workflow execution
#[derive(Debug, Clone)]
pub struct GitChangeTracker {
    /// Git repository handle
    repo_path: PathBuf,
    /// Initial commit when workflow started
    pub(crate) workflow_start_commit: Option<String>,
    /// Changes tracked for each step
    pub(crate) step_changes: HashMap<String, StepChanges>,
    /// Current step ID
    pub(crate) current_step_id: Option<String>,
    /// Last known commit before current step
    pub(crate) last_commit: Option<String>,
}

/// Changes tracked for a single step
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct StepChanges {
    /// Files added in this step
    pub files_added: Vec<String>,
    /// Files modified in this step
    pub files_modified: Vec<String>,
    /// Files deleted in this step
    pub files_deleted: Vec<String>,
    /// Commit SHAs created in this step
    pub commits: Vec<String>,
    /// Lines inserted
    pub insertions: usize,
    /// Lines deleted
    pub deletions: usize,
}

/// Diff statistics between two commits
#[derive(Debug, Clone, Default)]
struct DiffStats {
    insertions: usize,
    deletions: usize,
    files_added: Vec<String>,
    files_modified: Vec<String>,
    files_deleted: Vec<String>,
}

impl StepChanges {
    /// Get all changed files (added + modified + deleted)
    pub fn files_changed(&self) -> Vec<String> {
        let mut all_files = Vec::new();
        all_files.extend(self.files_added.clone());
        all_files.extend(self.files_modified.clone());
        all_files.extend(self.files_deleted.clone());
        all_files.sort();
        all_files.dedup();
        all_files
    }

    /// Get commit count
    pub fn commit_count(&self) -> usize {
        self.commits.len()
    }

    /// Merge changes from another StepChanges
    pub fn merge(&mut self, other: &StepChanges) {
        self.files_added.extend(other.files_added.clone());
        self.files_modified.extend(other.files_modified.clone());
        self.files_deleted.extend(other.files_deleted.clone());
        self.commits.extend(other.commits.clone());
        self.insertions += other.insertions;
        self.deletions += other.deletions;

        // Remove duplicates
        self.files_added.sort();
        self.files_added.dedup();
        self.files_modified.sort();
        self.files_modified.dedup();
        self.files_deleted.sort();
        self.files_deleted.dedup();
        self.commits.sort();
        self.commits.dedup();
    }

    /// Filter files by pattern
    pub fn filter_files(&self, files: &[String], pattern: &str) -> Vec<String> {
        let matcher = match glob::Pattern::new(pattern) {
            Ok(m) => m,
            Err(e) => {
                warn!("Invalid glob pattern '{}': {}", pattern, e);
                return files.to_vec();
            }
        };

        files
            .iter()
            .filter(|f| matcher.matches(f))
            .cloned()
            .collect()
    }
}

/// Format for variable output
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum VariableFormat {
    /// Space-separated (default)
    SpaceSeparated,
    /// Newline-separated
    NewlineSeparated,
    /// JSON array
    JsonArray,
    /// Comma-separated
    CommaSeparated,
}

/// Parse variable format from modifier string
fn parse_variable_format(modifier: Option<&str>) -> VariableFormat {
    match modifier {
        Some("json") => VariableFormat::JsonArray,
        Some("lines") | Some("newline") => VariableFormat::NewlineSeparated,
        Some("csv") | Some("comma") => VariableFormat::CommaSeparated,
        _ => VariableFormat::SpaceSeparated,
    }
}

/// Extract glob pattern from modifier if it contains glob characters
fn extract_glob_pattern(modifier: Option<&str>) -> Option<&str> {
    modifier.filter(|m| m.contains('*') || m.contains('?'))
}

/// Parse variable path into base path and modifier
fn parse_variable_path(var_path: &str) -> Result<(Vec<&str>, Option<&str>)> {
    let parts: Vec<&str> = var_path.split('.').collect();

    if parts.is_empty() {
        return Err(anyhow::anyhow!("Empty variable path"));
    }

    // Parse format and pattern from variable path
    // Format: step.files_added:*.md or step.files_added:json
    if let Some(pos) = parts.last().unwrap().find(':') {
        let last = parts.last().unwrap();
        let base = &last[..pos];
        let modifier = &last[pos + 1..];
        let base_path = parts[..parts.len() - 1]
            .iter()
            .chain(&[base])
            .copied()
            .collect::<Vec<_>>();
        Ok((base_path, Some(modifier)))
    } else {
        Ok((parts, None))
    }
}

impl GitChangeTracker {
    /// Create a new git change tracker
    pub fn new(working_dir: impl AsRef<Path>) -> Result<Self> {
        let repo_path = working_dir.as_ref().to_path_buf();

        // Try to open repository to validate it exists
        if let Ok(repo) = Repository::open(&repo_path) {
            let head_commit = Self::get_head_commit(&repo)?;
            debug!("Initialized GitChangeTracker at commit: {:?}", head_commit);

            Ok(Self {
                repo_path,
                workflow_start_commit: head_commit.clone(),
                step_changes: HashMap::new(),
                current_step_id: None,
                last_commit: head_commit,
            })
        } else {
            // Not a git repository, tracker will be inactive
            debug!("Working directory is not a git repository, git tracking disabled");
            Ok(Self {
                repo_path,
                workflow_start_commit: None,
                step_changes: HashMap::new(),
                current_step_id: None,
                last_commit: None,
            })
        }
    }

    /// Get HEAD commit SHA
    fn get_head_commit(repo: &Repository) -> Result<Option<String>> {
        if repo.head_detached()? {
            // Detached HEAD, get commit directly
            let head = repo.head()?;
            if let Some(oid) = head.target() {
                return Ok(Some(oid.to_string()));
            }
        } else {
            // On a branch
            let head = repo.head()?;
            if let Some(oid) = head.target() {
                return Ok(Some(oid.to_string()));
            }
        }
        Ok(None)
    }

    /// Start tracking a new step
    pub fn begin_step(&mut self, step_id: impl Into<String>) -> Result<()> {
        let step_id = step_id.into();
        debug!("Beginning step: {}", step_id);

        // Save current commit as baseline for this step
        if let Ok(repo) = Repository::open(&self.repo_path) {
            self.last_commit = Self::get_head_commit(&repo)?;
        }

        self.current_step_id = Some(step_id.clone());
        self.step_changes.insert(step_id, StepChanges::default());
        Ok(())
    }

    /// Complete tracking for current step and calculate changes
    pub fn complete_step(&mut self) -> Result<StepChanges> {
        let step_id = self
            .current_step_id
            .clone()
            .ok_or_else(|| anyhow::anyhow!("No active step to complete"))?;

        debug!("Completing step: {}", step_id);

        // Calculate changes if we're in a git repo
        if self.workflow_start_commit.is_some() {
            let changes = self.calculate_step_changes()?;
            self.step_changes.insert(step_id.clone(), changes.clone());

            // Update last commit for next step
            if let Ok(repo) = Repository::open(&self.repo_path) {
                self.last_commit = Self::get_head_commit(&repo)?;
            }

            self.current_step_id = None;
            Ok(changes)
        } else {
            // Not in a git repo, return empty changes
            self.current_step_id = None;
            Ok(StepChanges::default())
        }
    }

    /// Collect uncommitted file changes from git status
    fn collect_uncommitted_changes(repo: &Repository) -> Result<StepChanges> {
        let mut changes = StepChanges::default();
        let mut status_opts = StatusOptions::new();
        status_opts.include_untracked(true);
        let statuses = repo.statuses(Some(&mut status_opts))?;

        for entry in statuses.iter() {
            let path = match entry.path() {
                Some(p) => p,
                None => continue,
            };

            match classify_file_status(entry.status()) {
                FileChangeType::Added => changes.files_added.push(path.to_string()),
                FileChangeType::Modified => changes.files_modified.push(path.to_string()),
                FileChangeType::Deleted => changes.files_deleted.push(path.to_string()),
                FileChangeType::Unknown => {}
            }
        }

        Ok(changes)
    }

    /// Collect commit SHAs between two OIDs
    fn collect_commits_between(
        repo: &Repository,
        from_oid: Oid,
        to_oid: Oid,
    ) -> Result<Vec<String>> {
        let mut commits = Vec::new();
        let mut revwalk = repo.revwalk()?;
        revwalk.push(to_oid)?;
        revwalk.hide(from_oid)?;

        for oid in revwalk {
            commits.push(oid?.to_string());
        }

        Ok(commits)
    }

    /// Check if new commits exist between two commit references
    fn has_new_commits(last: &Option<String>, current: &Option<String>) -> bool {
        match (last, current) {
            (Some(l), Some(c)) => l != c,
            _ => false,
        }
    }

    /// Calculate diff statistics between two commits
    fn calculate_diff_stats(repo: &Repository, from_oid: Oid, to_oid: Oid) -> Result<DiffStats> {
        let from_commit = repo.find_commit(from_oid)?;
        let to_commit = repo.find_commit(to_oid)?;
        let from_tree = from_commit.tree()?;
        let to_tree = to_commit.tree()?;

        let diff = repo.diff_tree_to_tree(
            Some(&from_tree),
            Some(&to_tree),
            Some(&mut DiffOptions::new()),
        )?;

        let stats = diff.stats()?;
        let mut diff_stats = DiffStats {
            insertions: stats.insertions(),
            deletions: stats.deletions(),
            ..Default::default()
        };

        diff.foreach(
            &mut |delta, _progress| {
                if let Some(path_str) = extract_file_path(&delta) {
                    match classify_delta_status(delta.status()) {
                        FileChangeType::Added => {
                            add_unique_file(&mut diff_stats.files_added, path_str)
                        }
                        FileChangeType::Modified => {
                            add_unique_file(&mut diff_stats.files_modified, path_str)
                        }
                        FileChangeType::Deleted => {
                            add_unique_file(&mut diff_stats.files_deleted, path_str)
                        }
                        FileChangeType::Unknown => {}
                    }
                }
                true
            },
            None,
            None,
            None,
        )?;

        Ok(diff_stats)
    }

    /// Calculate changes for the current step
    pub(crate) fn calculate_step_changes(&self) -> Result<StepChanges> {
        let repo = Repository::open(&self.repo_path).context("Failed to open git repository")?;
        let current_commit = Self::get_head_commit(&repo)?;

        // Collect uncommitted changes
        let mut changes = Self::collect_uncommitted_changes(&repo)?;

        // Add committed changes if new commits exist
        if Self::has_new_commits(&self.last_commit, &current_commit) {
            let last_oid = Oid::from_str(self.last_commit.as_ref().unwrap())?;
            let current_oid = Oid::from_str(current_commit.as_ref().unwrap())?;

            // Collect commits and diff stats
            changes.commits = Self::collect_commits_between(&repo, last_oid, current_oid)?;
            let diff_stats = Self::calculate_diff_stats(&repo, last_oid, current_oid)?;

            // Merge diff stats into changes
            changes.insertions = diff_stats.insertions;
            changes.deletions = diff_stats.deletions;
            changes.files_added.extend(diff_stats.files_added);
            changes.files_modified.extend(diff_stats.files_modified);
            changes.files_deleted.extend(diff_stats.files_deleted);
        }

        // Remove duplicates and sort
        normalize_file_lists(&mut changes);

        debug!(
            "Step changes: {} added, {} modified, {} deleted, {} commits",
            changes.files_added.len(),
            changes.files_modified.len(),
            changes.files_deleted.len(),
            changes.commits.len()
        );

        Ok(changes)
    }

    /// Get changes for a specific step
    pub fn get_step_changes(&self, step_id: &str) -> Option<&StepChanges> {
        self.step_changes.get(step_id)
    }

    /// Get cumulative workflow changes
    pub fn get_workflow_changes(&self) -> StepChanges {
        let mut cumulative = StepChanges::default();
        for changes in self.step_changes.values() {
            cumulative.merge(changes);
        }
        cumulative
    }

    /// Format file list according to format specification
    pub fn format_file_list(files: &[String], format: VariableFormat) -> String {
        match format {
            VariableFormat::SpaceSeparated => files.join(" "),
            VariableFormat::NewlineSeparated => files.join("\n"),
            VariableFormat::JsonArray => {
                serde_json::to_string(files).unwrap_or_else(|_| "[]".to_string())
            }
            VariableFormat::CommaSeparated => files.join(","),
        }
    }

    /// Resolve a git context variable
    pub fn resolve_variable(&self, var_path: &str) -> Result<String> {
        let (base_path, modifier) = parse_variable_path(var_path)?;
        let format = parse_variable_format(modifier);
        let pattern = extract_glob_pattern(modifier);

        match base_path[..] {
            ["step", var_name] => {
                let changes = self.get_current_step_changes();
                self.resolve_step_variable(&changes, var_name, format, pattern)
            }
            ["workflow", var_name] => {
                let changes = self.get_workflow_changes();
                self.resolve_step_variable(&changes, var_name, format, pattern)
            }
            _ => Err(anyhow::anyhow!("Unknown git variable path: {}", var_path)),
        }
    }

    /// Get changes for the current step
    fn get_current_step_changes(&self) -> StepChanges {
        if let Some(step_id) = &self.current_step_id {
            self.step_changes.get(step_id).cloned().unwrap_or_default()
        } else {
            StepChanges::default()
        }
    }

    /// Resolve a variable for step changes
    fn resolve_step_variable(
        &self,
        changes: &StepChanges,
        var_name: &str,
        format: VariableFormat,
        pattern: Option<&str>,
    ) -> Result<String> {
        match var_name {
            "files_added" => {
                Ok(self.resolve_file_list(&changes.files_added, changes, format, pattern))
            }
            "files_modified" => {
                Ok(self.resolve_file_list(&changes.files_modified, changes, format, pattern))
            }
            "files_deleted" => {
                Ok(self.resolve_file_list(&changes.files_deleted, changes, format, pattern))
            }
            "files_changed" => {
                let all = changes.files_changed();
                Ok(self.resolve_file_list(&all, changes, format, pattern))
            }
            "commits" => Ok(Self::format_file_list(&changes.commits, format)),
            "commit_count" => Ok(changes.commit_count().to_string()),
            "insertions" => Ok(changes.insertions.to_string()),
            "deletions" => Ok(changes.deletions.to_string()),
            _ => Err(anyhow::anyhow!("Unknown step variable: {}", var_name)),
        }
    }

    /// Resolve a file list variable with optional pattern filtering
    fn resolve_file_list(
        &self,
        files: &[String],
        changes: &StepChanges,
        format: VariableFormat,
        pattern: Option<&str>,
    ) -> String {
        let filtered = if let Some(p) = pattern {
            changes.filter_files(files, p)
        } else {
            files.to_vec()
        };
        Self::format_file_list(&filtered, format)
    }

    /// Check if tracker is active (in a git repository)
    pub fn is_active(&self) -> bool {
        self.workflow_start_commit.is_some()
    }
}