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
#[cfg(test)]
mod tests {
    use super::super::dlq::*;
    use anyhow::Result;
    use chrono::{Duration, Utc};
    use serde_json::Value;
    use tempfile::tempdir;

    // Pure test helper functions
    fn create_test_failure_detail(
        attempt: u32,
        error_type: ErrorType,
        message: &str,
    ) -> FailureDetail {
        FailureDetail {
            attempt_number: attempt,
            timestamp: Utc::now(),
            error_type,
            error_message: message.to_string(),
            error_context: None,
            stack_trace: None,
            agent_id: format!("agent-{}", attempt),
            step_failed: "test-step".to_string(),
            duration_ms: 100,
            json_log_location: None,
        }
    }

    fn create_test_item(
        item_id: &str,
        data: Value,
        failure_count: u32,
        error_signature: &str,
        reprocess_eligible: bool,
    ) -> DeadLetteredItem {
        DeadLetteredItem {
            item_id: item_id.to_string(),
            item_data: data,
            first_attempt: Utc::now() - Duration::hours(1),
            last_attempt: Utc::now(),
            failure_count,
            failure_history: vec![create_test_failure_detail(
                1,
                ErrorType::Unknown,
                "Test error",
            )],
            error_signature: error_signature.to_string(),
            worktree_artifacts: None,
            reprocess_eligible,
            manual_review_required: false,
        }
    }

    async fn create_test_dlq(job_id: &str, max_items: usize) -> Result<DeadLetterQueue> {
        let temp_dir = tempdir()?;
        DeadLetterQueue::new(
            job_id.to_string(),
            temp_dir.path().to_path_buf(),
            max_items,
            30,
            None,
        )
        .await
    }

    fn assert_item_matches_expected(actual: &DeadLetteredItem, expected: &DeadLetteredItem) {
        assert_eq!(actual.item_id, expected.item_id);
        assert_eq!(actual.failure_count, expected.failure_count);
        assert_eq!(actual.error_signature, expected.error_signature);
        assert_eq!(actual.reprocess_eligible, expected.reprocess_eligible);
    }

    #[tokio::test]
    async fn test_dlq_basic_operations() -> Result<()> {
        let dlq = create_test_dlq("test-job", 100).await?;
        let test_item = create_test_item(
            "item-1",
            serde_json::json!({"test": "data"}),
            3,
            "TestSignature",
            true,
        );

        // Test adding an item
        dlq.add(test_item.clone()).await?;

        // Test getting the item
        let retrieved = dlq.get_item("item-1").await?;
        assert!(retrieved.is_some());
        assert_item_matches_expected(&retrieved.unwrap(), &test_item);

        // Test listing items
        let items = dlq.list_items(DLQFilter::default()).await?;
        assert_eq!(items.len(), 1);

        // Test stats
        let stats = dlq.get_stats().await?;
        assert_eq!(stats.total_items, 1);
        assert_eq!(stats.eligible_for_reprocess, 1);

        Ok(())
    }

    #[tokio::test]
    async fn test_dlq_pattern_analysis() -> Result<()> {
        let dlq = create_test_dlq("test-pattern", 100).await?;
        let error_signature = "Timeout::Connection timeout";

        // Add multiple items with similar errors
        for i in 0..3 {
            let item = create_test_item(
                &format!("item-{}", i),
                serde_json::json!({"id": i}),
                3,
                error_signature,
                true,
            );
            dlq.add(item).await?;
        }

        // Analyze patterns
        let analysis = dlq.analyze_patterns().await?;
        assert_eq!(analysis.total_items, 3);
        assert_eq!(analysis.pattern_groups.len(), 1);
        assert_eq!(analysis.pattern_groups[0].count, 3);
        assert_eq!(analysis.pattern_groups[0].signature, error_signature);

        Ok(())
    }

    #[tokio::test]
    async fn test_dlq_filtering() -> Result<()> {
        let dlq = create_test_dlq("test-filter", 100).await?;

        // Add items with different characteristics
        let reprocessable = create_test_item(
            "reprocessable",
            serde_json::json!({"type": "reprocessable"}),
            2,
            "ReprocessableError",
            true,
        );
        let non_reprocessable = create_test_item(
            "non-reprocessable",
            serde_json::json!({"type": "non-reprocessable"}),
            5,
            "FatalError",
            false,
        );

        dlq.add(reprocessable).await?;
        dlq.add(non_reprocessable).await?;

        // Test filtering by reprocess eligibility
        let filter = DLQFilter {
            reprocess_eligible: Some(true),
            ..Default::default()
        };
        let reprocessable_items = dlq.list_items(filter).await?;
        assert_eq!(reprocessable_items.len(), 1);
        assert_eq!(reprocessable_items[0].item_id, "reprocessable");

        // Test filtering by error signature
        let filter = DLQFilter {
            error_signature: Some("Fatal".to_string()),
            ..Default::default()
        };
        let fatal_items = dlq.list_items(filter).await?;
        assert_eq!(fatal_items.len(), 1);
        assert_eq!(fatal_items[0].item_id, "non-reprocessable");

        Ok(())
    }

    #[tokio::test]
    async fn test_dlq_capacity_management() -> Result<()> {
        let max_items = 3;
        let dlq = create_test_dlq("test-capacity", max_items).await?;

        // Add items up to capacity
        for i in 0..max_items + 2 {
            let item = create_test_item(
                &format!("item-{}", i),
                serde_json::json!({"index": i}),
                1,
                "TestError",
                true,
            );
            dlq.add(item).await?;
        }

        // Should have evicted oldest items
        let stats = dlq.get_stats().await?;
        assert!(stats.total_items <= max_items);

        // Verify the oldest items were evicted
        let item_0 = dlq.get_item("item-0").await?;
        assert!(item_0.is_none(), "Oldest item should have been evicted");

        Ok(())
    }

