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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
//! B3.12/B3.13: Tests for error receipt metadata
//!
//! These tests validate that error receipts contain proper metadata:
//! 1. error_kind is set appropriately based on error type
//! 2. error_reason is non-empty and doesn't contain secrets
//! 3. pipeline.execution_strategy == "controlled" even on error
//! 4. Timestamp and phase metadata are still populated
//!
//! Tests use various methods to force errors:
//! - Stub backend that returns errors
//! - Phase errors before LLM invocation
//! - Configuration errors
//! - Timeout errors

use anyhow::Result;
use std::collections::HashMap;

/// Helper to create a dry-run config for testing
fn dry_run_config() -> xchecker::orchestrator::OrchestratorConfig {
    xchecker::orchestrator::OrchestratorConfig {
        dry_run: true,
        config: HashMap::new(),
        full_config: None,
        selectors: None,
        strict_validation: false,
        redactor: Default::default(),
        hooks: None,
    }
}

/// Helper to create a unique spec ID for test isolation
fn unique_spec_id(test_name: &str) -> String {
    format!("error-receipt-{}-{}", test_name, std::process::id())
}

/// B3.12.1: Test error receipt when LLM invocation fails (simulated via invalid phase transition)
///
/// This test forces an error by attempting an invalid phase transition (Design without Requirements).
/// It verifies that the error receipt contains:
/// - error_kind set to CliArgs (invalid transition)
/// - error_reason is non-empty
/// - pipeline.execution_strategy == "controlled"
/// - Timestamp and phase metadata are populated
#[tokio::test]
async fn test_error_receipt_invalid_phase_transition() -> Result<()> {
    let _temp = xchecker::paths::with_isolated_home();
    let spec_id = unique_spec_id("invalid-transition");
    let config = dry_run_config();

    let mut handle =
        xchecker::orchestrator::OrchestratorHandle::with_config_and_force(&spec_id, config, false)?;

    // Attempt to run Design phase without running Requirements first
    // This should fail with InvalidTransition error
    let result = handle.run_phase(xchecker::types::PhaseId::Design).await;

    // The operation should fail
    assert!(
        result.is_err(),
        "Running Design without Requirements should fail"
    );

    // Check if there's a receipt for the failed Design phase
    let spec_dir = xchecker::paths::spec_root(&spec_id);
    let receipts_dir = spec_dir.join("receipts");

    if receipts_dir.exists() {
        // Look for Design receipt
        let entries: Vec<_> = std::fs::read_dir(&receipts_dir)?
            .filter_map(|e| e.ok())
            .filter(|e| {
                e.file_name()
                    .to_str()
                    .map(|n| n.starts_with("design-"))
                    .unwrap_or(false)
            })
            .collect();

        if !entries.is_empty() {
            // Read the receipt and verify error metadata
            let receipt_path = entries[0].path();
            let receipt_content = std::fs::read_to_string(&receipt_path)?;
            let receipt: serde_json::Value = serde_json::from_str(&receipt_content)?;

            // Verify error_kind is set
            assert!(
                !receipt["error_kind"].is_null(),
                "error_kind should be set on error receipt"
            );
            assert_eq!(
                receipt["error_kind"].as_str(),
                Some("cli_args"),
                "error_kind should be cli_args for invalid transition"
            );

            // Verify error_reason is non-empty
            assert!(
                !receipt["error_reason"].is_null(),
                "error_reason should be set on error receipt"
            );
            let error_reason = receipt["error_reason"].as_str().unwrap();
            assert!(!error_reason.is_empty(), "error_reason should not be empty");

            // Verify error reason mentions the problem
            assert!(
                error_reason.contains("Design") || error_reason.contains("transition"),
                "error_reason should describe the transition error"
            );

            // Verify pipeline.execution_strategy is still "controlled"
            if !receipt["pipeline"].is_null() {
                assert_eq!(
                    receipt["pipeline"]["execution_strategy"].as_str(),
                    Some("controlled"),
                    "execution_strategy should be 'controlled' even on error"
                );
            }

            // Verify timestamp is present
            assert!(
                !receipt["emitted_at"].is_null(),
                "emitted_at should be set on error receipt"
            );

            // Verify phase metadata is present
            assert_eq!(
                receipt["phase"].as_str(),
                Some("design"),
                "phase should be set correctly"
            );
            assert_eq!(
                receipt["spec_id"].as_str(),
                Some(spec_id.as_str()),
                "spec_id should be set correctly"
            );

            // Verify exit code is non-zero
            assert!(
                receipt["exit_code"].as_i64().unwrap() != 0,
                "exit_code should be non-zero for error"
            );
        }
    }

    Ok(())
}

