xchecker 1.2.0

Spec pipeline with receipts and gateable JSON contracts
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
//! Integration tests for resume functionality (FR-ORC-002, FR-ORC-003)
//!
//! **WHITE-BOX TEST**: This test uses internal module APIs (`orchestrator::{OrchestratorConfig,
//! PhaseOrchestrator}`, `types::{...}`) and may break with internal refactors. These tests are
//! intentionally white-box to validate internal phase orchestration behavior. Production code
//! should use `OrchestratorHandle`. See FR-TEST-4 for white-box test policy.
//!
//! Tests that the orchestrator properly handles resume operations including:
//! - Current state detection from last receipt
//! - Partial artifact detection and handling
//! - Dependency validation
//! - Failed dependency detection

use anyhow::Result;
use std::collections::HashMap;
use tempfile::TempDir;
use xchecker::orchestrator::{OrchestratorConfig, PhaseOrchestrator};
use xchecker::types::{ErrorKind, PhaseId};

/// Helper to set up test environment
fn setup_test_environment(test_name: &str) -> (PhaseOrchestrator, TempDir) {
    // Use isolated home for each test to avoid conflicts
    let temp_dir = xchecker::paths::with_isolated_home();

    // Create spec directory structure
    let spec_id = format!("test-resume-{test_name}");
    let orchestrator = PhaseOrchestrator::new(&spec_id).unwrap();

    (orchestrator, temp_dir)
}

/// Test resume from Requirements phase
#[tokio::test]
async fn test_resume_from_requirements() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment("from-requirements");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Execute Requirements phase first
    let result = orchestrator.execute_requirements_phase(&config).await?;
    assert!(result.success, "Requirements phase should succeed");

    // Resume from Requirements (re-run)
    let resume_result = orchestrator
        .resume_from_phase(PhaseId::Requirements, &config)
        .await?;
    assert!(
        resume_result.success,
        "Resume from Requirements should succeed"
    );
    assert_eq!(resume_result.phase, PhaseId::Requirements);

    Ok(())
}

/// Test resume from Design phase
#[tokio::test]
async fn test_resume_from_design() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment("from-design");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Execute Requirements and Design phases
    orchestrator.execute_requirements_phase(&config).await?;
    let result = orchestrator.execute_design_phase(&config).await?;
    assert!(result.success, "Design phase should succeed");

    // Resume from Design (re-run)
    let resume_result = orchestrator
        .resume_from_phase(PhaseId::Design, &config)
        .await?;
    assert!(resume_result.success, "Resume from Design should succeed");
    assert_eq!(resume_result.phase, PhaseId::Design);

    Ok(())
}

/// Test resume from Tasks phase
#[tokio::test]
async fn test_resume_from_tasks() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment("from-tasks");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Execute Requirements, Design, and Tasks phases
    orchestrator.execute_requirements_phase(&config).await?;
    orchestrator.execute_design_phase(&config).await?;
    let result = orchestrator.execute_tasks_phase(&config).await?;
    assert!(result.success, "Tasks phase should succeed");

    // Resume from Tasks (re-run)
    let resume_result = orchestrator
        .resume_from_phase(PhaseId::Tasks, &config)
        .await?;
    assert!(resume_result.success, "Resume from Tasks should succeed");
    assert_eq!(resume_result.phase, PhaseId::Tasks);

    Ok(())
}

/// Test resume with missing dependency fails
#[tokio::test]
async fn test_resume_with_missing_dependency_fails() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment("missing-dep");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Try to resume Design without Requirements
    let result = orchestrator
        .resume_from_phase(PhaseId::Design, &config)
        .await;
    assert!(
        result.is_err(),
        "Resume should fail with missing dependency"
    );

    let err = result.unwrap_err();
    let err_msg = err.to_string();
    assert!(
        err_msg.contains("dependency")
            || err_msg.contains("Requirements")
            || err_msg.contains("requirements")
            || err_msg.contains("transition"),
        "Error should mention dependency or Requirements: {err_msg}"
    );

    Ok(())
}