    #[tokio::test]
    async fn test_dlq_reprocessing() -> Result<()> {
        let dlq = create_test_dlq("test-reprocess", 100).await?;

        // Add reprocessable items
        let item1 = create_test_item(
            "reprocess-1",
            serde_json::json!({"data": 1}),
            2,
            "RetryableError",
            true,
        );
        let item2 = create_test_item(
            "reprocess-2",
            serde_json::json!({"data": 2}),
            2,
            "RetryableError",
            false, // Not eligible
        );

        dlq.add(item1).await?;
        dlq.add(item2).await?;

        // Reprocess eligible items
        let reprocessed = dlq
            .reprocess(vec!["reprocess-1".to_string(), "reprocess-2".to_string()])
            .await?;

        assert_eq!(reprocessed.len(), 1);
        assert_eq!(reprocessed[0], serde_json::json!({"data": 1}));

        // Verify eligible item was removed
        let item1_after = dlq.get_item("reprocess-1").await?;
        assert!(item1_after.is_none());

        // Verify non-eligible item remains
        let item2_after = dlq.get_item("reprocess-2").await?;
        assert!(item2_after.is_some());

        Ok(())
    }

    #[tokio::test]
    async fn test_dlq_purging() -> Result<()> {
        let dlq = create_test_dlq("test-purge", 100).await?;

        // Add old and new items
        let old_item = DeadLetteredItem {
            item_id: "old-item".to_string(),
            item_data: serde_json::json!({"age": "old"}),
            first_attempt: Utc::now() - Duration::days(10),
            last_attempt: Utc::now() - Duration::days(5),
            failure_count: 1,
            failure_history: vec![create_test_failure_detail(
                1,
                ErrorType::Unknown,
                "Old error",
            )],
            error_signature: "OldError".to_string(),
            worktree_artifacts: None,
            reprocess_eligible: true,
            manual_review_required: false,
        };

        let new_item = create_test_item(
            "new-item",
            serde_json::json!({"age": "new"}),
            1,
            "NewError",
            true,
        );

        dlq.add(old_item).await?;
        dlq.add(new_item).await?;

        // Purge items older than 3 days
        let cutoff = Utc::now() - Duration::days(3);
        let purged_count = dlq.purge_old_items(cutoff).await?;

        assert_eq!(purged_count, 1);

        // Verify old item was purged
        let old_item_after = dlq.get_item("old-item").await?;
        assert!(old_item_after.is_none());

        // Verify new item remains
        let new_item_after = dlq.get_item("new-item").await?;
        assert!(new_item_after.is_some());

        Ok(())
    }

    #[tokio::test]
    async fn test_error_signature_creation() {
        let signature = DeadLetterQueue::create_error_signature(
            &ErrorType::Timeout,
            "Connection failed at /path/to/file:123 with error code 42",
        );

        // Should filter out paths and numbers
        assert_eq!(signature, "Timeout::Connection failed at with error code");
    }

    #[tokio::test]
    async fn test_should_move_to_dlq() {
        assert!(!DeadLetterQueue::should_move_to_dlq(2, 3));
        assert!(DeadLetterQueue::should_move_to_dlq(4, 3));
        assert!(DeadLetterQueue::should_move_to_dlq(3, 2));
    }

    #[tokio::test]
    async fn test_dlq_error_distribution_analysis() -> Result<()> {
        let dlq = create_test_dlq("test-error-dist", 100).await?;

        // Add items with different error types
        let error_types = vec![
            ErrorType::Timeout,
            ErrorType::Timeout,
            ErrorType::CommandFailed { exit_code: 1 },
            ErrorType::ValidationFailed,
        ];

        for (i, error_type) in error_types.into_iter().enumerate() {
            let mut item = create_test_item(
                &format!("item-{}", i),
                serde_json::json!({"id": i}),
                1,
                "TestError",
                true,
            );
            item.failure_history = vec![create_test_failure_detail(
                1,
                error_type.clone(),
                "Test message",
            )];
            dlq.add(item).await?;
        }

        let analysis = dlq.analyze_patterns().await?;

        // Verify error distribution
        assert_eq!(
            *analysis
                .error_distribution
                .get(&ErrorType::Timeout)
                .unwrap_or(&0),
            2
        );
        assert_eq!(
            *analysis
                .error_distribution
                .get(&ErrorType::CommandFailed { exit_code: 1 })
                .unwrap_or(&0),
            1
        );
        assert_eq!(
            *analysis
                .error_distribution
                .get(&ErrorType::ValidationFailed)
                .unwrap_or(&0),
            1
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_dlq_persistence() -> Result<()> {
        let temp_dir = tempdir()?;
        let job_id = "persistence-test";
        let base_path = temp_dir.path().to_path_buf();

        // Create DLQ and add an item
        {
            let dlq =
                DeadLetterQueue::new(job_id.to_string(), base_path.clone(), 100, 30, None).await?;

            let item = create_test_item(
                "persistent-item",
                serde_json::json!({"persisted": true}),
                1,
                "PersistenceTest",
                true,
            );
            dlq.add(item).await?;
        }

        // Create new DLQ instance and verify item persisted
        {
            let dlq = DeadLetterQueue::new(job_id.to_string(), base_path, 100, 30, None).await?;
            let retrieved = dlq.get_item("persistent-item").await?;

            assert!(retrieved.is_some());
            let item = retrieved.unwrap();
            assert_eq!(item.item_id, "persistent-item");
            assert_eq!(item.error_signature, "PersistenceTest");
        }

        Ok(())
    }
}