xchecker-engine 1.2.0

Core orchestration engine for xchecker phase execution
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
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
//! Single-phase execution, timeout handling, and receipt emission.
//!
//! This module contains phase execution code extracted from mod.rs.

use std::collections::HashMap;
use std::path::PathBuf;

use anyhow::{Context, Result};

use crate::error::{PhaseError, XCheckerError};
use crate::exit_codes;
use crate::fixup::{FixupMode, FixupPhase};
use crate::hooks::{HookContext, HookExecutor, HookType, execute_and_process_hook};
use crate::packet::PacketBuilder;
use crate::phase::{Phase, PhaseContext};
use crate::phases::{DesignPhase, RequirementsPhase, ReviewPhase, TasksPhase};
use crate::status::artifact::{Artifact, ArtifactType};
use crate::types::{ErrorKind, FileType, LlmInfo, PacketEvidence, PhaseId, PipelineInfo};

use super::llm::{ClaudeExecutionMetadata, LlmInvocationError};
use super::{OrchestratorConfig, PhaseOrchestrator, PhaseTimeout};

/// Result of executing a phase through the orchestrator.
///
/// Contains all information about the phase execution including
/// success status, artifacts created, and any errors encountered.
///
/// This is the primary result type returned by orchestrator execution methods
/// and is intended for CLI and Kiro callers to inspect execution outcomes.
///
/// # Fields
/// - `phase`: The phase that was executed
/// - `success`: Whether the phase completed successfully (exit_code == 0)
/// - `exit_code`: Exit code from the phase execution (0 = success, see `exit_codes` module)
/// - `artifact_paths`: Paths to artifacts that were created (empty on failure)
/// - `receipt_path`: Path to the receipt file (always present, even on failure)
/// - `error`: Human-readable error message if execution failed
#[derive(Debug)]
pub struct ExecutionResult {
    /// The phase that was executed
    pub phase: PhaseId,
    /// Whether the phase completed successfully
    pub success: bool,
    /// Exit code from the phase execution
    pub exit_code: i32,
    /// Paths to artifacts that were created
    pub artifact_paths: Vec<PathBuf>,
    /// Path to the receipt file
    pub receipt_path: Option<PathBuf>,
    /// Any error that occurred during execution
    pub error: Option<String>,
}

// ============================================================================
// ORC-002: Phase Core Execution Architecture
// ============================================================================
//
// The execute_phase function follows a 10-step execution sequence:
//
// 1. Remove stale .partial/ directories (FR-ORC-003, FR-ORC-007)
// 2. Validate transition and acquire exclusive lock
// 3. Build packet with phase context
// 4. Scan for secrets (blocks on detection)
// 5. Execute LLM invocation (or simulate in dry-run)
// 6. Handle Claude CLI failure (save partial, create failure receipt)
// 7. Postprocess Claude response into artifacts
// 8. Write partial artifacts to .partial/ staging directory (FR-ORC-004)
// 9. Promote staged artifacts to final location (atomic rename)
// 10. Create and write receipt with cryptographic hashes (FR-ORC-005, FR-ORC-006)
//
// Future Refactoring: execute_phase_core
// ---------------------------------------
// To reduce duplication between phase_exec.rs and workflow.rs, a future
// execute_phase_core function will extract steps 3-10 into a reusable helper.
// This function will return PhaseCoreOutput containing all intermediate values
// needed for receipt generation and artifact management.
//
// The separation will look like:
//   execute_phase (current)      → High-level orchestration + timeout handling
//   execute_phase_core (future)  → Core execution logic (packet → receipt)
//
// Benefits:
// - Eliminates duplicate receipt/artifact handling logic
// - Makes testing easier (can test core logic independently)
// - Simplifies workflow.rs implementation
// - Maintains backward compatibility with existing tests
//
// PhaseCoreOutput Structure
// --------------------------
// The PhaseCoreOutput struct captures intermediate values generated during
// phase execution. These values are needed for:
// - Receipt generation (packet_evidence, claude_metadata, llm_result)
// - Error handling (claude_exit_code)
// - Artifact content (phase_result)
//
// Fields:
// - packet_evidence: PacketEvidence - Metadata about files included in packet
// - claude_exit_code: i32 - Exit code from LLM execution (0 = success)
// - claude_metadata: Option<ClaudeExecutionMetadata> - LLM execution details
// - llm_result: Option<LlmResult> - Structured LLM result for receipt
// - phase_result: PhaseResult - Postprocessed artifacts from response
//
// Removed fields (v1.0 cleanup - not consumed by callers):
// - phase_id, prompt, claude_response, artifact_paths, output_hashes, atomic_write_warnings
//
// This struct is returned by execute_phase_core and consumed by workflow.rs
// for receipt generation.

/// Captures intermediate values generated during core phase execution.
///
/// This structure is used by `execute_phase_core` (ORC-002) to return values
/// needed for receipt generation and artifact management in workflow execution.
///
/// # Usage
///
/// ```ignore
/// let core_output = execute_phase_core(phase, config).await?;
///
/// // Create receipt from core output
/// let receipt = receipt_manager.create_receipt(
///     spec_id,
///     phase_id,  // from phase.id()
///     core_output.claude_exit_code,
///     // ... other fields from core_output
/// );
/// ```
///
/// # Fields
///
/// - `packet_evidence`: Metadata about files included in the LLM packet
/// - `claude_exit_code`: Exit code from LLM (0 = success, non-zero = failure)
/// - `claude_metadata`: Optional execution metadata (model, version, runner info)
/// - `llm_result`: Structured LLM result that converts to `LlmInfo` on receipts
/// - `phase_result`: Postprocessed artifacts with parsed content
///
/// # Removed Fields (v1.0 cleanup)
///
/// The following fields were removed as they are not consumed by callers:
/// - `phase_id`: Callers use `phase.id()` directly
/// - `prompt`: Only used internally during execution
/// - `claude_response`: Only used internally for postprocessing
/// - `artifact_paths`: Callers re-store artifacts directly
/// - `output_hashes`: Callers re-compute hashes
/// - `atomic_write_warnings`: Not used by workflow execution
pub(crate) struct PhaseCoreOutput {
    /// Metadata about files included in the LLM packet
    pub packet_evidence: PacketEvidence,
    /// Exit code from LLM execution (0 = success, non-zero = failure)
    pub claude_exit_code: i32,
    /// Optional LLM execution metadata (model, version, runner info, stderr)
    pub claude_metadata: Option<ClaudeExecutionMetadata>,
    /// Structured LLM result that converts to LlmInfo on receipts
    pub llm_result: Option<crate::llm::LlmResult>,
    /// Warning message when a fallback provider was used
    pub llm_fallback_warning: Option<String>,
    /// Postprocessed artifacts with parsed content from LLM response
    pub phase_result: xchecker_phase_api::PhaseResult,
}

/// Execute a phase with timeout enforcement
pub(crate) async fn execute_phase_with_timeout<F, T>(
    fut: F,
    phase_id: PhaseId,
    timeout_config: &PhaseTimeout,
) -> Result<T, XCheckerError>
where
    F: std::future::Future<Output = Result<T>>,
{
    match tokio::time::timeout(timeout_config.duration, fut).await {
        Ok(result) => result.map_err(|e| {
            // Convert anyhow::Error to XCheckerError
            // Try to downcast to XCheckerError first
            match e.downcast::<XCheckerError>() {
                Ok(xchecker_err) => xchecker_err,
                Err(_original_err) => {
                    // If it's not an XCheckerError, wrap it as a generic phase error
                    XCheckerError::Phase(PhaseError::ExecutionFailed {
                        phase: phase_id.as_str().to_string(),
                        code: 1,
                    })
                }
            }
        }),
        Err(_) => {
            // Timeout occurred
            Err(XCheckerError::Phase(PhaseError::Timeout {
                phase: phase_id.as_str().to_string(),
                timeout_seconds: timeout_config.duration.as_secs(),
            }))
        }
    }
}