/// B3.12.2: Test error receipt with packet overflow error
///
/// This test simulates a packet overflow error to verify error receipt metadata.
#[test]
fn test_error_receipt_packet_overflow() -> Result<()> {
    let _temp = xchecker::paths::with_isolated_home();
    let spec_id = unique_spec_id("packet-overflow");

    // Create an error
    let error = xchecker::error::XCheckerError::PacketOverflow {
        used_bytes: 100000,
        limit_bytes: 65536,
        used_lines: 1500,
        limit_lines: 1200,
    };

    // Create receipt manager
    let spec_dir = xchecker::paths::spec_root(&spec_id);
    let receipt_manager = xchecker::receipt::ReceiptManager::new(&spec_dir);

    // Create error receipt
    let packet = xchecker::types::PacketEvidence {
        files: vec![],
        max_bytes: 65536,
        max_lines: 1200,
    };

    let pipeline = Some(xchecker::types::PipelineInfo {
        execution_strategy: Some("controlled".to_string()),
    });

    let error_receipt = receipt_manager.create_error_receipt(
        &spec_id,
        xchecker::types::PhaseId::Requirements,
        &error,
        "0.1.0",
        "0.8.1",
        "haiku",
        None,
        HashMap::new(),
        packet,
        Some("Packet overflow stderr".to_string()),
        None,
        vec![],
        None,
        "native",
        None,
        None,
        pipeline,
    );

    // Verify error_kind
    assert_eq!(
        error_receipt.error_kind,
        Some(xchecker::types::ErrorKind::PacketOverflow),
        "error_kind should be PacketOverflow"
    );

    // Verify error_reason is set
    assert!(
        error_receipt.error_reason.is_some(),
        "error_reason should be set"
    );
    let error_reason = error_receipt.error_reason.as_ref().unwrap();
    assert!(!error_reason.is_empty(), "error_reason should not be empty");
    assert!(
        error_reason.contains("100000") || error_reason.contains("overflow"),
        "error_reason should mention the overflow details"
    );

    // Verify pipeline.execution_strategy is "controlled"
    assert!(
        error_receipt.pipeline.is_some(),
        "pipeline should be set on error receipt"
    );
    assert_eq!(
        error_receipt.pipeline.as_ref().unwrap().execution_strategy,
        Some("controlled".to_string()),
        "execution_strategy should be 'controlled' even on error"
    );

    // Verify timestamp is populated
    assert!(
        error_receipt.emitted_at > chrono::Utc::now() - chrono::Duration::seconds(5),
        "emitted_at should be recent"
    );

    // Verify phase metadata
    assert_eq!(error_receipt.phase, "requirements");
    assert_eq!(error_receipt.spec_id, spec_id);

    // Verify exit code matches expected error code
    assert_eq!(
        error_receipt.exit_code, 7,
        "exit_code should be 7 for PacketOverflow"
    );

    // Verify no outputs on error
    assert_eq!(
        error_receipt.outputs.len(),
        0,
        "outputs should be empty on error"
    );

    // Verify stderr_tail is present
    assert!(
        error_receipt.stderr_tail.is_some(),
        "stderr_tail should be preserved"
    );

    Ok(())
}

