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
use super::*;
use crate::git::error::GitError;
use crate::storage::error::StorageError;
use crate::subprocess::error::ProcessError;
use std::time::Duration;

#[test]
fn test_prodigy_error_construction() {
    // Test config error
    let err = ProdigyError::config("Configuration file not found");
    assert!(matches!(err, ProdigyError::Config { .. }));
    assert_eq!(err.exit_code(), 2);
    assert_eq!(err.code(), ErrorCode::CONFIG_GENERIC);

    // Test session error
    let err = ProdigyError::session("Session expired");
    assert!(matches!(err, ProdigyError::Session { .. }));
    assert_eq!(err.exit_code(), 3);
    assert_eq!(err.code(), ErrorCode::SESSION_GENERIC);

    // Test storage error
    let err = ProdigyError::storage("Storage unavailable");
    assert!(matches!(err, ProdigyError::Storage { .. }));
    assert_eq!(err.exit_code(), 4);
    assert_eq!(err.code(), ErrorCode::STORAGE_GENERIC);

    // Test execution error
    let err = ProdigyError::execution("Command failed");
    assert!(matches!(err, ProdigyError::Execution { .. }));
    assert_eq!(err.exit_code(), 5);
    assert_eq!(err.code(), ErrorCode::EXEC_GENERIC);

    // Test workflow error
    let err = ProdigyError::workflow("Workflow failed");
    assert!(matches!(err, ProdigyError::Workflow { .. }));
    assert_eq!(err.exit_code(), 6);
    assert_eq!(err.code(), ErrorCode::WORKFLOW_GENERIC);

    // Test git error
    let err = ProdigyError::git(ErrorCode::GIT_MERGE_CONFLICT, "Merge conflict", "merge");
    assert!(matches!(err, ProdigyError::Git { .. }));
    assert_eq!(err.exit_code(), 7);
    assert_eq!(err.code(), ErrorCode::GIT_MERGE_CONFLICT);

    // Test validation error
    let err = ProdigyError::validation("Invalid input");
    assert!(matches!(err, ProdigyError::Validation { .. }));
    assert_eq!(err.exit_code(), 8);
    assert_eq!(err.code(), ErrorCode::VALIDATION_GENERIC);

    // Test other error
    let err = ProdigyError::other("Unknown error");
    assert!(matches!(err, ProdigyError::Other { .. }));
    assert_eq!(err.exit_code(), 1);
    assert_eq!(err.code(), ErrorCode::OTHER_GENERIC);
}

#[test]
fn test_error_with_context() {
    let err = ProdigyError::config("Config error").with_context("Additional context");
    let err_str = err.to_string();
    assert!(err_str.contains("Config error"));
    assert!(err_str.contains("Additional context"));
}

#[test]
fn test_error_with_source() {
    let source_err = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
    let err = ProdigyError::storage("Storage error").with_source(source_err);

    let err_str = err.to_string();
    assert!(err_str.contains("Storage error"));
    assert!(err_str.contains("[E3000]"));
}

#[test]
fn test_git_error_conversion() {
    // Test NotARepository
    let git_err = GitError::NotARepository;
    let prodigy_err: ProdigyError = git_err.into();
    assert!(matches!(prodigy_err, ProdigyError::Git { .. }));
    assert_eq!(prodigy_err.code(), ErrorCode::GIT_NOT_REPO);

    // Test BranchNotFound
    let git_err = GitError::BranchNotFound("main".to_string());
    let prodigy_err: ProdigyError = git_err.into();
    assert_eq!(prodigy_err.code(), ErrorCode::GIT_BRANCH_NOT_FOUND);

    // Test MergeConflict
    let git_err = GitError::MergeConflict { files: vec![] };
    let prodigy_err: ProdigyError = git_err.into();
    assert_eq!(prodigy_err.code(), ErrorCode::GIT_MERGE_CONFLICT);

    // Test UncommittedChanges
    let git_err = GitError::UncommittedChanges;
    let prodigy_err: ProdigyError = git_err.into();
    assert_eq!(prodigy_err.code(), ErrorCode::GIT_UNCOMMITTED);

    // Test WorktreeExists
    let git_err = GitError::WorktreeExists("feature".to_string());
    let prodigy_err: ProdigyError = git_err.into();
    assert_eq!(prodigy_err.code(), ErrorCode::GIT_WORKTREE_EXISTS);

    // Test DetachedHead
    let git_err = GitError::DetachedHead;
    let prodigy_err: ProdigyError = git_err.into();
    assert_eq!(prodigy_err.code(), ErrorCode::GIT_DETACHED_HEAD);

    // Test CommandFailed
    let git_err = GitError::CommandFailed("git pull failed".to_string());
    let prodigy_err: ProdigyError = git_err.into();
    assert_eq!(prodigy_err.code(), ErrorCode::GIT_COMMAND_FAILED);
}