impl PhaseOrchestrator {
    /// Execute the Requirements phase end-to-end with timeout.
    ///
    /// This is the primary entry point for generating requirements from a spec.
    /// Validates the phase transition, builds a packet, invokes the LLM,
    /// and stores the resulting artifacts and receipt.
    ///
    /// Used by CLI and Kiro flows; respects `dry_run` and LLM configuration.
    ///
    /// **Note**: Public for tests and advanced integrations; prefer `OrchestratorHandle`
    /// for general use. May be narrowed to `pub(crate)` in a future version.
    ///
    /// Reserved for future orchestration API; not currently used by CLI.
    ///
    /// # Errors
    /// Returns error if transition is invalid or execution fails.
    #[cfg_attr(not(test), allow(dead_code))]
    pub async fn execute_requirements_phase(
        &self,
        config: &OrchestratorConfig,
    ) -> Result<ExecutionResult> {
        // Validate transition before execution (FR-ORC-001)
        self.validate_transition(PhaseId::Requirements)?;

        let phase = self.get_phase_impl(PhaseId::Requirements, config)?;
        self.execute_phase_with_timeout_handling(phase.as_ref(), config)
            .await
    }

    /// Execute the Design phase end-to-end with timeout.
    ///
    /// Generates architecture and design documents based on requirements.
    /// Validates the phase transition and dependencies before execution.
    ///
    /// Used by CLI and Kiro flows; respects `dry_run` and LLM configuration.
    ///
    /// **Note**: Public for tests and advanced integrations; prefer `OrchestratorHandle`
    /// for general use. May be narrowed to `pub(crate)` in a future version.
    ///
    /// # Errors
    /// Returns error if transition is invalid or execution fails.
    #[allow(dead_code)] // Phase execution method for CLI/library usage
    pub async fn execute_design_phase(
        &self,
        config: &OrchestratorConfig,
    ) -> Result<ExecutionResult> {
        // Validate transition before execution (FR-ORC-001)
        self.validate_transition(PhaseId::Design)?;

        let phase = self.get_phase_impl(PhaseId::Design, config)?;
        self.execute_phase_with_timeout_handling(phase.as_ref(), config)
            .await
    }

    /// Execute the Tasks phase end-to-end with timeout.
    ///
    /// Generates implementation tasks and milestones based on design.
    /// Validates the phase transition and dependencies before execution.
    ///
    /// Used by CLI and Kiro flows; respects `dry_run` and LLM configuration.
    ///
    /// **Note**: Public for tests and advanced integrations; prefer `OrchestratorHandle`
    /// for general use. May be narrowed to `pub(crate)` in a future version.
    ///
    /// # Errors
    /// Returns error if transition is invalid or execution fails.
    #[allow(dead_code)] // Phase execution method for CLI/library usage
    pub async fn execute_tasks_phase(
        &self,
        config: &OrchestratorConfig,
    ) -> Result<ExecutionResult> {
        // Validate transition before execution (FR-ORC-001)
        self.validate_transition(PhaseId::Tasks)?;

        let phase = self.get_phase_impl(PhaseId::Tasks, config)?;
        self.execute_phase_with_timeout_handling(phase.as_ref(), config)
            .await
    }

    /// Resume execution from a specific phase.
    ///
    /// Validates that all dependencies are satisfied before executing.
    /// Use this to continue a workflow from any valid phase.
    ///
    /// # Arguments
    /// * `phase_id` - The phase to resume from
    /// * `config` - Execution configuration
    ///
    /// # Errors
    /// Returns error if dependencies are not satisfied or execution fails.
    pub async fn resume_from_phase(
        &self,
        phase_id: PhaseId,
        config: &OrchestratorConfig,
    ) -> Result<ExecutionResult> {
        // Validate transition before execution (FR-ORC-001, FR-ORC-002)
        self.validate_transition(phase_id)?;

        // Use phase factory to get the appropriate phase implementation
        let phase = self.get_phase_impl(phase_id, config)?;
        self.execute_phase_with_resume(phase.as_ref(), config).await
    }

    /// Execute a phase with timeout handling
    pub(crate) async fn execute_phase_with_timeout_handling(
        &self,
        phase: &dyn Phase,
        config: &OrchestratorConfig,
    ) -> Result<ExecutionResult> {
        let phase_id = phase.id();

        // Get timeout configuration from config
        let timeout_config = PhaseTimeout::from_config(config);

        // Execute phase with timeout
        match execute_phase_with_timeout(
            self.execute_phase(phase, config),
            phase_id,
            &timeout_config,
        )
        .await
        {
            Ok(result) => Ok(result),
            Err(XCheckerError::Phase(PhaseError::Timeout {
                phase: _,
                timeout_seconds,
            })) => {
                // Handle timeout: write partial artifact and receipt with warning
                self.handle_phase_timeout(phase_id, timeout_seconds, config)
                    .await
            }
            Err(e) => Err(e.into()),
        }
    }

    /// Handle phase timeout by writing partial artifact and receipt
    async fn handle_phase_timeout(
        &self,
        phase_id: PhaseId,
        timeout_seconds: u64,
        config: &OrchestratorConfig,
    ) -> Result<ExecutionResult> {
        // Create minimal partial artifact
        let partial_content = format!(
            "# {} Phase (Partial - Timeout)\n\nThis phase timed out after {} seconds.\n\nNo output was generated before the timeout occurred.\n",
            phase_id.as_str(),
            timeout_seconds
        );

        let partial_filename = format!(
            "{:02}-{}.partial.md",
            self.get_phase_number(phase_id),
            phase_id.as_str().to_lowercase()
        );

        // Store partial artifact
        let partial_artifact = Artifact {
            name: partial_filename,
            content: partial_content.clone(),
            artifact_type: ArtifactType::Partial,
            blake3_hash: blake3::hash(partial_content.as_bytes())
                .to_hex()
                .to_string(),
        };

        let partial_result = self.artifact_manager().store_artifact(&partial_artifact)?;
        let partial_path = partial_result.path;

        // Create receipt with timeout warning
        let packet_evidence = PacketEvidence {
            files: vec![],
            max_bytes: 65536,
            max_lines: 1200,
        };

        let mut flags = HashMap::new();
        flags.insert("phase".to_string(), phase_id.as_str().to_string());

        let warnings = vec![format!("phase_timeout:{}", timeout_seconds)];
        let pipeline_info = Some(PipelineInfo {
            execution_strategy: Some("controlled".to_string()),
        });

        // Use config values for truthful failure receipts (no hard-coded metadata)
        let configured_model = config.config.get("model").map_or("unknown", |s| s.as_str());
        let configured_runner = config
            .config
            .get("runner_mode")
            .map_or("unknown", |s| s.as_str());

        let receipt = self.receipt_manager().create_receipt_with_redactor(
            config.redactor.as_ref(),
            self.spec_id(),
            phase_id,
            exit_codes::codes::PHASE_TIMEOUT, // Exit code 10
            vec![],                           // No successful outputs
            env!("CARGO_PKG_VERSION"),
            "unknown", // Claude may have been running but we don't have metadata
            configured_model,
            None, // No model alias
            flags,
            packet_evidence,
            None, // No stderr_tail
            None, // No stderr_redacted
            warnings,
            None, // No fallback
            configured_runner,
            None, // No runner distro
            Some(ErrorKind::PhaseTimeout),
            Some(format!("Phase timed out after {timeout_seconds} seconds")),
            None, // No diff_context,
            pipeline_info,
        );

        let receipt_path = self.receipt_manager().write_receipt(&receipt)?;

        Ok(ExecutionResult {
            phase: phase_id,
            success: false,
            exit_code: exit_codes::codes::PHASE_TIMEOUT,
            artifact_paths: vec![partial_path.into_std_path_buf()],
            receipt_path: Some(receipt_path.into_std_path_buf()),
            error: Some(format!("Phase timed out after {timeout_seconds} seconds")),
        })
    }