/// B3.12.3: Test error receipt with secret detection error
///
/// This test simulates a secret detection error to verify:
/// - error_kind is SecretDetected
/// - error_reason doesn't contain the actual secret
/// - All metadata fields are properly populated
#[test]
fn test_error_receipt_secret_detected() -> Result<()> {
    let _temp = xchecker::paths::with_isolated_home();
    let spec_id = unique_spec_id("secret-detected");

    // Create a secret detection error
    let error = xchecker::error::XCheckerError::SecretDetected {
        pattern: "github_pat".to_string(),
        location: "test.txt:42:10".to_string(),
    };

    // Create receipt manager
    let spec_dir = xchecker::paths::spec_root(&spec_id);
    let receipt_manager = xchecker::receipt::ReceiptManager::new(&spec_dir);

    let packet = xchecker::types::PacketEvidence {
        files: vec![],
        max_bytes: 65536,
        max_lines: 1200,
    };

    let pipeline = Some(xchecker::types::PipelineInfo {
        execution_strategy: Some("controlled".to_string()),
    });

    let error_receipt = receipt_manager.create_error_receipt(
        &spec_id,
        xchecker::types::PhaseId::Design,
        &error,
        "0.1.0",
        "0.8.1",
        "haiku",
        None,
        HashMap::new(),
        packet,
        None,
        None,
        vec!["Secret pattern detected".to_string()],
        None,
        "native",
        None,
        None,
        pipeline,
    );

    // Verify error_kind
    assert_eq!(
        error_receipt.error_kind,
        Some(xchecker::types::ErrorKind::SecretDetected),
        "error_kind should be SecretDetected"
    );

    // Verify error_reason is set and redacted
    assert!(
        error_receipt.error_reason.is_some(),
        "error_reason should be set"
    );
    let error_reason = error_receipt.error_reason.as_ref().unwrap();
    assert!(!error_reason.is_empty(), "error_reason should not be empty");

    // Error reason should mention the problem but not expose actual secrets
    assert!(
        error_reason.contains("Secret") || error_reason.contains("pattern"),
        "error_reason should describe the error type"
    );

    // Verify exit code
    assert_eq!(
        error_receipt.exit_code, 8,
        "exit_code should be 8 for SecretDetected"
    );

    // Verify pipeline metadata
    assert_eq!(
        error_receipt
            .pipeline
            .as_ref()
            .unwrap()
            .execution_strategy
            .as_deref(),
        Some("controlled")
    );

    // Verify warnings are preserved
    assert_eq!(
        error_receipt.warnings.len(),
        1,
        "warnings should be preserved"
    );

    // Verify timestamp and phase
    assert!(error_receipt.emitted_at > chrono::Utc::now() - chrono::Duration::seconds(5));
    assert_eq!(error_receipt.phase, "design");

    Ok(())
}

/// B3.12.4: Test error receipt with lock held error
///
/// Verifies that concurrent execution errors produce proper error receipts.
#[test]
fn test_error_receipt_lock_held() -> Result<()> {
    let _temp = xchecker::paths::with_isolated_home();
    let spec_id = unique_spec_id("lock-held");

    let error = xchecker::error::XCheckerError::ConcurrentExecution {
        id: spec_id.clone(),
    };

    let spec_dir = xchecker::paths::spec_root(&spec_id);
    let receipt_manager = xchecker::receipt::ReceiptManager::new(&spec_dir);

    let packet = xchecker::types::PacketEvidence {
        files: vec![],
        max_bytes: 65536,
        max_lines: 1200,
    };

    let pipeline = Some(xchecker::types::PipelineInfo {
        execution_strategy: Some("controlled".to_string()),
    });

    let error_receipt = receipt_manager.create_error_receipt(
        &spec_id,
        xchecker::types::PhaseId::Tasks,
        &error,
        "0.1.0",
        "0.8.1",
        "haiku",
        None,
        HashMap::new(),
        packet,
        None,
        None,
        vec![],
        None,
        "native",
        None,
        None,
        pipeline,
    );

    // Verify error_kind
    assert_eq!(
        error_receipt.error_kind,
        Some(xchecker::types::ErrorKind::LockHeld),
        "error_kind should be LockHeld"
    );

    // Verify error_reason
    assert!(error_receipt.error_reason.is_some());
    let error_reason = error_receipt.error_reason.as_ref().unwrap();
    assert!(
        error_reason.contains(&spec_id) || error_reason.contains("concurrent"),
        "error_reason should mention concurrent execution"
    );

    // Verify exit code
    assert_eq!(
        error_receipt.exit_code, 9,
        "exit_code should be 9 for LockHeld"
    );

    // Verify pipeline metadata
    assert_eq!(
        error_receipt
            .pipeline
            .as_ref()
            .unwrap()
            .execution_strategy
            .as_deref(),
        Some("controlled")
    );

    // Verify phase and timestamp
    assert_eq!(error_receipt.phase, "tasks");
    assert!(error_receipt.emitted_at > chrono::Utc::now() - chrono::Duration::seconds(5));

    Ok(())
}

