ggen-core 26.7.3

Core graph-aware code generation engine
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
//! Staged Pipeline Orchestrator
//!
//! Orchestrates the complete v26_5_19 projection pipeline: A = μ(O)
//!
//! The pipeline runs passes in order:
//! 0. μ₀: Pack Resolution (bundle expansion, dependency resolution, ontology merge)
//! 1. μ₁: Normalization (CONSTRUCT)
//! 2. μ₂: Extraction (SELECT)
//! 3. μ₃: Emission (Tera)
//! 4. μ₄: Canonicalization (formatting)
//! 5. μ₅: Receipt (provenance)

use crate::graph::{Graph, GraphExport};
use crate::marketplace::trust::TrustTier;
use crate::pack_resolver::{PackResolver, ResolvedPacks};
use crate::pipeline_engine::epoch::Epoch;
use crate::pipeline_engine::guard::GuardSet;
use crate::pipeline_engine::pass::{Pass, PassContext, PassExecution, PassResult};
use crate::pipeline_engine::passes::{
    CanonicalizationPass, EmissionPass, ExtractionPass, NormalizationPass, ReceiptGenerationPass,
};
use crate::pipeline_engine::receipt::{
    BuildReceipt, BundleExpansionRef, OutputFile, PackProvenance, ReceiptPolicies,
};
use crate::pipeline_engine::vocabulary::VocabularyRegistry;
use crate::utils::error::{Error, Result};
use oxigraph::io::RdfFormat;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
use std::time::Instant;

/// Stage pack templates into `<project>/.ggen/pack-stage/` for μ₃ rules or inspection.
#[allow(dead_code)]
fn stage_pack_templates(
    base_path: &Path, templates: &[crate::pack_resolver::TemplateDef],
) -> Result<Vec<String>> {
    let stage = base_path.join(".ggen").join("pack-stage");
    let mut names = Vec::new();
    for t in templates {
        let dest = stage.join(&t.path);
        if let Some(p) = dest.parent() {
            std::fs::create_dir_all(p)
                .map_err(|e| Error::new(&format!("Failed to create pack-stage dir: {}", e)))?;
        }
        std::fs::write(&dest, &t.content)
            .map_err(|e| Error::new(&format!("Failed to write staged template: {}", e)))?;
        names.push(t.path.display().to_string());
    }
    Ok(names)
}

/// Verification mode for the pipeline
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum VerifyMode {
    /// Don't verify, just generate
    #[default]
    None,
    /// Verify inputs match epoch before running
    VerifyInputs,
    /// Verify outputs match receipt after running
    VerifyOutputs,
    /// Verify both inputs and outputs
    Full,
}

/// Pipeline configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PipelineConfig {
    /// Project name
    pub project_name: String,

    /// Project version
    pub project_version: String,

    /// Base path for the project
    pub base_path: PathBuf,

    /// Ontology source files
    pub ontology_sources: Vec<PathBuf>,

    /// Output directory for generated files
    pub output_dir: PathBuf,

    /// Path to write receipt
    pub receipt_path: Option<PathBuf>,

    /// Verification mode
    pub verify_mode: VerifyMode,

    /// Previous receipt for verification
    pub previous_receipt: Option<PathBuf>,

    /// ggen version
    pub toolchain_version: String,

    /// Enable μ₀ pack resolution stage
    #[serde(default)]
    pub enable_pack_resolution: bool,
}

impl PipelineConfig {
    /// Create a new pipeline configuration
    pub fn new(project_name: impl Into<String>, project_version: impl Into<String>) -> Self {
        Self {
            project_name: project_name.into(),
            project_version: project_version.into(),
            base_path: PathBuf::from("."),
            ontology_sources: Vec::new(),
            output_dir: PathBuf::from("."),
            receipt_path: Some(PathBuf::from(".ggen/receipt.json")),
            verify_mode: VerifyMode::None,
            previous_receipt: None,
            toolchain_version: env!("CARGO_PKG_VERSION").to_string(),
            enable_pack_resolution: true,
        }
    }

    /// Set base path
    pub fn with_base_path(mut self, path: impl Into<PathBuf>) -> Self {
        self.base_path = path.into();
        self
    }

    /// Add an ontology source
    pub fn with_ontology(mut self, path: impl Into<PathBuf>) -> Self {
        self.ontology_sources.push(path.into());
        self
    }