    /// Execute a phase with resume support (handles partial artifacts)
    async fn execute_phase_with_resume(
        &self,
        phase: &dyn Phase,
        config: &OrchestratorConfig,
    ) -> Result<ExecutionResult> {
        let phase_id = phase.id();

        // Check if there's a partial artifact from a previous failed run
        let has_partial = self.artifact_manager().has_partial_artifact(phase_id);

        if has_partial {
            println!(
                "Found partial artifact for {} phase from previous failed run",
                phase_id.as_str()
            );

            if !config.dry_run {
                // In a real implementation, we might want to ask the user if they want to:
                // 1. Continue from partial (not implemented yet)
                // 2. Start fresh (delete partial and re-run)
                // For now, we'll delete the partial and start fresh
                println!("Deleting partial artifact and starting fresh...");
                self.artifact_manager().delete_partial_artifact(phase_id)?;
            }
        }

        // Execute the phase normally
        let result = self.execute_phase(phase, config).await?;

        // If successful and we had a partial, clean up any remaining partials
        if result.success {
            // Delete any partial artifacts on success (R4.5)
            if let Err(e) = self.artifact_manager().delete_partial_artifact(phase_id) {
                // Log warning but don't fail the operation
                eprintln!("Warning: Failed to clean up partial artifact: {e}");
            }
        }

        Ok(result)
    }

    /// Execute core phase logic: packet → LLM → artifacts (steps 1-8 from execute_phase).
    ///
    /// This function extracts the common execution flow shared between execute_phase
    /// and workflow execution. It performs:
    ///
    /// 1. Prompt generation
    /// 2. Packet creation with context
    /// 3. Secret scanning (returns early with error on detection)
    /// 4. Debug packet writing (if enabled)
    /// 5. LLM invocation (dry-run or real)
    /// 6. Postprocessing response into artifacts
    /// 7. Artifact staging to .partial/ + warnings collection
    /// 8. File hashing + artifact promotion to final location
    ///
    /// # Returns
    ///
    /// `PhaseCoreOutput` containing all intermediate values needed for receipt generation.
    ///
    /// # Errors
    ///
    /// Returns error if any core execution step fails. Note that LLM failures
    /// (non-zero exit codes) are NOT errors - they're captured in `claude_exit_code`.
    /// Early returns occur for:
    /// - Secret detection (XCheckerError::Phase with ErrorKind::SecretDetected)
    /// - Packet creation failures
    /// - Postprocessing failures (only if exit_code == 0)
    pub(crate) async fn execute_phase_core(
        &self,
        phase: &dyn Phase,
        config: &OrchestratorConfig,
    ) -> Result<PhaseCoreOutput> {
        let phase_id = phase.id();

        // Create phase context
        let phase_context = self.create_phase_context(phase_id, config)?;

        // Check dependencies
        self.check_phase_dependencies(phase)?;

        // Step 1: Generate prompt
        let prompt = phase.prompt(&phase_context);

        // Step 2: Build packet (FR-ORC-003)
        let packet = phase.make_packet(&phase_context).map_err(|e| {
            XCheckerError::Phase(PhaseError::PacketCreationFailed {
                phase: phase_id.as_str().to_string(),
                reason: e.to_string(),
            })
        })?;

        // Log packet hash and budget usage for visibility
        let budget = packet.budget_usage();
        tracing::info!(
            target: "xchecker::packet",
            spec_id = %self.spec_id(),
            phase = %phase_id.as_str(),
            packet_hash = %packet.hash(),
            bytes_used = budget.bytes_used,
            bytes_limit = budget.max_bytes,
            lines_used = budget.lines_used,
            lines_limit = budget.max_lines,
            "Built packet for phase"
        );

        let packet_evidence = packet.evidence.clone();

        // Step 3: Scan for secrets (FR-ORC-003, FR-SEC)
        let redactor = config.redactor.as_ref();

        // Check for secrets in the packet content - return error immediately if found
        if redactor.has_secrets(&packet.content, "packet")? {
            return Err(XCheckerError::Phase(PhaseError::ExecutionFailed {
                phase: phase_id.as_str().to_string(),
                code: exit_codes::codes::SECRET_DETECTED,
            })
            .into());
        }

        // Store packet for debugging/preview
        let _packet_preview_path = self
            .artifact_manager()
            .store_context_file(&format!("{}-packet", phase_id.as_str()), &packet.content)?;

        // Step 4: Write full debug packet if --debug-packet flag is set (FR-PKT-006, FR-PKT-007)
        // Only write after secret scan passes; file is excluded from receipts
        let debug_packet_enabled = config
            .config
            .get("debug_packet")
            .is_some_and(|s| s == "true");

        if debug_packet_enabled {
            // Get context directory from artifact manager
            let context_dir = self.artifact_manager().context_path();

            // Create a temporary PacketBuilder just to call write_debug_packet
            let temp_builder = PacketBuilder::new().map_err(|e| {
                XCheckerError::Phase(PhaseError::PacketCreationFailed {
                    phase: phase_id.as_str().to_string(),
                    reason: format!("Failed to create PacketBuilder for debug packet: {e}"),
                })
            })?;

            if let Err(e) =
                temp_builder.write_debug_packet(&packet.content, phase_id.as_str(), &context_dir)
            {
                // Log warning but don't fail the operation (debug packet is optional)
                eprintln!("Warning: Failed to write debug packet: {e}");
            }
        }

        // Step 5: Execute LLM (or simulate in dry-run mode)
        let (claude_response, claude_exit_code, claude_metadata, llm_result, llm_fallback_warning) =
            if config.dry_run {
                let simulated_llm = self.simulate_llm_result(phase_id);
                let simulated_metadata = super::llm::ClaudeExecutionMetadata {
                    model_alias: None,
                    model_full_name: "haiku".to_string(),
                    claude_cli_version: "0.8.1".to_string(),
                    fallback_used: false,
                    runner: "simulated".to_string(),
                    runner_distro: None,
                    stderr_tail: None,
                };
                (
                    self.simulate_claude_response(phase_id, &prompt),
                    0,
                    Some(simulated_metadata),
                    Some(simulated_llm),
                    None,
                )
            } else {
                // Use new LLM backend abstraction (V11: Claude CLI only)
                self.run_llm_invocation(&prompt, &packet.content, phase_id, config)
                    .await?
            };

        // Step 6: Postprocess Claude response (only if LLM succeeded)
        let phase_result = if claude_exit_code == 0 {
            phase
                .postprocess(&claude_response, &phase_context)
                .with_context(|| {
                    format!(
                        "Failed to postprocess response for phase: {}",
                        phase_id.as_str()
                    )
                })?
        } else {
            // For failed LLM runs, return empty result - caller will handle partial artifact
            xchecker_phase_api::PhaseResult {
                artifacts: vec![],
                next_step: xchecker_phase_api::NextStep::Continue,
                metadata: xchecker_phase_api::PhaseMetadata::default(),
            }
        };

        // Step 7: Write partial artifacts to .partial/ subdirectory (FR-ORC-004)
        for artifact in &phase_result.artifacts {
            // Store to .partial/ staging directory first
            let _partial_result = self
                .artifact_manager()
                .store_partial_staged_artifact(artifact)
                .with_context(|| format!("Failed to store partial artifact: {}", artifact.name))?;
        }

        // Step 8: Promote to final (atomic rename) (FR-ORC-004)
        for artifact in &phase_result.artifacts {
            let _final_path = self
                .artifact_manager()
                .promote_staged_to_final(&artifact.name)
                .with_context(|| {
                    format!("Failed to promote artifact to final: {}", artifact.name)
                })?;
        }

        // Return intermediate values for receipt generation
        // Note: Callers (workflow.rs) re-store artifacts and re-compute hashes,
        // so we don't return phase_id, artifact_paths, output_hashes, or atomic_write_warnings
        Ok(PhaseCoreOutput {
            packet_evidence,
            claude_exit_code,
            claude_metadata,
            llm_result,
            llm_fallback_warning,
            phase_result,
        })
    }