#[test]
fn test_storage_error_conversion() {
    // Test Io error
    let storage_err = StorageError::Io(std::io::Error::new(
        std::io::ErrorKind::PermissionDenied,
        "Permission denied",
    ));
    let prodigy_err: ProdigyError = storage_err.into();
    assert!(matches!(prodigy_err, ProdigyError::Storage { .. }));
    assert_eq!(prodigy_err.code(), ErrorCode::STORAGE_IO_ERROR);

    // Test Serialization error
    let storage_err = StorageError::serialization("JSON parse error");
    let prodigy_err: ProdigyError = storage_err.into();
    assert_eq!(prodigy_err.code(), ErrorCode::STORAGE_SERIALIZATION_ERROR);

    // Test NotFound error
    let storage_err = StorageError::not_found("item");
    let prodigy_err: ProdigyError = storage_err.into();
    assert_eq!(prodigy_err.code(), ErrorCode::STORAGE_NOT_FOUND);

    // Test Lock error
    let storage_err = StorageError::lock("Lock failed");
    let prodigy_err: ProdigyError = storage_err.into();
    assert_eq!(prodigy_err.code(), ErrorCode::STORAGE_LOCK_FAILED);

    // Test Conflict error
    let storage_err = StorageError::conflict("Already exists");
    let prodigy_err: ProdigyError = storage_err.into();
    assert_eq!(prodigy_err.code(), ErrorCode::STORAGE_ALREADY_EXISTS);

    // Test Timeout error
    let storage_err = StorageError::Timeout(Duration::from_secs(5));
    let prodigy_err: ProdigyError = storage_err.into();
    assert_eq!(prodigy_err.code(), ErrorCode::STORAGE_TEMPORARY);
}

#[test]
fn test_process_error_conversion() {
    // Test CommandNotFound
    let proc_err = ProcessError::CommandNotFound("git".to_string());
    let prodigy_err: ProdigyError = proc_err.into();
    assert!(matches!(prodigy_err, ProdigyError::Execution { .. }));
    assert_eq!(prodigy_err.code(), ErrorCode::EXEC_COMMAND_NOT_FOUND);

    // Test Timeout
    let proc_err = ProcessError::Timeout(Duration::from_secs(30));
    let prodigy_err: ProdigyError = proc_err.into();
    assert_eq!(prodigy_err.code(), ErrorCode::EXEC_TIMEOUT);

    // Test ExitCode
    let proc_err = ProcessError::ExitCode(1);
    let prodigy_err: ProdigyError = proc_err.into();
    assert_eq!(prodigy_err.code(), ErrorCode::EXEC_SUBPROCESS_FAILED);
    if let ProdigyError::Execution { exit_code, .. } = prodigy_err {
        assert_eq!(exit_code, Some(1));
    }

    // Test Signal
    let proc_err = ProcessError::Signal(15);
    let prodigy_err: ProdigyError = proc_err.into();
    assert_eq!(prodigy_err.code(), ErrorCode::EXEC_SIGNAL_RECEIVED);

    // Test Io
    let proc_err = ProcessError::Io(std::io::Error::new(
        std::io::ErrorKind::NotFound,
        "Command not found",
    ));
    let prodigy_err: ProdigyError = proc_err.into();
    assert_eq!(prodigy_err.code(), ErrorCode::EXEC_SPAWN_FAILED);
}