    /// Add multiple ontology sources
    pub fn with_ontologies(mut self, paths: Vec<PathBuf>) -> Self {
        self.ontology_sources.extend(paths);
        self
    }

    /// Set output directory
    pub fn with_output_dir(mut self, path: impl Into<PathBuf>) -> Self {
        self.output_dir = path.into();
        self
    }

    /// Set receipt path
    pub fn with_receipt_path(mut self, path: impl Into<PathBuf>) -> Self {
        self.receipt_path = Some(path.into());
        self
    }

    /// Set verification mode
    pub fn with_verify_mode(mut self, mode: VerifyMode) -> Self {
        self.verify_mode = mode;
        self
    }

    /// Set previous receipt for verification
    pub fn with_previous_receipt(mut self, path: impl Into<PathBuf>) -> Self {
        self.previous_receipt = Some(path.into());
        self
    }
}

/// The staged compilation pipeline
pub struct StagedPipeline {
    /// Configuration
    config: PipelineConfig,

    /// Loaded RDF graph
    graph: Graph,

    /// Input epoch
    epoch: Option<Epoch>,

    /// Executed passes
    executed_passes: Vec<PassExecution>,

    /// Generated files
    generated_files: Vec<PathBuf>,

    /// Vocabulary registry for governance
    vocabulary_registry: VocabularyRegistry,

    /// Guard set for output validation
    guard_set: GuardSet,

    /// μ₀: Pack resolver
    pack_resolver: Option<PackResolver>,

    /// μ₀: Resolved packs (after bundle expansion and dependency resolution)
    resolved_packs: Option<ResolvedPacks>,

    /// Normalization pass
    normalization: NormalizationPass,

    /// Extraction pass
    extraction: ExtractionPass,

    /// Emission pass
    emission: EmissionPass,

    /// Canonicalization pass
    canonicalization: CanonicalizationPass,

    /// Receipt generation pass
    #[allow(dead_code)]
    receipt_gen: ReceiptGenerationPass,
}

impl StagedPipeline {
    /// Create a new staged pipeline
    pub fn new(config: PipelineConfig) -> Result<Self> {
        let graph = Graph::new()?;

        // Initialize pack resolver if lockfile exists
        let pack_resolver = PackResolver::new(&config.base_path).ok();

        let mut receipt_gen = ReceiptGenerationPass::new(&config.toolchain_version);
        if let Some(path) = &config.receipt_path {
            receipt_gen = receipt_gen.with_receipt_path(path.clone());
        }

        Ok(Self {
            config: config.clone(),
            graph,
            epoch: None,
            executed_passes: Vec::new(),
            generated_files: Vec::new(),
            vocabulary_registry: VocabularyRegistry::with_standard_vocabularies(),
            guard_set: GuardSet::default_v26(),
            pack_resolver,
            resolved_packs: None,
            normalization: NormalizationPass::with_standard_rules(),
            extraction: ExtractionPass::with_standard_rules(),
            emission: EmissionPass::new(),
            canonicalization: CanonicalizationPass::new(),
            receipt_gen,
        })
    }

    /// Set custom normalization pass
    pub fn with_normalization(mut self, pass: NormalizationPass) -> Self {
        self.normalization = pass;
        self
    }

    /// Set custom extraction pass
    pub fn with_extraction(mut self, pass: ExtractionPass) -> Self {
        self.extraction = pass;
        self
    }

    /// Set custom emission pass
    pub fn with_emission(mut self, pass: EmissionPass) -> Self {
        self.emission = pass;
        self
    }

    /// Set vocabulary registry
    pub fn with_vocabulary_registry(mut self, registry: VocabularyRegistry) -> Self {
        self.vocabulary_registry = registry;
        self
    }

    /// Set guard set
    pub fn with_guards(mut self, guards: GuardSet) -> Self {
        self.guard_set = guards;
        self
    }