/// B3.12.5: Test error receipt with Claude failure (LLM error)
///
/// Simulates an LLM invocation failure to verify error receipt metadata.
#[test]
fn test_error_receipt_claude_failure() -> Result<()> {
    let _temp = xchecker::paths::with_isolated_home();
    let spec_id = unique_spec_id("claude-failure");

    // Simulate a Claude CLI error
    let error =
        xchecker::error::XCheckerError::Claude(xchecker::error::ClaudeError::ExecutionFailed {
            stderr: "Claude CLI execution failed: command not found".to_string(),
        });

    let spec_dir = xchecker::paths::spec_root(&spec_id);
    let receipt_manager = xchecker::receipt::ReceiptManager::new(&spec_dir);

    let packet = xchecker::types::PacketEvidence {
        files: vec![],
        max_bytes: 65536,
        max_lines: 1200,
    };

    let pipeline = Some(xchecker::types::PipelineInfo {
        execution_strategy: Some("controlled".to_string()),
    });

    let error_receipt = receipt_manager.create_error_receipt(
        &spec_id,
        xchecker::types::PhaseId::Review,
        &error,
        "0.1.0",
        "0.8.1",
        "haiku",
        None,
        HashMap::new(),
        packet,
        Some("Error: claude: command not found\n".to_string()),
        Some("Redacted stderr output".to_string()),
        vec!["Claude CLI not available".to_string()],
        None,
        "native",
        None,
        None,
        pipeline,
    );

    // Verify error_kind
    assert_eq!(
        error_receipt.error_kind,
        Some(xchecker::types::ErrorKind::ClaudeFailure),
        "error_kind should be ClaudeFailure"
    );

    // Verify error_reason
    assert!(error_receipt.error_reason.is_some());
    let error_reason = error_receipt.error_reason.as_ref().unwrap();
    assert!(
        error_reason.contains("Claude") || error_reason.contains("failed"),
        "error_reason should describe Claude failure"
    );

    // Verify exit code
    assert_eq!(
        error_receipt.exit_code, 70,
        "exit_code should be 70 for ClaudeFailure"
    );

    // Verify pipeline metadata is preserved on error
    assert!(error_receipt.pipeline.is_some());
    assert_eq!(
        error_receipt
            .pipeline
            .as_ref()
            .unwrap()
            .execution_strategy
            .as_deref(),
        Some("controlled"),
        "execution_strategy should remain 'controlled' even on LLM failure"
    );

    // Verify stderr is captured
    assert!(error_receipt.stderr_tail.is_some());
    assert!(error_receipt.stderr_redacted.is_some());

    // Verify warnings are preserved
    assert!(!error_receipt.warnings.is_empty());

    // Verify metadata fields
    assert_eq!(error_receipt.phase, "review");
    assert_eq!(error_receipt.spec_id, spec_id);
    assert!(error_receipt.emitted_at > chrono::Utc::now() - chrono::Duration::seconds(5));
    assert_eq!(error_receipt.xchecker_version, "0.1.0");
    assert_eq!(error_receipt.claude_cli_version, "0.8.1");
    assert_eq!(error_receipt.model_full_name, "haiku");

    Ok(())
}

/// B3.12.6: Test error receipt with unknown error
///
/// Tests that generic/unknown errors still produce valid receipts.
#[test]
fn test_error_receipt_unknown_error() -> Result<()> {
    let _temp = xchecker::paths::with_isolated_home();
    let spec_id = unique_spec_id("unknown-error");

    // Create a generic IO error
    let error =
        xchecker::error::XCheckerError::Io(std::io::Error::other("Something unexpected happened"));

    let spec_dir = xchecker::paths::spec_root(&spec_id);
    let receipt_manager = xchecker::receipt::ReceiptManager::new(&spec_dir);

    let packet = xchecker::types::PacketEvidence {
        files: vec![],
        max_bytes: 65536,
        max_lines: 1200,
    };

    let pipeline = Some(xchecker::types::PipelineInfo {
        execution_strategy: Some("controlled".to_string()),
    });

    let error_receipt = receipt_manager.create_error_receipt(
        &spec_id,
        xchecker::types::PhaseId::Fixup,
        &error,
        "0.1.0",
        "0.8.1",
        "haiku",
        None,
        HashMap::new(),
        packet,
        None,
        None,
        vec![],
        None,
        "native",
        None,
        None,
        pipeline,
    );

    // Verify error_kind defaults to Unknown
    assert_eq!(
        error_receipt.error_kind,
        Some(xchecker::types::ErrorKind::Unknown),
        "error_kind should be Unknown for unclassified errors"
    );

    // Verify error_reason is set
    assert!(error_receipt.error_reason.is_some());
    let error_reason = error_receipt.error_reason.as_ref().unwrap();
    assert!(!error_reason.is_empty());

    // Verify exit code defaults to 1
    assert_eq!(
        error_receipt.exit_code, 1,
        "exit_code should be 1 for unknown errors"
    );

    // Verify pipeline metadata is still set
    assert!(error_receipt.pipeline.is_some());
    assert_eq!(
        error_receipt
            .pipeline
            .as_ref()
            .unwrap()
            .execution_strategy
            .as_deref(),
        Some("controlled")
    );

    // Verify basic metadata
    assert_eq!(error_receipt.phase, "fixup");
    assert_eq!(error_receipt.spec_id, spec_id);
    assert!(error_receipt.emitted_at > chrono::Utc::now() - chrono::Duration::seconds(5));

    Ok(())
}