/// Test resume with failed dependency fails
#[tokio::test]
async fn test_resume_with_failed_dependency_fails() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment("failed-dep");

    // Create a failed receipt for Requirements phase
    let receipt_manager =
        xchecker::receipt::ReceiptManager::new(orchestrator.artifact_manager().base_path());

    let failed_receipt = receipt_manager.create_receipt(
        "test-resume-failed-dep",
        PhaseId::Requirements,
        1, // Non-zero exit code indicates failure
        vec![],
        "0.1.0",
        "0.8.1",
        "haiku",
        None,
        HashMap::new(),
        xchecker::types::PacketEvidence {
            files: vec![],
            max_bytes: 65536,
            max_lines: 1200,
        },
        None,                             // stderr_tail
        None,                             // stderr_redacted
        vec![],                           // warnings
        None,                             // fallback_used
        "native",                         // runner
        None,                             // runner_distro
        Some(ErrorKind::Unknown),         // error_kind
        Some("Test failure".to_string()), // error_reason
        None,                             // diff_context,
        None,                             // pipeline
    );

    receipt_manager.write_receipt(&failed_receipt)?;

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Try to resume Design - should fail because Requirements failed
    let result = orchestrator
        .resume_from_phase(PhaseId::Design, &config)
        .await;
    assert!(result.is_err(), "Resume should fail when dependency failed");

    let err = result.unwrap_err();
    let err_msg = err.to_string();
    // The error could be about invalid transition or dependency not satisfied
    // Both are acceptable since the dependency failed
    assert!(
        err_msg.contains("dependency")
            || err_msg.contains("Requirements")
            || err_msg.contains("requirements")
            || err_msg.contains("failed")
            || err_msg.contains("transition")
            || err_msg.contains("Design")
            || err_msg.contains("design"),
        "Error should mention dependency, Requirements, or transition: {err_msg}"
    );

    Ok(())
}

/// Test resume with partial artifact deletes and restarts
#[tokio::test]
async fn test_resume_with_partial_artifact_deletes_and_restarts() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment("partial-artifact");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Execute Requirements phase successfully
    orchestrator.execute_requirements_phase(&config).await?;

    // Manually create a partial artifact for Design phase
    // Note: Design phase number is 10, not 01
    let partial_artifact = xchecker::artifact::Artifact {
        name: "10-design.partial.md".to_string(),
        content: "# Partial Design\n\nThis is a partial artifact from a failed run.".to_string(),
        artifact_type: xchecker::artifact::ArtifactType::Partial,
        blake3_hash: blake3::hash(b"partial content").to_hex().to_string(),
    };

    orchestrator
        .artifact_manager()
        .store_artifact(&partial_artifact)?;

    // Verify partial artifact exists
    assert!(
        orchestrator
            .artifact_manager()
            .has_partial_artifact(PhaseId::Design),
        "Partial artifact should exist before resume"
    );

    // Resume from Design - should delete partial and restart
    let resume_result = orchestrator
        .resume_from_phase(PhaseId::Design, &config)
        .await?;
    assert!(
        resume_result.success,
        "Resume should succeed after deleting partial"
    );

    // Verify partial artifact was deleted (or replaced with successful artifact)
    // After successful execution, partial should be cleaned up
    // Note: The implementation may keep the partial until success, so we just verify
    // that the phase completed successfully
    assert_eq!(resume_result.phase, PhaseId::Design);

    Ok(())
}

/// Test current state detection from last successful receipt
#[tokio::test]
async fn test_current_state_detection() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment("state-detection");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Initially, no phase should be completed
    let current = orchestrator.get_current_phase_state()?;
    assert!(current.is_none(), "Fresh spec should have no current phase");

    // Execute Requirements
    orchestrator.execute_requirements_phase(&config).await?;
    let current = orchestrator.get_current_phase_state()?;
    assert_eq!(
        current,
        Some(PhaseId::Requirements),
        "Current phase should be Requirements"
    );

    // Execute Design
    orchestrator.execute_design_phase(&config).await?;
    let current = orchestrator.get_current_phase_state()?;
    assert_eq!(
        current,
        Some(PhaseId::Design),
        "Current phase should be Design"
    );

    // Execute Tasks
    orchestrator.execute_tasks_phase(&config).await?;
    let current = orchestrator.get_current_phase_state()?;
    assert_eq!(
        current,
        Some(PhaseId::Tasks),
        "Current phase should be Tasks"
    );

    Ok(())
}

/// Test resume validates transition before execution
#[tokio::test]
async fn test_resume_validates_transition() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment("validate-transition");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Try to resume Tasks without any prior phases
    let result = orchestrator
        .resume_from_phase(PhaseId::Tasks, &config)
        .await;
    assert!(
        result.is_err(),
        "Resume should validate transition and fail"
    );

    // Execute Requirements
    orchestrator.execute_requirements_phase(&config).await?;

    // Try to resume Tasks (still missing Design)
    let result = orchestrator
        .resume_from_phase(PhaseId::Tasks, &config)
        .await;
    assert!(
        result.is_err(),
        "Resume should validate transition and fail without Design"
    );

    // Execute Design
    orchestrator.execute_design_phase(&config).await?;

    // Now Tasks should work
    let result = orchestrator
        .resume_from_phase(PhaseId::Tasks, &config)
        .await;
    assert!(
        result.is_ok(),
        "Resume should succeed after all dependencies satisfied"
    );

    Ok(())
}