    /// Load ontology sources and create epoch
    pub fn load_ontologies(&mut self) -> Result<&Epoch> {
        // Create epoch from ontology sources
        self.epoch = Some(Epoch::create(
            &self.config.base_path,
            &self.config.ontology_sources,
        )?);

        // Load each ontology file into the graph
        for source in &self.config.ontology_sources {
            let full_path = self.config.base_path.join(source);
            let content = std::fs::read_to_string(&full_path).map_err(|e| {
                Error::new(&format!(
                    "Failed to read ontology '{}': {}",
                    full_path.display(),
                    e
                ))
            })?;

            // Validate vocabulary governance
            let namespaces = VocabularyRegistry::extract_namespaces(&content);
            self.vocabulary_registry.validate_namespaces(&namespaces)?;

            // Load into graph
            self.graph.insert_turtle(&content)?;
        }

        self.epoch
            .as_ref()
            .ok_or_else(|| Error::new("Pipeline epoch is not initialized"))
    }

    /// Get resolved packs from μ₀ stage
    pub fn resolved_packs(&self) -> Option<&ResolvedPacks> {
        self.resolved_packs.as_ref()
    }

    /// Verify inputs match a previous epoch
    pub fn verify_inputs(&self, previous_epoch: &Epoch) -> Result<bool> {
        if let Some(ref epoch) = self.epoch {
            Ok(epoch.id == previous_epoch.id)
        } else {
            Err(Error::new("No epoch loaded. Call load_ontologies() first."))
        }
    }

    /// Run a single pass and record execution
    #[allow(dead_code)]
    fn run_pass<P: Pass>(&mut self, pass: &P, ctx: &mut PassContext<'_>) -> Result<PassResult> {
        let start = Instant::now();
        let result = pass.execute(ctx)?;
        let duration = start.elapsed();

        // Record execution
        let mut execution = pass.create_execution_record(&result);
        execution.duration_ms = duration.as_millis() as u64;
        self.executed_passes.push(execution);

        Ok(result)
    }