    /// Execute a single phase with full orchestration
    pub(crate) async fn execute_phase(
        &self,
        phase: &dyn Phase,
        config: &OrchestratorConfig,
    ) -> Result<ExecutionResult> {
        let phase_id = phase.id();
        let pipeline_info = Some(PipelineInfo {
            execution_strategy: Some("controlled".to_string()),
        });

        // Step 0: Remove stale .partial/ directories (FR-ORC-003, FR-ORC-007)
        self.artifact_manager()
            .remove_stale_partial_dir()
            .with_context(|| "Failed to remove stale .partial/ directory")?;

        // Step 1: Validate transition (already done before calling this method)
        // Step 2: Acquire exclusive lock (already done in constructor)

        // Create phase context
        let phase_context = self.create_phase_context(phase_id, config)?;

        // Check dependencies (Requirements phase has no deps)
        self.check_phase_dependencies(phase)?;

        // Execute pre-phase hook if configured
        // Hooks run from invocation CWD so relative paths like ./scripts/... work
        let mut hook_warnings: Vec<String> = Vec::new();
        if let Some(ref hooks_config) = config.hooks
            && let Some(hook_config) = hooks_config.get_pre_phase_hook(phase_id)
        {
            let executor = HookExecutor::new(
                std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")),
            );
            let context = HookContext::new(self.spec_id(), phase_id, HookType::PrePhase);

            match execute_and_process_hook(
                &executor,
                hook_config,
                &context,
                HookType::PrePhase,
                phase_id,
            )
            .await
            {
                Ok(outcome) => {
                    if let Some(warning) = outcome.warning() {
                        hook_warnings.push(warning.to_warning_string());
                    }
                    if !outcome.should_continue() {
                        // Pre-hook failed with on_fail=fail - abort phase but still create receipt
                        let error_reason = format!(
                            "Pre-phase hook failed: {}",
                            outcome.error().map(|e| e.to_string()).unwrap_or_default()
                        );

                        // Create failure receipt for hook failure (audit trail requirement)
                        let packet_evidence = PacketEvidence {
                            files: vec![],
                            max_bytes: 65536,
                            max_lines: 1200,
                        };
                        let mut flags = HashMap::new();
                        flags.insert("phase".to_string(), phase_id.as_str().to_string());
                        flags.insert("hook_failure".to_string(), "pre_phase".to_string());

                        // Use config values for truthful failure receipts (no hard-coded metadata)
                        let configured_model =
                            config.config.get("model").map_or("unknown", |s| s.as_str());
                        let configured_runner = config
                            .config
                            .get("runner_mode")
                            .map_or("unknown", |s| s.as_str());

                        let receipt = self.receipt_manager().create_receipt_with_redactor(
                            config.redactor.as_ref(),
                            self.spec_id(),
                            phase_id,
                            exit_codes::codes::CLAUDE_FAILURE,
                            vec![], // No outputs
                            env!("CARGO_PKG_VERSION"),
                            "unknown", // Claude hasn't run yet, so version is unknown
                            configured_model,
                            None,
                            flags,
                            packet_evidence,
                            None,
                            None,
                            hook_warnings.clone(),
                            None,
                            configured_runner,
                            None,
                            Some(ErrorKind::ClaudeFailure),
                            Some(error_reason.clone()),
                            None,
                            pipeline_info.clone(),
                        );

                        let receipt_path = self.receipt_manager().write_receipt(&receipt)?;

                        return Ok(ExecutionResult {
                            phase: phase_id,
                            success: false,
                            exit_code: exit_codes::codes::CLAUDE_FAILURE,
                            artifact_paths: vec![],
                            receipt_path: Some(receipt_path.into_std_path_buf()),
                            error: Some(error_reason),
                        });
                    }
                }
                Err(e) => {
                    // Hook execution error - treat as failure but still create receipt
                    let error_reason = format!("Pre-phase hook error: {}", e);

                    // Create failure receipt for hook error (audit trail requirement)
                    let packet_evidence = PacketEvidence {
                        files: vec![],
                        max_bytes: 65536,
                        max_lines: 1200,
                    };
                    let mut flags = HashMap::new();
                    flags.insert("phase".to_string(), phase_id.as_str().to_string());
                    flags.insert("hook_error".to_string(), "pre_phase".to_string());

                    // Use config values for truthful failure receipts (no hard-coded metadata)
                    let configured_model =
                        config.config.get("model").map_or("unknown", |s| s.as_str());
                    let configured_runner = config
                        .config
                        .get("runner_mode")
                        .map_or("unknown", |s| s.as_str());

                    let receipt = self.receipt_manager().create_receipt_with_redactor(
                        config.redactor.as_ref(),
                        self.spec_id(),
                        phase_id,
                        exit_codes::codes::CLAUDE_FAILURE,
                        vec![], // No outputs
                        env!("CARGO_PKG_VERSION"),
                        "unknown", // Claude hasn't run yet, so version is unknown
                        configured_model,
                        None,
                        flags,
                        packet_evidence,
                        None,
                        None,
                        vec![format!("hook_error:pre_phase:{}", e)],
                        None,
                        configured_runner,
                        None,
                        Some(ErrorKind::ClaudeFailure),
                        Some(error_reason.clone()),
                        None,
                        pipeline_info.clone(),
                    );

                    let receipt_path = self.receipt_manager().write_receipt(&receipt)?;

                    return Ok(ExecutionResult {
                        phase: phase_id,
                        success: false,
                        exit_code: exit_codes::codes::CLAUDE_FAILURE,
                        artifact_paths: vec![],
                        receipt_path: Some(receipt_path.into_std_path_buf()),
                        error: Some(error_reason),
                    });
                }
            }
        }

        // Generate prompt
        let prompt = phase.prompt(&phase_context);

        // Step 3: Build packet (FR-ORC-003)
        let packet = phase.make_packet(&phase_context).map_err(|e| {
            XCheckerError::Phase(PhaseError::PacketCreationFailed {
                phase: phase_id.as_str().to_string(),
                reason: e.to_string(),
            })
        })?;

        // Log packet hash and budget usage for visibility
        let budget = packet.budget_usage();
        tracing::info!(
            target: "xchecker::packet",
            spec_id = %self.spec_id(),
            phase = %phase_id.as_str(),
            packet_hash = %packet.hash(),
            bytes_used = budget.bytes_used,
            bytes_limit = budget.max_bytes,
            lines_used = budget.lines_used,
            lines_limit = budget.max_lines,
            "Built packet for phase"
        );

        // Step 4: Scan for secrets (FR-ORC-003, FR-SEC)
        let redactor = config.redactor.as_ref();

        // Check for secrets in the packet content
        if redactor.has_secrets(&packet.content, "packet")? {
            let matches = redactor.scan_for_secrets(&packet.content, "packet")?;

            // Create error receipt for secret detection (FR-SEC, FR-EXIT)
            let packet_evidence = packet.evidence.clone();
            let mut flags = HashMap::new();
            flags.insert("phase".to_string(), phase_id.as_str().to_string());

            let secret_patterns: Vec<String> =
                matches.iter().map(|m| m.pattern_id.clone()).collect();
            let error_reason = format!(
                "Secret detected in packet. Matched patterns: {}",
                secret_patterns.join(", ")
            );

            let receipt = self.receipt_manager().create_receipt_with_redactor(
                config.redactor.as_ref(),
                self.spec_id(),
                phase_id,
                exit_codes::codes::SECRET_DETECTED, // Exit code 8
                vec![],                             // No successful outputs
                env!("CARGO_PKG_VERSION"),
                "0.8.1", // Default Claude CLI version
                "haiku", // Default model
                None,    // No model alias
                flags,
                packet_evidence,
                None, // No stderr_tail
                None, // No stderr_redacted
                vec![format!("Secret detection prevented Claude invocation")],
                None,     // No fallback
                "native", // Default runner
                None,     // No runner distro
                Some(ErrorKind::SecretDetected),
                Some(error_reason.clone()),
                None, // No diff_context,
                pipeline_info.clone(),
            );

            let receipt_path = self.receipt_manager().write_receipt(&receipt)?;

            return Ok(ExecutionResult {
                phase: phase_id,
                success: false,
                exit_code: exit_codes::codes::SECRET_DETECTED,
                artifact_paths: vec![],
                receipt_path: Some(receipt_path.into_std_path_buf()),
                error: Some(error_reason),
            });
        }

        // Store packet for debugging/preview
        let _packet_preview_path = self
            .artifact_manager()
            .store_context_file(&format!("{}-packet", phase_id.as_str()), &packet.content)?;

        // Write full debug packet if --debug-packet flag is set (FR-PKT-006, FR-PKT-007)
        // Only write after secret scan passes; file is excluded from receipts
        let debug_packet_enabled = config
            .config
            .get("debug_packet")
            .is_some_and(|s| s == "true");

        if debug_packet_enabled {
            // Get context directory from artifact manager
            let context_dir = self.artifact_manager().context_path();

            // Create a temporary PacketBuilder just to call write_debug_packet
            // (This is a bit awkward but maintains the existing API)
            let temp_builder = PacketBuilder::new().map_err(|e| {
                XCheckerError::Phase(PhaseError::PacketCreationFailed {
                    phase: phase_id.as_str().to_string(),
                    reason: format!("Failed to create PacketBuilder for debug packet: {e}"),
                })
            })?;

            if let Err(e) =
                temp_builder.write_debug_packet(&packet.content, phase_id.as_str(), &context_dir)
            {
                // Log warning but don't fail the operation (debug packet is optional)
                eprintln!("Warning: Failed to write debug packet: {e}");
            }
        }

        // Execute LLM (or simulate in dry-run mode)
        let mut llm_fallback_warning: Option<String> = None;
        let (claude_response, claude_exit_code, claude_metadata, llm_result) = if config.dry_run {
            let simulated_llm = self.simulate_llm_result(phase_id);
            let simulated_metadata = super::llm::ClaudeExecutionMetadata {
                model_alias: None,
                model_full_name: "haiku".to_string(),
                claude_cli_version: "0.8.1".to_string(),
                fallback_used: false,
                runner: "simulated".to_string(),
                runner_distro: None,
                stderr_tail: None,
            };
            (
                self.simulate_claude_response(phase_id, &prompt),
                0,
                Some(simulated_metadata),
                Some(simulated_llm),
            )
        } else {
            // Use new LLM backend abstraction (V11: Claude CLI only)
            match self
                .run_llm_invocation(&prompt, &packet.content, phase_id, config)
                .await
            {
                Ok((response, exit_code, metadata, result, fallback_warning)) => {
                    llm_fallback_warning = fallback_warning;
                    (response, exit_code, metadata, result)
                }
                Err(e) => {
                    let (xchecker_err, fallback_warning) =
                        if let Some(invocation_err) = e.downcast_ref::<LlmInvocationError>() {
                            (
                                invocation_err.error(),
                                invocation_err.fallback_warning().map(|s| s.to_string()),
                            )
                        } else if let Some(xchecker_err) = e.downcast_ref::<XCheckerError>() {
                            (xchecker_err, None)
                        } else {
                            return Err(e);
                        };

                    llm_fallback_warning = fallback_warning;

                    // Check if this is a budget exhaustion error by downcasting
                    if let XCheckerError::Llm(llm_err) = xchecker_err {
                        if matches!(llm_err, crate::llm::LlmError::BudgetExceeded { .. }) {
                            // Handle budget exhaustion specially - create receipt with budget_exhausted flag
                            let packet_evidence = packet.evidence.clone();
                            let mut flags = HashMap::new();
                            flags.insert("phase".to_string(), phase_id.as_str().to_string());

                            // Use config values for truthful failure receipts (no hard-coded metadata)
                            let configured_model =
                                config.config.get("model").map_or("unknown", |s| s.as_str());
                            let configured_runner = config
                                .config
                                .get("runner_mode")
                                .map_or("unknown", |s| s.as_str());

                            let mut warnings = vec![format!("LLM budget exhausted: {}", llm_err)];
                            if let Some(ref warning) = llm_fallback_warning {
                                warnings.push(warning.clone());
                            }

                            let mut receipt = self.receipt_manager().create_receipt_with_redactor(
                                config.redactor.as_ref(),
                                self.spec_id(),
                                phase_id,
                                exit_codes::codes::CLAUDE_FAILURE, // Exit code 70
                                vec![],                            // No successful outputs
                                env!("CARGO_PKG_VERSION"),
                                "unknown", // Claude hasn't completed, so version is unknown
                                configured_model,
                                None, // No model alias
                                flags,
                                packet_evidence,
                                None, // No stderr_tail
                                None, // No stderr_redacted
                                warnings,
                                None, // No fallback
                                configured_runner,
                                None, // No runner distro
                                Some(ErrorKind::ClaudeFailure),
                                Some(llm_err.to_string()),
                                None, // No diff_context
                                pipeline_info.clone(),
                            );

                            // Attach LlmInfo with budget_exhausted flag
                            receipt.llm = Some(LlmInfo::for_budget_exhaustion());

                            let receipt_path = self.receipt_manager().write_receipt(&receipt)?;

                            return Ok(ExecutionResult {
                                phase: phase_id,
                                success: false,
                                exit_code: exit_codes::codes::CLAUDE_FAILURE,
                                artifact_paths: vec![],
                                receipt_path: Some(receipt_path.into_std_path_buf()),
                                error: Some(llm_err.to_string()),
                            });
                        }

                        let packet_evidence = packet.evidence.clone();
                        let mut flags = HashMap::new();
                        flags.insert("phase".to_string(), phase_id.as_str().to_string());

                        // Use config values for truthful failure receipts (no hard-coded metadata)
                        let configured_model =
                            config.config.get("model").map_or("unknown", |s| s.as_str());
                        let configured_runner = config
                            .config
                            .get("runner_mode")
                            .map_or("unknown", |s| s.as_str());

                        let (exit_code, error_kind) =
                            exit_codes::error_to_exit_code_and_kind(xchecker_err);

                        let invocation =
                            self.build_llm_invocation(phase_id, &prompt, &packet.content, config);
                        let provider = self
                            .config_from_orchestrator_config(config)
                            .llm
                            .provider
                            .unwrap_or_else(|| "claude-cli".to_string());

                        let mut llm_info = LlmInfo {
                            provider: Some(provider),
                            model_used: if invocation.model.is_empty() {
                                None
                            } else {
                                Some(invocation.model.clone())
                            },
                            tokens_input: None,
                            tokens_output: None,
                            timed_out: None,
                            timeout_seconds: Some(invocation.timeout.as_secs()),
                            budget_exhausted: None,
                        };

                        let mut warnings = Vec::new();
                        match llm_err {
                            crate::llm::LlmError::Timeout { duration } => {
                                llm_info.timed_out = Some(true);
                                llm_info.timeout_seconds = Some(duration.as_secs());
                                warnings.push(format!("phase_timeout:{}", duration.as_secs()));
                            }
                            _ => {
                                llm_info.timed_out = Some(false);
                                warnings.push(format!("llm_error:{}", llm_err));
                            }
                        }
                        if let Some(ref warning) = llm_fallback_warning {
                            warnings.push(warning.clone());
                        }

                        let mut receipt = self.receipt_manager().create_receipt_with_redactor(
                            config.redactor.as_ref(),
                            self.spec_id(),
                            phase_id,
                            exit_code,
                            vec![], // No successful outputs
                            env!("CARGO_PKG_VERSION"),
                            "unknown", // Claude hasn't completed, so version is unknown
                            configured_model,
                            None, // No model alias
                            flags,
                            packet_evidence,
                            None, // No stderr_tail
                            None, // No stderr_redacted
                            warnings,
                            None, // No fallback
                            configured_runner,
                            None, // No runner distro
                            Some(error_kind),
                            Some(llm_err.to_string()),
                            None, // No diff_context
                            pipeline_info.clone(),
                        );

                        receipt.llm = Some(llm_info);

                        let receipt_path = self.receipt_manager().write_receipt(&receipt)?;

                        return Ok(ExecutionResult {
                            phase: phase_id,
                            success: false,
                            exit_code,
                            artifact_paths: vec![],
                            receipt_path: Some(receipt_path.into_std_path_buf()),
                            error: Some(llm_err.to_string()),
                        });
                    }
                    // For other errors, propagate normally
                    return Err(e);
                }
            }
        };

        // Handle Claude CLI failure (R4.3)
        if claude_exit_code != 0 {
            // Save partial output as required by R4.3
            let partial_filename = format!(
                "{:02}-{}.partial.md",
                self.get_phase_number(phase_id),
                phase_id.as_str().to_lowercase()
            );

            let partial_result = self.artifact_manager().store_artifact(&Artifact {
                name: partial_filename.clone(),
                content: claude_response.clone(),
                artifact_type: ArtifactType::Partial,
                blake3_hash: blake3::hash(claude_response.as_bytes())
                    .to_hex()
                    .to_string(),
            })?;
            let partial_path = partial_result.path;

            // Create failure receipt with stderr_tail and warnings (R4.3)
            // Use the actual packet evidence from the packet that was created
            let packet_evidence = packet.evidence.clone();

            let mut flags = HashMap::new();
            flags.insert("phase".to_string(), phase_id.as_str().to_string());

            let (model_alias, model_full_name) = if let Some(metadata) = &claude_metadata {
                (
                    metadata.model_alias.clone(),
                    metadata.model_full_name.clone(),
                )
            } else {
                (None, "haiku".to_string())
            };

            let mut warnings = vec!["Phase execution failed with non-zero exit code".to_string()];
            if let Some(ref warning) = llm_fallback_warning {
                warnings.push(warning.clone());
            }

            let mut receipt = self.receipt_manager().create_receipt_with_redactor(
                config.redactor.as_ref(),
                self.spec_id(),
                phase_id,
                claude_exit_code,
                vec![], // No successful outputs
                env!("CARGO_PKG_VERSION"),
                claude_metadata
                    .as_ref()
                    .map_or("0.8.1", |m| m.claude_cli_version.as_str()),
                &model_full_name,
                model_alias,
                flags,
                packet_evidence,
                Some("Claude CLI execution failed".to_string()), // stderr_tail
                None,                                            // stderr_redacted
                warnings,
                claude_metadata.as_ref().map(|m| m.fallback_used),
                claude_metadata
                    .as_ref()
                    .map_or("native", |m| m.runner.as_str()),
                claude_metadata
                    .as_ref()
                    .and_then(|m| m.runner_distro.clone()),
                Some(ErrorKind::ClaudeFailure),
                Some("Claude CLI execution failed".to_string()),
                None, // No diff_context
                pipeline_info.clone(),
            );

            receipt.llm = llm_result.map(|result| result.into_llm_info());

            let receipt_path = self.receipt_manager().write_receipt(&receipt)?;

            // Create enhanced error with stderr information (R4.3)
            let stderr_info = claude_metadata
                .as_ref()
                .and_then(|m| m.stderr_tail.clone())
                .unwrap_or_else(|| "No stderr captured".to_string());

            let enhanced_error = if !stderr_info.is_empty() && stderr_info != "No stderr captured" {
                XCheckerError::Phase(PhaseError::ExecutionFailedWithStderr {
                    phase: phase_id.as_str().to_string(),
                    code: claude_exit_code,
                    stderr_tail: stderr_info,
                })
            } else {
                XCheckerError::Phase(PhaseError::PartialOutputSaved {
                    phase: phase_id.as_str().to_string(),
                    partial_path: format!("artifacts/{partial_filename}"),
                })
            };

            return Ok(ExecutionResult {
                phase: phase_id,
                success: false,
                exit_code: claude_exit_code,
                artifact_paths: vec![partial_path.into_std_path_buf()], // Include partial artifact
                receipt_path: Some(receipt_path.into_std_path_buf()),
                error: Some(enhanced_error.to_string()),
            });
        }

        // Process Claude response
        let phase_result = phase
            .postprocess(&claude_response, &phase_context)
            .with_context(|| {
                format!(
                    "Failed to postprocess response for phase: {}",
                    phase_id.as_str()
                )
            })?;

        // Step 7: Write partial artifacts to .partial/ subdirectory (FR-ORC-004)
        let mut artifact_paths = Vec::new();
        let mut output_hashes = Vec::new();
        let mut atomic_write_warnings = Vec::new();

        for artifact in &phase_result.artifacts {
            // Store to .partial/ staging directory first
            let partial_result = self
                .artifact_manager()
                .store_partial_staged_artifact(artifact)
                .with_context(|| format!("Failed to store partial artifact: {}", artifact.name))?;

            // Collect atomic write warnings
            for warning in &partial_result.atomic_write_result.warnings {
                atomic_write_warnings.push(format!("{}: {}", artifact.name, warning));
            }

            // Create file hash for receipt (using final content)
            // Determine file type from extension for proper canonicalization
            let file_type = if let Some(ext) = std::path::Path::new(&artifact.name).extension() {
                FileType::from_extension(ext.to_str().unwrap_or(""))
            } else {
                // Fallback to artifact type if no extension
                match artifact.artifact_type {
                    ArtifactType::Markdown => FileType::Markdown,
                    ArtifactType::CoreYaml => FileType::Yaml,
                    _ => FileType::Text,
                }
            };

            let file_hash = self
                .receipt_manager()
                .create_file_hash(
                    &format!("artifacts/{}", artifact.name),
                    &artifact.content,
                    file_type,
                    phase_id.as_str(),
                )
                .map_err(|e| {
                    XCheckerError::Phase(PhaseError::OutputValidationFailed {
                        phase: phase_id.as_str().to_string(),
                        reason: e.to_string(),
                    })
                })?;

            output_hashes.push(file_hash);
        }

        // Step 8: Promote to final (atomic rename) (FR-ORC-004)
        for artifact in &phase_result.artifacts {
            let final_path = self
                .artifact_manager()
                .promote_staged_to_final(&artifact.name)
                .with_context(|| {
                    format!("Failed to promote artifact to final: {}", artifact.name)
                })?;

            artifact_paths.push(final_path.into_std_path_buf());
        }

        // Step 9: Create and write receipt (FR-ORC-005, FR-ORC-006)
        // Use the actual packet evidence from the packet that was created
        let packet_evidence = packet.evidence.clone();

        let mut flags = HashMap::new();
        flags.insert("phase".to_string(), phase_id.as_str().to_string());

        let (model_alias, model_full_name) = if let Some(metadata) = &claude_metadata {
            (
                metadata.model_alias.clone(),
                metadata.model_full_name.clone(),
            )
        } else {
            (None, "haiku".to_string())
        };

        let mut warnings: Vec<String> = atomic_write_warnings
            .into_iter()
            .chain(hook_warnings.iter().cloned())
            .collect();
        if let Some(warning) = llm_fallback_warning {
            warnings.push(warning);
        }

        let mut receipt = self.receipt_manager().create_receipt_with_redactor(
            config.redactor.as_ref(),
            self.spec_id(),
            phase_id,
            0, // Success exit code
            output_hashes,
            env!("CARGO_PKG_VERSION"),
            claude_metadata
                .as_ref()
                .map_or("0.8.1", |m| m.claude_cli_version.as_str()),
            &model_full_name,
            model_alias,
            flags,
            packet_evidence,
            None,     // No stderr_tail for successful execution
            None,     // No stderr_redacted for successful execution
            warnings, // Include atomic write warnings, hook warnings, and LLM fallback warning
            claude_metadata.as_ref().map(|m| m.fallback_used),
            claude_metadata
                .as_ref()
                .map_or("native", |m| m.runner.as_str()),
            claude_metadata
                .as_ref()
                .and_then(|m| m.runner_distro.clone()),
            None, // No error_kind for successful execution
            None, // No error_reason for successful execution
            None, // No diff_context
            pipeline_info.clone(),
        );
        // Set LLM info from the invocation result (V11+ multi-provider support)
        receipt.llm = llm_result.map(|r| r.into_llm_info());

        let receipt_path = self
            .receipt_manager()
            .write_receipt(&receipt)
            .with_context(|| format!("Failed to write receipt for phase: {}", phase_id.as_str()))?;

        // Execute post-phase hook if configured (runs on success)
        // Hooks run from invocation CWD so relative paths like ./scripts/... work
        // Note: Post-hook failures are treated as warnings, not phase failures
        // (artifacts have already been created and receipt written)
        if let Some(ref hooks_config) = config.hooks
            && let Some(hook_config) = hooks_config.get_post_phase_hook(phase_id)
        {
            let executor = HookExecutor::new(
                std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")),
            );
            let context = HookContext::new(self.spec_id(), phase_id, HookType::PostPhase);

            match execute_and_process_hook(
                &executor,
                hook_config,
                &context,
                HookType::PostPhase,
                phase_id,
            )
            .await
            {
                Ok(outcome) => {
                    // Log any warnings from successful or failed hooks
                    if let Some(warning) = outcome.warning() {
                        tracing::warn!(
                            phase = %phase_id.as_str(),
                            "Post-phase hook warning: {}",
                            warning.to_warning_string()
                        );
                    }
                    // Check if hook wanted to fail but we treat it as warning
                    // (post-hooks run after artifacts are created, so we don't fail the phase)
                    if !outcome.should_continue() {
                        tracing::warn!(
                            phase = %phase_id.as_str(),
                            "Post-phase hook had on_fail=fail but phase artifacts already created; treating as warning"
                        );
                    }
                }
                Err(e) => {
                    // Log hook execution errors but don't fail the phase
                    // (artifacts are already created at this point)
                    tracing::warn!(
                        phase = %phase_id.as_str(),
                        error = %e,
                        "Post-phase hook execution error (treated as warning)"
                    );
                }
            }
        }

        Ok(ExecutionResult {
            phase: phase_id,
            success: true,
            exit_code: 0,
            artifact_paths,
            receipt_path: Some(receipt_path.into_std_path_buf()),
            error: None,
        })
    }