/// Test resume after timeout creates new attempt
#[tokio::test]
async fn test_resume_after_timeout() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment("after-timeout");

    // Create a timeout receipt for Requirements phase
    let receipt_manager =
        xchecker::receipt::ReceiptManager::new(orchestrator.artifact_manager().base_path());

    let timeout_receipt = receipt_manager.create_receipt(
        "test-resume-after-timeout",
        PhaseId::Requirements,
        10, // Exit code 10 for timeout
        vec![],
        "0.1.0",
        "0.8.1",
        "haiku",
        None,
        HashMap::new(),
        xchecker::types::PacketEvidence {
            files: vec![],
            max_bytes: 65536,
            max_lines: 1200,
        },
        None,                                                  // stderr_tail
        None,                                                  // stderr_redacted
        vec!["phase_timeout:600".to_string()],                 // warnings
        None,                                                  // fallback_used
        "native",                                              // runner
        None,                                                  // runner_distro
        Some(ErrorKind::PhaseTimeout),                         // error_kind
        Some("Phase timed out after 600 seconds".to_string()), // error_reason
        None,                                                  // diff_context,
        None,                                                  // pipeline
    );

    receipt_manager.write_receipt(&timeout_receipt)?;

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Resume from Requirements after timeout - should work
    let resume_result = orchestrator
        .resume_from_phase(PhaseId::Requirements, &config)
        .await?;
    assert!(resume_result.success, "Resume should succeed after timeout");
    assert_eq!(resume_result.phase, PhaseId::Requirements);

    Ok(())
}

/// Test resume cleans up partial artifacts on success
#[tokio::test]
async fn test_resume_cleans_up_partial_on_success() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment("cleanup-partial");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Execute Requirements successfully
    orchestrator.execute_requirements_phase(&config).await?;

    // Create a partial artifact for Design
    // Note: Design phase number is 10, not 01
    let partial_artifact = xchecker::artifact::Artifact {
        name: "10-design.partial.md".to_string(),
        content: "# Partial Design".to_string(),
        artifact_type: xchecker::artifact::ArtifactType::Partial,
        blake3_hash: blake3::hash(b"partial").to_hex().to_string(),
    };

    orchestrator
        .artifact_manager()
        .store_artifact(&partial_artifact)?;

    // Resume Design - should succeed and clean up partial
    let result = orchestrator
        .resume_from_phase(PhaseId::Design, &config)
        .await?;
    assert!(result.success, "Resume should succeed");

    // Note: Cleanup happens after success, so we just verify the phase completed
    assert_eq!(result.phase, PhaseId::Design);

    Ok(())
}

/// Test `can_resume_from_phase` helper method
#[tokio::test]
async fn test_can_resume_from_phase() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment("can-resume");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Fresh spec can resume Requirements
    assert!(
        orchestrator.can_resume_from_phase_public(PhaseId::Requirements)?,
        "Should be able to resume Requirements on fresh spec"
    );

    // Fresh spec cannot resume Design
    assert!(
        !orchestrator.can_resume_from_phase_public(PhaseId::Design)?,
        "Should not be able to resume Design without Requirements"
    );

    // Execute Requirements
    orchestrator.execute_requirements_phase(&config).await?;

    // Now can resume Design
    assert!(
        orchestrator.can_resume_from_phase_public(PhaseId::Design)?,
        "Should be able to resume Design after Requirements"
    );

    // Still cannot resume Tasks
    assert!(
        !orchestrator.can_resume_from_phase_public(PhaseId::Tasks)?,
        "Should not be able to resume Tasks without Design"
    );

    // Execute Design
    orchestrator.execute_design_phase(&config).await?;

    // Now can resume Tasks
    assert!(
        orchestrator.can_resume_from_phase_public(PhaseId::Tasks)?,
        "Should be able to resume Tasks after Design"
    );

    Ok(())
}

/// Test resume from each phase in sequence
#[tokio::test]
async fn test_resume_from_each_phase_in_sequence() -> Result<()> {
    let (orchestrator, _temp_dir) = setup_test_environment("each-phase");

    let config = OrchestratorConfig {
        dry_run: true,
        ..Default::default()
    };

    // Execute and resume Requirements
    orchestrator.execute_requirements_phase(&config).await?;
    let result = orchestrator
        .resume_from_phase(PhaseId::Requirements, &config)
        .await?;
    assert!(result.success && result.phase == PhaseId::Requirements);

    // Execute and resume Design
    orchestrator.execute_design_phase(&config).await?;
    let result = orchestrator
        .resume_from_phase(PhaseId::Design, &config)
        .await?;
    assert!(result.success && result.phase == PhaseId::Design);

    // Execute and resume Tasks
    orchestrator.execute_tasks_phase(&config).await?;
    let result = orchestrator
        .resume_from_phase(PhaseId::Tasks, &config)
        .await?;
    assert!(result.success && result.phase == PhaseId::Tasks);

    Ok(())
}