    /// Run the complete pipeline
    ///
    /// # Returns
    /// * `Ok(BuildReceipt)` - Pipeline completed successfully
    /// * `Err(Error)` - Pipeline failed at some stage
    pub fn run(&mut self) -> Result<BuildReceipt> {
        let _pipeline_start = Instant::now();

        // μ₀: Pack Resolution (if lockfile exists)
        if let Some(ref resolver) = self.pack_resolver {
            match resolver.resolve() {
                Ok(resolved) => {
                    // Use merged ontology from resolved packs
                    self.graph = resolved.merged_ontology.clone();
                    self.resolved_packs = Some(resolved);
                }
                Err(e) if e.to_string().contains("Lockfile not found") => {
                    // No lockfile - skip pack resolution, continue with project ontologies
                    tracing::debug!("No lockfile found, skipping pack resolution");
                }
                Err(e) => {
                    // Other errors should propagate
                    return Err(e);
                }
            }
        }

        // Synthetic epoch when only pack substrate is present (no project TTL epoch yet)
        if self.epoch.is_none() && self.resolved_packs.is_some() {
            let turtle = GraphExport::new(&self.graph)
                .write_to_string(RdfFormat::Turtle)
                .map_err(|e| Error::new(&format!("Pack graph export failed: {}", e)))?;
            let digest = format!("{:x}", Sha256::digest(turtle.as_bytes()));
            let triple_count = self.graph.len();
            self.epoch = Some(Epoch::from_pack_merged_substrate(digest, triple_count));
        }

        // Load ontologies if not already loaded (skip if μ₀ provided merged ontology)
        if self.epoch.is_none() && self.resolved_packs.is_none() {
            self.load_ontologies()?;
        }

        // Verify inputs if requested
        if matches!(
            self.config.verify_mode,
            VerifyMode::VerifyInputs | VerifyMode::Full
        ) {
            if let Some(ref receipt_path) = self.config.previous_receipt {
                let previous_receipt = BuildReceipt::read_from_file(receipt_path)?;
                // Check if epoch matches
                if self.epoch.as_ref().map(|e| &e.id) != Some(&previous_receipt.epoch_id) {
                    return Err(Error::new(
                        "Input epoch does not match previous receipt. Inputs have changed.",
                    ));
                }
            }
        }

        // Create output directory
        let output_dir = self.config.base_path.join(&self.config.output_dir);
        std::fs::create_dir_all(&output_dir).map_err(|e| {
            Error::new(&format!(
                "Failed to create output directory '{}': {}",
                output_dir.display(),
                e
            ))
        })?;

        // Clone passes to avoid borrow issues
        let normalization = self.normalization.clone();
        let mut extraction = self.extraction.clone();
        let mut emission = self.emission.clone();

        // Load pack queries into μ₂ and pack templates into μ₃
        if let Some(ref rp) = self.resolved_packs {
            // Load pack queries into μ₂ (CONSTRUCT-only queries from packs)
            extraction.extend_with_pack_construct_queries(&rp.queries)?;
            // Load pack templates into μ₃ (Tera templates from packs)
            emission.extend_with_pack_templates(&rp.templates)?;
        }
        let canonicalization = self.canonicalization.clone();

        // Create pass context
        let mut ctx = PassContext::new(&self.graph, self.config.base_path.clone(), output_dir)
            .with_project(
                self.config.project_name.clone(),
                self.config.project_version.clone(),
            );

        // μ₁: Normalization (pipeline.load — load RDF ontology into normalized graph)
        let start = Instant::now();
        let span = tracing::info_span!(
            "pipeline.load",
            "operation.name" = "pipeline.load",
            "operation.type" = "pipeline",
            "pipeline.stage" = "mu1",
        );
        let guard = span.enter();
        let norm_result = normalization.execute(&mut ctx)?;
        let mut execution = normalization.create_execution_record(&norm_result);
        execution.duration_ms = start.elapsed().as_millis() as u64;
        self.executed_passes.push(execution);
        if !norm_result.success {
            return Err(Error::new(&format!(
                "Normalization failed: {:?}",
                norm_result.error
            )));
        }
        let elapsed = start.elapsed();
        tracing::Span::current().record("pipeline.duration_ms", elapsed.as_millis() as u64);
        tracing::info!(
            stage = "mu1",
            elapsed_ms = elapsed.as_millis() as u64,
            "Pipeline stage completed"
        );
        drop(guard);

        // μ₂: Extraction (pipeline.extract — extract skill definitions via SELECT)
        let start = Instant::now();
        let span = tracing::info_span!(
            "pipeline.extract",
            "operation.name" = "pipeline.extract",
            "operation.type" = "pipeline",
            "pipeline.stage" = "mu2",
        );
        let guard = span.enter();
        let extract_result = extraction.execute(&mut ctx)?;
        let mut execution = extraction.create_execution_record(&extract_result);
        execution.duration_ms = start.elapsed().as_millis() as u64;
        self.executed_passes.push(execution);
        if !extract_result.success {
            return Err(Error::new(&format!(
                "Extraction failed: {:?}",
                extract_result.error
            )));
        }
        let elapsed = start.elapsed();
        tracing::Span::current().record("pipeline.duration_ms", elapsed.as_millis() as u64);
        tracing::info!(
            stage = "mu2",
            elapsed_ms = elapsed.as_millis() as u64,
            "Pipeline stage completed"
        );
        drop(guard);

        // Stage pack templates under .ggen/pack-stage/ (μ₃ rules may reference these paths)
        if let Some(ref rp) = self.resolved_packs {
            let _staged = stage_pack_templates(&self.config.base_path, &rp.templates)?;
        }

        // μ₃: Emission (pipeline.generate — generate code via Tera templates)
        let start = Instant::now();
        let span = tracing::info_span!(
            "pipeline.generate",
            "operation.name" = "pipeline.generate",
            "operation.type" = "pipeline",
            "pipeline.stage" = "mu3",
        );
        let guard = span.enter();

        let emission_result = emission.execute(&mut ctx)?;
        let mut execution = emission.create_execution_record(&emission_result);
        execution.duration_ms = start.elapsed().as_millis() as u64;
        self.executed_passes.push(execution);
        if !emission_result.success {
            return Err(Error::new(&format!(
                "Emission failed: {:?}",
                emission_result.error
            )));
        }
        let elapsed = start.elapsed();
        tracing::Span::current().record("pipeline.duration_ms", elapsed.as_millis() as u64);
        tracing::info!(
            stage = "mu3",
            elapsed_ms = elapsed.as_millis() as u64,
            "Pipeline stage completed"
        );
        drop(guard);

        // μ₄: Canonicalization (pipeline.validate — quality gate validation)
        let start = Instant::now();
        let span = tracing::info_span!(
            "pipeline.validate",
            "operation.name" = "pipeline.validate",
            "operation.type" = "pipeline",
            "pipeline.stage" = "mu4",
        );
        let guard = span.enter();
        let canon_result = canonicalization.execute(&mut ctx)?;
        let mut execution = canonicalization.create_execution_record(&canon_result);
        execution.duration_ms = start.elapsed().as_millis() as u64;
        self.executed_passes.push(execution);
        if !canon_result.success {
            return Err(Error::new(&format!(
                "Canonicalization failed: {:?}",
                canon_result.error
            )));
        }
        let elapsed = start.elapsed();
        tracing::Span::current().record("pipeline.duration_ms", elapsed.as_millis() as u64);
        tracing::info!(
            stage = "mu4",
            elapsed_ms = elapsed.as_millis() as u64,
            "Pipeline stage completed"
        );
        drop(guard);

        // Collect generated files
        self.generated_files = ctx.generated_files.clone();

        // Create output file records
        let output_records = self.create_output_records(&ctx)?;

        // μ₅: Receipt generation (pipeline.emit — write generated files + provenance receipt)
        let start = Instant::now();
        let span = tracing::info_span!(
            "pipeline.emit",
            "operation.name" = "pipeline.emit",
            "operation.type" = "pipeline",
            "pipeline.stage" = "mu5",
        );
        let guard = span.enter();

        let epoch = self
            .epoch
            .as_ref()
            .ok_or_else(|| Error::new("Pipeline epoch is not initialized"))?;
        let mut receipt = BuildReceipt::new(
            epoch,
            self.executed_passes.clone(),
            output_records,
            &self.config.toolchain_version,
        )
        .with_policies(ReceiptPolicies {
            blank_node_policy: "canonicalize".to_string(),
            ordering_policy: "deterministic".to_string(),
            formatting_policy: "language-specific".to_string(),
            active_guards: vec!["path-guard".to_string(), "secret-guard".to_string()],
        });

        // Add pack provenance to receipt (if μ₀ resolved packs)
        if let Some(ref resolved) = self.resolved_packs {
            // Add bundle expansions
            for expansion in &resolved.bundle_expansions {
                receipt.add_bundle_expansion(BundleExpansionRef {
                    bundle_id: expansion.bundle_id.clone(),
                    expanded_to: expansion.expanded_to.clone(),
                });
            }

            // Add pack provenance
            for pack_id in &resolved.atomic_packs {
                let version = resolved
                    .pack_versions
                    .get(&pack_id.to_string())
                    .cloned()
                    .unwrap_or_else(|| "unknown".to_string());

                receipt.add_pack(PackProvenance {
                    pack_id: pack_id.to_string(),
                    version,
                    signature: resolved
                        .pack_signatures
                        .get(&pack_id.to_string())
                        .cloned()
                        .unwrap_or_else(|| "local:unsigned".to_string()),
                    digest: resolved.digest_for_pack(pack_id),
                    registry_type: resolved
                        .pack_registry_types
                        .get(&pack_id.to_string())
                        .cloned(),
                    origin_url: resolved.pack_origin_urls.get(&pack_id.to_string()).cloned(),
                    templates_contributed: resolved.template_paths_for_pack(pack_id),
                    queries_contributed: resolved.query_names_for_pack(pack_id),
                    files_generated: vec![],
                });
            }

            receipt.set_profile(crate::pipeline_engine::receipt::ProfileRef {
                profile_id: resolved.profile.clone(),
                runtime_constraints: vec![],
                trust_requirement: TrustTier::Experimental,
            });

            // Close O*: bind the CONTENT of every template (μ₃) and SPARQL
            // query (μ₂) into the receipt. The epoch / `ontology_hash` covers
            // only the ontology substrate; templates and queries equally
            // determine the artifact, so a verifier must see them in the
            // witnessed closure. Without this, editing a template would leave
            // every receipt hash unchanged (contract drift).
            for q in &resolved.queries {
                receipt.add_closure_input(&format!("query:{}", q.name), q.sparql.as_bytes());
            }
            for t in &resolved.templates {
                receipt.add_closure_input(
                    &format!("template:{}", t.path.display()),
                    t.content.as_bytes(),
                );
            }
        }

        // Finalize the receipt id now that the full closure (ontology epoch +
        // template/query content + actuator identity) is bound.
        receipt.recompute_id();

        // Dry-run gating note: this core layer has no `dry_run` concept. A
        // receipt is emitted ONLY when `receipt_path` is `Some`; a preview run
        // sets it to `None` (and writes no artifacts), so no receipt for a
        // never-happened actuation can be produced here. The CLI dry-run gate
        // lives at the only call site — `ggen-cli sync` skips receipt emission
        // entirely on `--dry_run` (crates/ggen-cli/src/cmds/sync.rs).
        // Write receipt if path specified
        if let Some(ref receipt_path) = self.config.receipt_path {
            let full_receipt_path = self.config.base_path.join(receipt_path);
            if let Some(parent) = full_receipt_path.parent() {
                std::fs::create_dir_all(parent)?;
            }
            receipt.write_to_file(&full_receipt_path)?;
        }

        // Verify outputs if requested
        if matches!(
            self.config.verify_mode,
            VerifyMode::VerifyOutputs | VerifyMode::Full
        ) {
            let output_dir = self.config.base_path.join(&self.config.output_dir);
            if !receipt.verify_outputs(&output_dir)? {
                return Err(Error::new(
                    "Output verification failed. hash(A) ≠ hash(μ(O))",
                ));
            }
        }

        let elapsed = start.elapsed();
        tracing::Span::current().record("pipeline.duration_ms", elapsed.as_millis() as u64);
        tracing::info!(
            stage = "mu5",
            elapsed_ms = elapsed.as_millis() as u64,
            "Pipeline stage completed"
        );
        drop(guard);

        Ok(receipt)
    }