    /// Create phase context for execution
    pub(crate) fn create_phase_context(
        &self,
        phase_id: PhaseId,
        config: &OrchestratorConfig,
    ) -> Result<PhaseContext> {
        // List available artifacts from previous phases
        let artifacts = self.artifact_manager().list_artifacts().map_err(|e| {
            XCheckerError::Phase(PhaseError::ContextCreationFailed {
                phase: phase_id.as_str().to_string(),
                reason: format!("Failed to list existing artifacts: {e}"),
            })
        })?;

        Ok(PhaseContext {
            spec_id: self.spec_id().to_string(),
            spec_dir: self
                .artifact_manager()
                .base_path()
                .clone()
                .into_std_path_buf(),
            config: config.config.clone(),
            artifacts,
            selectors: config.selectors.clone(),
            strict_validation: config.strict_validation,
            redactor: config.redactor.clone(),
        })
    }

    /// Check that phase dependencies are satisfied
    pub(crate) fn check_phase_dependencies(&self, phase: &dyn Phase) -> Result<()> {
        let deps = phase.deps();

        for dep_phase in deps {
            // Check if we have a successful receipt for the dependency
            if let Some(receipt) = self.receipt_manager().read_latest_receipt(*dep_phase)? {
                if receipt.exit_code != 0 {
                    return Err(XCheckerError::Phase(PhaseError::DependencyNotSatisfied {
                        phase: phase.id().as_str().to_string(),
                        dependency: dep_phase.as_str().to_string(),
                    })
                    .into());
                }
            } else {
                return Err(XCheckerError::Phase(PhaseError::DependencyNotSatisfied {
                    phase: phase.id().as_str().to_string(),
                    dependency: dep_phase.as_str().to_string(),
                })
                .into());
            }
        }

        Ok(())
    }

