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
//! Unified command specification and types

use anyhow::Result;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::Duration;

/// Unified command specification for all command types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum CommandSpec {
    /// Claude AI agent command
    Claude {
        command: String,
        context: Option<String>,
        tools: Option<Vec<String>>,
        output_format: Option<OutputFormat>,
    },
    /// Shell command execution
    Shell {
        command: String,
        shell: Option<String>,
        working_dir: Option<PathBuf>,
        env: Option<HashMap<String, String>>,
    },
    /// Test command with validation
    Test {
        command: String,
        expected_exit_code: Option<i32>,
        validation_script: Option<String>,
        retry_config: Option<RetryConfig>,
    },
    /// Handler command for workflow actions
    Handler {
        action: HandlerAction,
        context: HandlerContext,
        condition: Option<String>,
    },
}

/// Command request with full configuration
#[derive(Debug, Clone)]
pub struct CommandRequest {
    pub spec: CommandSpec,
    pub execution_config: ExecutionConfig,
    pub context: ExecutionContext,
    pub metadata: CommandMetadata,
}

/// Execution configuration for commands
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionConfig {
    pub timeout: Option<Duration>,
    pub capture_output: CaptureOutputMode,
    pub working_dir: Option<PathBuf>,
    pub env: HashMap<String, String>,
    pub retry_config: Option<RetryConfig>,
    pub resource_limits: Option<ResourceLimits>,
    pub validation: Option<ValidationConfig>,
}

impl Default for ExecutionConfig {
    fn default() -> Self {
        Self {
            timeout: None,
            capture_output: CaptureOutputMode::Both,
            working_dir: None,
            env: HashMap::new(),
            retry_config: None,
            resource_limits: None,
            validation: None,
        }
    }
}

/// Output capture mode for commands
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CaptureOutputMode {
    None,
    Stdout,
    Stderr,
    Both,
    Structured, // For commands that output structured data
}

/// Command metadata for tracking and observability
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommandMetadata {
    pub command_id: String,
    pub step_id: String,
    pub workflow_id: String,
    pub iteration: usize,
    pub created_at: DateTime<Utc>,
    pub tags: HashMap<String, String>,
}

impl CommandMetadata {
    pub fn new(command_type: &str) -> Self {
        Self {
            command_id: uuid::Uuid::new_v4().to_string(),
            step_id: String::new(),
            workflow_id: String::new(),
            iteration: 0,
            created_at: Utc::now(),
            tags: HashMap::from([("type".to_string(), command_type.to_string())]),
        }
    }
}

/// Retry configuration for commands
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryConfig {
    /// Retry strategy: constant, linear, or exponential
    #[serde(default = "default_strategy")]
    pub strategy: String,
    /// Maximum retry attempts
    pub max_attempts: u32,
    /// Initial delay between retries
    #[serde(
        serialize_with = "serialize_duration_secs",
        deserialize_with = "deserialize_duration_secs"
    )]
    pub initial_delay: Duration,
    /// Maximum delay cap
    #[serde(
        serialize_with = "serialize_duration_secs",
        deserialize_with = "deserialize_duration_secs"
    )]
    pub max_delay: Duration,
    /// Jitter factor (0.0 - 1.0)
    #[serde(default)]
    pub jitter: Option<f64>,
    /// Exponential base (deprecated, use strategy instead)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exponential_base: Option<f32>,
}

fn default_strategy() -> String {
    "exponential".to_string()
}

fn serialize_duration_secs<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    serializer.serialize_u64(duration.as_secs())
}

fn deserialize_duration_secs<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let secs = u64::deserialize(deserializer)?;
    Ok(Duration::from_secs(secs))
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            strategy: "exponential".to_string(),
            max_attempts: 3,
            initial_delay: Duration::from_secs(1),
            max_delay: Duration::from_secs(60),
            jitter: Some(0.25),
            exponential_base: None,
        }
    }
}

/// Resource limits for command execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceLimits {
    pub max_memory_bytes: Option<u64>,
    pub max_cpu_percent: Option<f32>,
    pub max_disk_io_bytes: Option<u64>,
    pub max_network_bytes: Option<u64>,
    pub max_file_descriptors: Option<u32>,
}

/// Validation configuration for command output
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationConfig {
    pub script: Option<String>,
    pub expected_pattern: Option<String>,
    pub forbidden_patterns: Option<Vec<String>>,
    pub json_schema: Option<serde_json::Value>,
}

/// Handler action types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum HandlerAction {
    OnSuccess { command: String },
    OnFailure { command: String },
    Cleanup { command: String },
    Rollback { command: String },
}

/// Handler execution context
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HandlerContext {
    pub previous_result: Option<String>,
    pub error_message: Option<String>,
    pub workflow_state: HashMap<String, serde_json::Value>,
}

/// Output format specification
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum OutputFormat {
    Json,
    Yaml,
    PlainText,
    Structured,
}

/// Command type enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CommandType {
    Claude,
    Shell,
    Test,
    Handler,
}

/// Execution context with runtime state
#[derive(Debug, Clone)]
pub struct ExecutionContext {
    pub working_dir: PathBuf,
    pub env_vars: HashMap<String, String>,
    pub variables: HashMap<String, String>,
    pub capture_output: bool,
    pub timeout: Option<Duration>,
    pub stdin: Option<String>,
}

impl Default for ExecutionContext {
    fn default() -> Self {
        Self {
            working_dir: std::env::current_dir().unwrap_or_default(),
            env_vars: HashMap::new(),
            variables: HashMap::new(),
            capture_output: true,
            timeout: None,
            stdin: None,
        }
    }
}