/// B3.12.7: Test error receipt serialization and deserialization
///
/// Verifies that error receipts can be written to disk and read back correctly.
#[test]
fn test_error_receipt_write_and_read() -> Result<()> {
    let _temp = xchecker::paths::with_isolated_home();
    let spec_id = unique_spec_id("write-read");

    let error = xchecker::error::XCheckerError::PacketOverflow {
        used_bytes: 100000,
        limit_bytes: 65536,
        used_lines: 1500,
        limit_lines: 1200,
    };

    let spec_dir = xchecker::paths::spec_root(&spec_id);
    let receipt_manager = xchecker::receipt::ReceiptManager::new(&spec_dir);

    let packet = xchecker::types::PacketEvidence {
        files: vec![],
        max_bytes: 65536,
        max_lines: 1200,
    };

    let pipeline = Some(xchecker::types::PipelineInfo {
        execution_strategy: Some("controlled".to_string()),
    });

    let error_receipt = receipt_manager.create_error_receipt(
        &spec_id,
        xchecker::types::PhaseId::Requirements,
        &error,
        "0.1.0",
        "0.8.1",
        "haiku",
        Some("sonnet".to_string()),
        HashMap::new(),
        packet,
        Some("stderr output".to_string()),
        None,
        vec!["warning1".to_string()],
        None,
        "native",
        None,
        None,
        pipeline.clone(),
    );

    // Write receipt to disk
    let receipt_path = receipt_manager.write_receipt(&error_receipt)?;
    assert!(receipt_path.exists());

    // Read receipt back
    let read_receipt =
        receipt_manager.read_latest_receipt(xchecker::types::PhaseId::Requirements)?;
    assert!(read_receipt.is_some());

    let read_receipt = read_receipt.unwrap();

    // Verify all error fields are preserved
    assert_eq!(read_receipt.error_kind, error_receipt.error_kind);
    assert_eq!(read_receipt.error_reason, error_receipt.error_reason);
    assert_eq!(read_receipt.exit_code, error_receipt.exit_code);
    assert_eq!(read_receipt.stderr_tail, error_receipt.stderr_tail);
    assert_eq!(read_receipt.warnings, error_receipt.warnings);

    // Verify pipeline info is preserved
    assert!(read_receipt.pipeline.is_some());
    assert_eq!(
        read_receipt
            .pipeline
            .as_ref()
            .unwrap()
            .execution_strategy
            .as_deref(),
        Some("controlled")
    );

    // Verify metadata
    assert_eq!(read_receipt.spec_id, spec_id);
    assert_eq!(read_receipt.phase, "requirements");
    assert_eq!(read_receipt.model_alias, Some("sonnet".to_string()));

    Ok(())
}