    /// Create a simulated LlmResult for dry-run mode
    /// This ensures receipts have complete LLM metadata even during testing
    pub(crate) fn simulate_llm_result(&self, _phase_id: PhaseId) -> crate::llm::LlmResult {
        crate::llm::LlmResult::new(
            "simulated response".to_string(),
            "claude-cli-simulated".to_string(),
            "haiku".to_string(),
        )
        .with_tokens(1000, 2000)
        .with_timeout(false)
        .with_timeout_seconds(600) // Default timeout for simulated runs
        .with_extension("dry_run", serde_json::json!(true))
    }

    /// Simulate Claude CLI response for testing/dry-run
    pub(crate) fn simulate_claude_response(&self, _phase_id: PhaseId, _prompt: &str) -> String {
        match _phase_id {
            PhaseId::Requirements => {
                // Generate a realistic requirements document
                r"# Requirements Document

## Introduction

This is a generated requirements document for the current specification. The system will provide core functionality for managing and processing specifications through a structured workflow.

## Requirements

### Requirement 1

**User Story:** As a developer, I want to generate structured requirements from rough ideas, so that I can create comprehensive specifications efficiently.

#### Acceptance Criteria

1. WHEN I provide a problem statement THEN the system SHALL generate structured requirements in EARS format
2. WHEN requirements are generated THEN they SHALL include user stories and acceptance criteria
3. WHEN the process completes THEN the system SHALL produce both markdown and YAML artifacts

### Requirement 2

**User Story:** As a developer, I want deterministic output generation, so that I can reproduce results consistently.

#### Acceptance Criteria

1. WHEN identical inputs are provided THEN the system SHALL produce identical canonicalized outputs
2. WHEN artifacts are created THEN they SHALL include BLAKE3 hashes for verification
3. WHEN the process runs THEN it SHALL create audit receipts for traceability

### Requirement 3

**User Story:** As a developer, I want atomic file operations, so that partial writes don't corrupt the system state.

#### Acceptance Criteria

1. WHEN writing artifacts THEN the system SHALL use atomic write operations
2. WHEN failures occur THEN partial artifacts SHALL be preserved for debugging
3. WHEN operations complete THEN all files SHALL be in a consistent state

## Non-Functional Requirements

**NFR1 Performance:** The system SHALL complete requirements generation within reasonable time limits
**NFR2 Reliability:** All file operations SHALL be atomic to prevent corruption
**NFR3 Auditability:** All operations SHALL be logged with cryptographic verification
".to_string()
            }
            PhaseId::Design => {
                r"# Design Document

## Overview

This is a comprehensive design document for the current specification. The system implements a phase-based architecture for orchestrating spec generation workflows using the Claude CLI.

## Architecture

The system follows a modular architecture with clear separation of concerns:

```mermaid
graph TD
    A[CLI Entry] --> B[Phase Orchestrator]
    B --> C[Requirements Phase]
    C --> D[Design Phase]
    D --> E[Tasks Phase]
    E --> F[Review Phase]
```

## Components and Interfaces

### Phase System
- **Phase trait**: Defines the interface for all workflow phases
- **PhaseOrchestrator**: Manages phase execution and dependencies
- **PhaseContext**: Provides context and configuration to phases

### Artifact Management
- **ArtifactManager**: Handles atomic file operations and storage
- **ReceiptManager**: Creates and manages execution receipts
- **Canonicalizer**: Ensures deterministic output formatting

## Data Models

### Core Types
- `PhaseId`: Enumeration of available phases
- `Artifact`: Represents generated outputs with metadata
- `Receipt`: Audit trail for phase execution

### Configuration
- `OrchestratorConfig`: Runtime configuration parameters
- `PhaseContext`: Execution context for phases

## Error Handling

The system implements comprehensive error handling with:
- Structured error types for different failure modes
- Partial artifact preservation on failures
- Detailed error reporting with context

## Testing Strategy

- Unit tests for individual components
- Integration tests for end-to-end workflows
- Property-based tests for determinism validation
- Mock Claude CLI for testing scenarios
".to_string()
            }
            PhaseId::Tasks => {
                r"# Implementation Plan

## Milestone 1: Core Phase System

- [ ] 1. Set up project structure and core interfaces
  - Create directory structure for phases, artifacts, and receipts
  - Define Phase trait with separated concerns (prompt, make_packet, postprocess)
  - Implement PhaseId enum and basic dependency system
  - _Requirements: R10.1, R10.3_

- [ ] 2. Implement Requirements phase
- [ ] 2.1 Create RequirementsPhase struct
  - Implement Phase trait methods for requirements generation
  - Create prompt template for EARS format requirements
  - Add packet construction with basic context
  - _Requirements: R1.1_

- [ ] 2.2 Add requirements postprocessing
  - Parse Claude response into requirements.md artifact
  - Generate requirements.core.yaml with structured data
  - Implement artifact creation and storage
  - _Requirements: R1.1, R2.1_

- [ ]* 2.3 Write unit tests for Requirements phase
  - Test prompt generation and packet creation
  - Verify postprocessing creates correct artifacts
  - Test error handling scenarios
  - _Requirements: R1.1_

## Milestone 2: Design and Tasks Phases

- [ ] 3. Implement Design phase
- [ ] 3.1 Create DesignPhase struct
  - Implement Phase trait with architecture-focused prompts
  - Add dependency on Requirements phase
  - Include requirements artifacts in packet construction
  - _Requirements: R1.1_

- [ ] 3.2 Add design postprocessing
  - Parse Claude response into design.md artifact
  - Generate design.core.yaml with structured data
  - Implement component and interface extraction
  - _Requirements: R1.1, R2.1_

- [ ] 4. Implement Tasks phase
- [ ] 4.1 Create TasksPhase struct
  - Implement Phase trait with implementation planning prompts
  - Add dependencies on Design and Requirements phases
  - Include all upstream artifacts in packet construction
  - _Requirements: R1.1_

- [ ] 4.2 Add tasks postprocessing
  - Parse Claude response into tasks.md artifact
  - Generate tasks.core.yaml with structured task data
  - Implement task parsing and validation
  - _Requirements: R1.1, R2.1_

- [ ]* 4.3 Write integration tests for phase system
  - Test Requirements → Design → Tasks flow
  - Verify dependency checking works correctly
  - Test artifact propagation between phases
  - _Requirements: R1.1, R4.2_

## Milestone 3: Orchestrator Integration

- [ ] 5. Update PhaseOrchestrator for new phases
- [ ] 5.1 Add execution methods for Design and Tasks phases
  - Implement execute_design_phase method
  - Implement execute_tasks_phase method
  - Update dependency checking logic
  - _Requirements: R1.1, R4.2_

- [ ] 5.2 Enhance Claude response simulation
  - Add realistic responses for Design phase
  - Add realistic responses for Tasks phase
  - Update test scenarios for all phases
  - _Requirements: R4.1_

- [ ]* 5.3 Write end-to-end integration tests
  - Test complete Requirements → Design → Tasks workflow
  - Verify artifact creation and receipt generation
  - Test error handling and partial artifact storage
  - _Requirements: R1.1, R4.3_
".to_string()
            }
            _ => {
                format!("Simulated response for phase: {}", _phase_id.as_str())
            }
        }
    }