#[test]
fn test_error_code_descriptions() {
    // Test configuration error codes
    assert_eq!(
        describe_error_code(ErrorCode::CONFIG_NOT_FOUND),
        "Configuration file not found"
    );
    assert_eq!(
        describe_error_code(ErrorCode::CONFIG_INVALID_YAML),
        "Invalid YAML syntax in configuration"
    );

    // Test session error codes
    assert_eq!(
        describe_error_code(ErrorCode::SESSION_NOT_FOUND),
        "Session not found"
    );
    assert_eq!(
        describe_error_code(ErrorCode::SESSION_LOCKED),
        "Session is locked by another process"
    );

    // Test storage error codes
    assert_eq!(
        describe_error_code(ErrorCode::STORAGE_IO_ERROR),
        "Storage I/O error"
    );
    assert_eq!(
        describe_error_code(ErrorCode::STORAGE_DISK_FULL),
        "Storage disk is full"
    );

    // Test execution error codes
    assert_eq!(
        describe_error_code(ErrorCode::EXEC_TIMEOUT),
        "Command execution timeout"
    );
    assert_eq!(
        describe_error_code(ErrorCode::EXEC_COMMAND_NOT_FOUND),
        "Command not found"
    );

    // Test workflow error codes
    assert_eq!(
        describe_error_code(ErrorCode::WORKFLOW_NOT_FOUND),
        "Workflow not found"
    );
    assert_eq!(
        describe_error_code(ErrorCode::WORKFLOW_STEP_FAILED),
        "Workflow step failed"
    );

    // Test git error codes
    assert_eq!(
        describe_error_code(ErrorCode::GIT_NOT_REPO),
        "Not a git repository"
    );
    assert_eq!(
        describe_error_code(ErrorCode::GIT_MERGE_CONFLICT),
        "Git merge conflict"
    );

    // Test unknown error code
    assert_eq!(describe_error_code(65535), "Unknown error code");
}

#[test]
fn test_error_chaining() {
    // Create a chain of errors
    let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
    let storage_err = StorageError::Io(io_err);
    let prodigy_err: ProdigyError = storage_err.into();

    // Verify the error chain
    assert_eq!(prodigy_err.code(), ErrorCode::STORAGE_IO_ERROR);
    assert!(prodigy_err.to_string().contains("[E3001]"));
    assert!(prodigy_err.to_string().contains("Storage error"));
}

#[test]
fn test_error_formatting() {
    // Test error code formatting
    let err = ProdigyError::config_with_code(1001, "Config file missing");
    let formatted = err.to_string();
    assert!(formatted.contains("[E1001]"));
    assert!(formatted.contains("Configuration error"));
    assert!(formatted.contains("Config file missing"));

    // Test git error with operation
    let err = ProdigyError::git(6003, "Files have conflicts", "merge");
    let formatted = err.to_string();
    assert!(formatted.contains("[E6003]"));
    assert!(formatted.contains("Git operation failed"));
    assert!(formatted.contains("Files have conflicts"));
}

#[test]
fn test_lib_result_type() {
    // Test that LibResult works with ProdigyError
    fn test_function() -> crate::LibResult<String> {
        Ok("success".to_string())
    }

    fn test_error_function() -> crate::LibResult<String> {
        Err(ProdigyError::config("Test error"))
    }

    assert!(test_function().is_ok());
    assert!(test_error_function().is_err());

    if let Err(err) = test_error_function() {
        assert_eq!(err.exit_code(), 2);
    }
}