impl ExecutionContext {
    /// Substitute variables in a string
    pub fn substitute_variables(&self, input: &str) -> String {
        let mut result = input.to_string();
        for (key, value) in &self.variables {
            result = result.replace(&format!("${{{}}}", key), value);
            result = result.replace(&format!("${}", key), value);
        }
        result
    }
}

/// Executable command ready for process spawning
#[derive(Debug, Clone)]
pub struct ExecutableCommand {
    pub program: String,
    pub args: Vec<String>,
    pub command_type: CommandType,
    pub working_dir: Option<PathBuf>,
    pub env: HashMap<String, String>,
    pub expected_exit_code: Option<i32>,
    pub resource_requirements: ResourceRequirements,
    pub cleanup_requirements: CleanupRequirements,
}

impl ExecutableCommand {
    pub fn new(program: impl Into<String>) -> Self {
        Self {
            program: program.into(),
            args: Vec::new(),
            command_type: CommandType::Shell,
            working_dir: None,
            env: HashMap::new(),
            expected_exit_code: Some(0),
            resource_requirements: ResourceRequirements::default(),
            cleanup_requirements: CleanupRequirements::default(),
        }
    }

    pub fn arg(mut self, arg: impl Into<String>) -> Self {
        self.args.push(arg.into());
        self
    }

    pub fn args<I, S>(mut self, args: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.args.extend(args.into_iter().map(|s| s.into()));
        self
    }

    pub fn with_type(mut self, command_type: CommandType) -> Self {
        self.command_type = command_type;
        self
    }

    pub fn with_working_dir(mut self, dir: Option<PathBuf>) -> Self {
        self.working_dir = dir;
        self
    }

    pub fn with_env(mut self, env: HashMap<String, String>) -> Self {
        self.env = env;
        self
    }

    pub fn with_expected_exit_code(mut self, code: Option<i32>) -> Self {
        self.expected_exit_code = code;
        self
    }

    pub fn from_string(cmd: &str) -> Result<Self> {
        let parts = shell_words::split(cmd)?;
        if parts.is_empty() {
            anyhow::bail!("Empty command");
        }
        Ok(Self::new(&parts[0]).args(&parts[1..]))
    }

    pub fn display(&self) -> String {
        if self.args.is_empty() {
            self.program.clone()
        } else {
            format!("{} {}", self.program, self.args.join(" "))
        }
    }

    pub fn resource_requirements(&self) -> &ResourceRequirements {
        &self.resource_requirements
    }

    pub fn cleanup_requirements(&self) -> &CleanupRequirements {
        &self.cleanup_requirements
    }
}

/// Resource requirements for command execution
#[derive(Debug, Clone, Default)]
pub struct ResourceRequirements {
    pub estimated_memory_mb: Option<u64>,
    pub estimated_cpu_cores: Option<f32>,
    pub estimated_duration: Option<Duration>,
}

/// Cleanup requirements for process termination
#[derive(Debug, Clone)]
pub struct CleanupRequirements {
    pub kill_timeout: Duration,
    pub cleanup_children: bool,
    pub preserve_output: bool,
}

impl Default for CleanupRequirements {
    fn default() -> Self {
        Self {
            kill_timeout: Duration::from_secs(5),
            cleanup_children: true,
            preserve_output: false,
        }
    }
}

impl CommandSpec {
    /// Convert command spec to executable command
    pub fn to_executable_command(&self, context: &ExecutionContext) -> Result<ExecutableCommand> {
        match self {
            CommandSpec::Claude { command, .. } => {
                let substituted_command = context.substitute_variables(command);
                Ok(ExecutableCommand::new("claude")
                    .arg("--print")
                    .arg("--dangerously-skip-permissions")
                    .arg(&substituted_command)
                    .with_type(CommandType::Claude))
            }
            CommandSpec::Shell {
                command,
                shell,
                working_dir,
                env,
            } => {
                let substituted_command = context.substitute_variables(command);
                let shell_cmd = shell.as_deref().unwrap_or("sh");

                let mut exec = ExecutableCommand::new(shell_cmd)
                    .arg("-c")
                    .arg(&substituted_command)
                    .with_working_dir(working_dir.clone())
                    .with_type(CommandType::Shell);

                if let Some(env) = env {
                    exec = exec.with_env(env.clone());
                }

                Ok(exec)
            }
            CommandSpec::Test {
                command,
                expected_exit_code,
                ..
            } => {
                let substituted_command = context.substitute_variables(command);
                ExecutableCommand::from_string(&substituted_command)?
                    .with_expected_exit_code(*expected_exit_code)
                    .with_type(CommandType::Test)
                    .into()
            }
            CommandSpec::Handler { action, .. } => {
                self.action_to_executable_command(action, context)
            }
        }
    }

    fn action_to_executable_command(
        &self,
        action: &HandlerAction,
        context: &ExecutionContext,
    ) -> Result<ExecutableCommand> {
        let command = match action {
            HandlerAction::OnSuccess { command }
            | HandlerAction::OnFailure { command }
            | HandlerAction::Cleanup { command }
            | HandlerAction::Rollback { command } => command,
        };

        let substituted_command = context.substitute_variables(command);
        ExecutableCommand::from_string(&substituted_command)?
            .with_type(CommandType::Handler)
            .into()
    }
}

impl From<ExecutableCommand> for Result<ExecutableCommand> {
    fn from(cmd: ExecutableCommand) -> Self {
        Ok(cmd)
    }
}