    /// Get the phase number for artifact naming
    pub(crate) const fn get_phase_number(&self, phase_id: PhaseId) -> u8 {
        match phase_id {
            PhaseId::Requirements => 0,
            PhaseId::Design => 10,
            PhaseId::Tasks => 20,
            PhaseId::Review => 30,
            PhaseId::Fixup => 40,
            PhaseId::Final => 50,
        }
    }

    /// Get a phase implementation by ID (phase factory)
    /// This method creates the appropriate Phase trait object for the given phase ID
    pub(crate) fn get_phase_impl(
        &self,
        phase_id: PhaseId,
        config: &OrchestratorConfig,
    ) -> Result<Box<dyn Phase>> {
        match phase_id {
            PhaseId::Requirements => Ok(Box::new(RequirementsPhase::new())),
            PhaseId::Design => Ok(Box::new(DesignPhase::new())),
            PhaseId::Tasks => Ok(Box::new(TasksPhase::new())),
            PhaseId::Review => Ok(Box::new(ReviewPhase::new())),
            PhaseId::Fixup => {
                // Determine fixup mode from configuration (FR-FIX-004, FR-FIX-005)
                let apply_fixups = config
                    .config
                    .get("apply_fixups")
                    .is_some_and(|s| s == "true");

                let fixup_mode = if apply_fixups {
                    FixupMode::Apply
                } else {
                    FixupMode::Preview
                };

                Ok(Box::new(FixupPhase::new_with_mode(fixup_mode)))
            }
            PhaseId::Final => Err(anyhow::anyhow!("Final phase not yet implemented")),
        }
    }
}