    /// Create output file records from generated files
    fn create_output_records(&self, ctx: &PassContext<'_>) -> Result<Vec<OutputFile>> {
        let mut outputs = Vec::new();

        for rel_path in &ctx.generated_files {
            let full_path = ctx.output_dir.join(rel_path);

            if !full_path.exists() {
                continue;
            }

            outputs.push(OutputFile::from_path(
                &full_path,
                rel_path.clone(),
                "μ₃:emission",
            )?);
        }

        Ok(outputs)
    }

    /// Get the loaded epoch
    pub fn epoch(&self) -> Option<&Epoch> {
        self.epoch.as_ref()
    }

    /// Get executed passes
    pub fn executed_passes(&self) -> &[PassExecution] {
        &self.executed_passes
    }

    /// Get generated files
    pub fn generated_files(&self) -> &[PathBuf] {
        &self.generated_files
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pipeline_engine::vocabulary::AllowedVocabulary;
    use tempfile::TempDir;

    #[test]
    fn test_pipeline_config_builder() {
        let config = PipelineConfig::new("test", "1.0.0")
            .with_ontology("ontology/domain.ttl")
            .with_output_dir(".")
            .with_verify_mode(VerifyMode::Full);

        assert_eq!(config.project_name, "test");
        assert_eq!(config.project_version, "1.0.0");
        assert_eq!(config.ontology_sources.len(), 1);
        assert_eq!(config.verify_mode, VerifyMode::Full);
    }

    #[test]
    fn test_pipeline_creation() {
        let config = PipelineConfig::new("test", "1.0.0");
        let pipeline = StagedPipeline::new(config);
        assert!(pipeline.is_ok());
    }

    #[test]
    fn test_pipeline_empty_run() {
        let temp_dir = TempDir::new().unwrap();

        // Create a minimal ontology
        let ontology_dir = temp_dir.path().join("ontology");
        std::fs::create_dir_all(&ontology_dir).unwrap();
        std::fs::write(
            ontology_dir.join("domain.ttl"),
            r#"
            @prefix ex: <http://example.org/> .
            @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

            ex:Person a rdfs:Class ;
                rdfs:label "Person" .
            "#,
        )
        .unwrap();

        let config = PipelineConfig::new("test", "1.0.0")
            .with_base_path(temp_dir.path())
            .with_ontology(PathBuf::from("ontology/domain.ttl"))
            .with_output_dir("output");

        let mut pipeline = StagedPipeline::new(config).unwrap();

        // Add example.org to allowed vocabularies for testing
        let mut registry = VocabularyRegistry::with_standard_vocabularies();
        registry.add_allowed(
            AllowedVocabulary::new("http://example.org/", "ex")
                .with_description("Example namespace for testing"),
        );
        pipeline = pipeline.with_vocabulary_registry(registry);

        let receipt = pipeline.run().unwrap();

        assert!(receipt.is_valid);
        assert_eq!(receipt.toolchain_version, env!("CARGO_PKG_VERSION"));
    }
}