#[test]
fn test_error_recovery_checks() {
    // Test GitError recovery checks
    assert!(GitError::UncommittedChanges.is_recoverable());
    assert!(GitError::NothingToCommit.is_recoverable());
    assert!(GitError::DirtyWorkingTree.is_recoverable());
    assert!(GitError::RepositoryLocked.is_recoverable());
    assert!(!GitError::BranchNotFound("main".to_string()).is_recoverable());

    // Test GitError transient checks
    assert!(GitError::NetworkError("timeout".to_string()).is_transient());
    assert!(GitError::RepositoryLocked.is_transient());
    assert!(!GitError::BranchNotFound("main".to_string()).is_transient());

    // Test StorageError retry checks
    assert!(
        StorageError::Io(std::io::Error::new(std::io::ErrorKind::TimedOut, "Timeout"))
            .is_retryable()
    );
    assert!(StorageError::Lock("busy".to_string()).is_retryable());
    assert!(StorageError::Timeout(Duration::from_secs(5)).is_retryable());
    assert!(!StorageError::NotFound("item".to_string()).is_retryable());
}

#[test]
fn test_error_context_chain() {
    let err = ProdigyError::storage("File not found")
        .context("Loading configuration")
        .context("Starting application");

    let chain = err.chain();
    assert_eq!(chain.len(), 2);
    assert_eq!(chain[0].message, "Loading configuration");
    assert_eq!(chain[1].message, "Starting application");
}

#[test]
fn test_error_context_empty() {
    let err = ProdigyError::config("Config error");
    let chain = err.chain();
    assert!(chain.is_empty());
}

#[test]
fn test_error_source_chain() {
    let source = ProdigyError::storage("Disk full");
    let err = ProdigyError::execution("Command failed").with_error_source(source);

    assert!(err.error_source().is_some());
    let root = err.root_cause();
    assert!(matches!(root, ProdigyError::Storage { .. }));
}

#[test]
fn test_error_root_cause_no_source() {
    let err = ProdigyError::validation("Invalid input");
    let root = err.root_cause();
    // Root cause should be itself when no source
    assert!(matches!(root, ProdigyError::Validation { .. }));
}

#[test]
fn test_error_root_cause_deep_chain() {
    let err1 = ProdigyError::storage("Disk error");
    let err2 = ProdigyError::session("Session error").with_error_source(err1);
    let err3 = ProdigyError::workflow("Workflow error").with_error_source(err2);

    let root = err3.root_cause();
    assert!(matches!(root, ProdigyError::Storage { .. }));
}

#[test]
fn test_context_at_includes_location() {
    let err = ProdigyError::config("Config error").context_at("Loading file");

    let chain = err.chain();
    assert_eq!(chain.len(), 1);
    assert_eq!(chain[0].message, "Loading file");
    // Location should be set by #[track_caller]
    assert!(chain[0].location.is_some());
}

#[test]
fn test_developer_message_includes_context() {
    let err = ProdigyError::workflow("Workflow failed")
        .context("Executing step 1")
        .context("Running workflow");

    let dev_msg = err.developer_message();
    assert!(dev_msg.contains("Context chain"));
    assert!(dev_msg.contains("Executing step 1"));
    assert!(dev_msg.contains("Running workflow"));
}

#[test]
fn test_developer_message_includes_source() {
    let source = ProdigyError::storage("File not found");
    let err = ProdigyError::workflow("Workflow failed").with_error_source(source);

    let dev_msg = err.developer_message();
    assert!(dev_msg.contains("Caused by"));
}

#[test]
fn test_combined_context_and_source() {
    let source = ProdigyError::storage("Disk full").context("Writing file");
    let err = ProdigyError::execution("Command failed")
        .context("Running build")
        .with_error_source(source);

    let chain = err.chain();
    assert_eq!(chain.len(), 1);
    assert_eq!(chain[0].message, "Running build");

    let root = err.root_cause();
    assert!(matches!(root, ProdigyError::Storage { .. }));
}

#[test]
fn test_user_message_unchanged() {
    let err = ProdigyError::config("Config error")
        .context("Loading file")
        .context("Starting app");

    // user_message should not include context chain
    let user_msg = err.user_message();
    assert!(user_msg.contains("Config error"));
    // Context is separate from user message
}

#[test]
fn test_context_fluent_api() {
    // Test that context() returns Self for chaining
    let err = ProdigyError::validation("Invalid input")
        .context("Step 1")
        .context("Step 2")
        .context("Step 3");

    assert_eq!(err.chain().len(), 3);
}