/// B3.13.1: Test that packet evidence is populated even on error
///
/// Verifies that packet construction evidence is preserved in error receipts.
#[test]
fn test_error_receipt_preserves_packet_evidence() -> Result<()> {
    let _temp = xchecker::paths::with_isolated_home();
    let spec_id = unique_spec_id("packet-evidence");

    let error =
        xchecker::error::XCheckerError::Claude(xchecker::error::ClaudeError::ExecutionFailed {
            stderr: "LLM timeout".to_string(),
        });

    let spec_dir = xchecker::paths::spec_root(&spec_id);
    let receipt_manager = xchecker::receipt::ReceiptManager::new(&spec_dir);

    // Create packet evidence with some files
    let packet = xchecker::types::PacketEvidence {
        files: vec![
            xchecker::types::FileEvidence {
                path: "spec.md".to_string(),
                range: Some("L1-L100".to_string()),
                blake3_pre_redaction: "abc123".to_string(),
                priority: xchecker::types::Priority::High,
            },
            xchecker::types::FileEvidence {
                path: "requirements.yaml".to_string(),
                range: None,
                blake3_pre_redaction: "def456".to_string(),
                priority: xchecker::types::Priority::Upstream,
            },
        ],
        max_bytes: 65536,
        max_lines: 1200,
    };

    let pipeline = Some(xchecker::types::PipelineInfo {
        execution_strategy: Some("controlled".to_string()),
    });

    let error_receipt = receipt_manager.create_error_receipt(
        &spec_id,
        xchecker::types::PhaseId::Design,
        &error,
        "0.1.0",
        "0.8.1",
        "haiku",
        None,
        HashMap::new(),
        packet.clone(),
        None,
        None,
        vec![],
        None,
        "native",
        None,
        None,
        pipeline,
    );

    // Verify packet evidence is preserved
    assert_eq!(error_receipt.packet.max_bytes, 65536);
    assert_eq!(error_receipt.packet.max_lines, 1200);
    assert_eq!(error_receipt.packet.files.len(), 2);

    // Verify file evidence details
    assert_eq!(error_receipt.packet.files[0].path, "spec.md");
    assert_eq!(
        error_receipt.packet.files[0].range,
        Some("L1-L100".to_string())
    );
    assert_eq!(error_receipt.packet.files[0].blake3_pre_redaction, "abc123");

    assert_eq!(error_receipt.packet.files[1].path, "requirements.yaml");
    assert_eq!(error_receipt.packet.files[1].range, None);

    Ok(())
}

/// B3.13.2: Test error receipt fields are properly typed
///
/// Ensures all error receipt fields have correct types for JSON serialization.
#[test]
fn test_error_receipt_json_structure() -> Result<()> {
    let _temp = xchecker::paths::with_isolated_home();
    let spec_id = unique_spec_id("json-structure");

    let error = xchecker::error::XCheckerError::SecretDetected {
        pattern: "api_key".to_string(),
        location: "config.yaml:10".to_string(),
    };

    let spec_dir = xchecker::paths::spec_root(&spec_id);
    let receipt_manager = xchecker::receipt::ReceiptManager::new(&spec_dir);

    let packet = xchecker::types::PacketEvidence {
        files: vec![],
        max_bytes: 65536,
        max_lines: 1200,
    };

    let pipeline = Some(xchecker::types::PipelineInfo {
        execution_strategy: Some("controlled".to_string()),
    });

    let error_receipt = receipt_manager.create_error_receipt(
        &spec_id,
        xchecker::types::PhaseId::Requirements,
        &error,
        "0.1.0",
        "0.8.1",
        "haiku",
        None,
        HashMap::new(),
        packet,
        None,
        None,
        vec![],
        None,
        "native",
        None,
        None,
        pipeline,
    );

    // Serialize to JSON
    let json = serde_json::to_string_pretty(&error_receipt)?;

    // Parse back to verify structure
    let parsed: serde_json::Value = serde_json::from_str(&json)?;

    // Verify error fields are present and correctly typed
    assert!(
        parsed["error_kind"].is_string(),
        "error_kind should be string"
    );
    assert_eq!(parsed["error_kind"].as_str(), Some("secret_detected"));

    assert!(
        parsed["error_reason"].is_string(),
        "error_reason should be string"
    );
    assert!(
        !parsed["error_reason"].as_str().unwrap().is_empty(),
        "error_reason should not be empty"
    );

    assert!(
        parsed["exit_code"].is_number(),
        "exit_code should be number"
    );
    assert_eq!(parsed["exit_code"].as_i64(), Some(8));

    // Verify pipeline is object with execution_strategy
    assert!(parsed["pipeline"].is_object(), "pipeline should be object");
    assert_eq!(
        parsed["pipeline"]["execution_strategy"].as_str(),
        Some("controlled")
    );

    // Verify timestamp is string (RFC3339)
    assert!(
        parsed["emitted_at"].is_string(),
        "emitted_at should be string"
    );

    // Verify phase and spec_id
    assert_eq!(parsed["phase"].as_str(), Some("requirements"));
    assert_eq!(parsed["spec_id"].as_str(), Some(spec_id.as_str()));

    // Verify outputs is empty array on error
    assert!(parsed["outputs"].is_array(), "outputs should be array");
    assert_eq!(parsed["outputs"].as_array().unwrap().len(), 0);

    Ok(())
}