ggen-core 26.6.11

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
//! Phase execution with hooks and state management
//!
//! This module provides the core execution engine for lifecycle phases, including
//! hook execution (before/after), state persistence, and deterministic caching.
//!
//! ## Features
//!
//! - **Phase execution**: Run lifecycle phases with command execution
//! - **Hook support**: Execute before/after hooks for phases
//! - **State persistence**: Track phase history and generated files
//! - **Deterministic caching**: Cache key generation for reproducible builds
//! - **Thread-safe**: Support for parallel workspace execution
//!
//! ## Examples
//!
//! ### Running a Phase
//!
//! ```rust,no_run
//! use crate::lifecycle::exec::Context;
//! use crate::lifecycle::exec::run_phase;
//! use crate::lifecycle::Result;
//! use std::path::PathBuf;
//! use std::sync::Arc;
//!
//! # fn main() -> Result<()> {
//! // Create execution context
//! let root = PathBuf::from(".");
//! let make = Arc::new(crate::lifecycle::loader::load_make("make.toml")?);
//! let state_path = root.join(".ggen/state.json");
//! let ctx = Context::new(root, make, state_path, vec![]);
//!
//! // Run a phase
//! run_phase(&ctx, "build")?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Running Multiple Phases
//!
//! ```rust,no_run
//! use crate::lifecycle::exec::run_pipeline;
//! use crate::lifecycle::Result;
//! use std::path::PathBuf;
//! use std::sync::Arc;
//!
//! # fn main() -> Result<()> {
//! // Create context (same as above)
//! let root = PathBuf::from(".");
//! let make = Arc::new(crate::lifecycle::loader::load_make("make.toml")?);
//! let state_path = root.join(".ggen/state.json");
//! let ctx = crate::lifecycle::exec::Context::new(root, make, state_path, vec![]);
//!
//! // Run multiple phases in sequence
//! run_pipeline(&ctx, &vec!["test".to_string(), "lint".to_string(), "build".to_string()])?;
//! # Ok(())
//! # }
//! ```

use super::{cache::cache_key, error::*, loader::load_make, model::*, state::*};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::Instant;

use std::collections::HashSet;
use std::sync::{Arc, Mutex};

/// Execution context for lifecycle phases (thread-safe)
pub struct Context {
    pub root: PathBuf,
    pub make: Arc<Make>,
    pub state_path: PathBuf,
    pub env: Vec<(String, String)>,
    /// Hook recursion guard (thread-safe for parallel execution)
    hook_guard: Arc<Mutex<HashSet<String>>>,
    /// Call chain tracking for better error messages (thread-safe)
    /// **DfLSS Fix**: Tracks full call chain to show complete cycle path in error messages
    call_chain: Arc<Mutex<Vec<String>>>,
}

impl Context {
    /// Create new context with empty hook guard and call chain
    pub fn new(
        root: PathBuf, make: Arc<Make>, state_path: PathBuf, env: Vec<(String, String)>,
    ) -> Self {
        Self {
            root,
            make,
            state_path,
            env,
            hook_guard: Arc::new(Mutex::new(HashSet::new())),
            call_chain: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Check for hook recursion and add to guard
    ///
    /// **DfLSS Fix**: Now tracks full call chain for better error messages
    fn enter_phase(&self, phase: &str) -> Result<()> {
        let mut guard = self
            .hook_guard
            .lock()
            .map_err(|_| LifecycleError::MutexPoisoned {
                phase: phase.to_string(),
            })?;

        let mut chain = self
            .call_chain
            .lock()
            .map_err(|_| LifecycleError::MutexPoisoned {
                phase: phase.to_string(),
            })?;

        if guard.contains(phase) {
            // Build cycle chain: find where cycle starts and build full path
            let cycle_start = chain.iter().position(|p| p == phase).unwrap_or(0);
            let mut cycle = chain[cycle_start..].to_vec();
            cycle.push(phase.to_string());

            return Err(LifecycleError::hook_recursion_with_chain(
                phase.to_string(),
                cycle,
            ));
        }

        guard.insert(phase.to_string());
        chain.push(phase.to_string());
        Ok(())
    }

    /// Remove phase from guard after completion
    ///
    /// **DfLSS Fix**: Also removes phase from call chain
    fn exit_phase(&self, phase: &str) {
        match self.hook_guard.lock() {
            Ok(mut guard) => {
                guard.remove(phase);
            }
            Err(e) => {
                // Log mutex poisoning for debugging, but don't fail
                // If mutex is poisoned, we're in a panic scenario anyway
                tracing::error!(
                    phase = %phase,
                    error = %e,
                    "CRITICAL: Hook guard mutex poisoned - system may be in inconsistent state"
                );
            }
        }

        // Remove from call chain
        match self.call_chain.lock() {
            Ok(mut chain) => {
                if chain.last().map(|s| s.as_str()) == Some(phase) {
                    chain.pop();
                }
            }
            Err(e) => {
                tracing::error!(
                    phase = %phase,
                    error = %e,
                    "CRITICAL: Call chain mutex poisoned - system may be in inconsistent state"
                );
            }
        }
    }
}

/// Run a single lifecycle phase with hooks
#[tracing::instrument(name = "ggen.lifecycle.phase", skip(ctx), fields(phase = phase_name, duration_ms, status))]
pub fn run_phase(ctx: &Context, phase_name: &str) -> Result<()> {
    tracing::info!(phase = phase_name, "lifecycle phase starting");

    // Check for hook recursion FIRST
    ctx.enter_phase(phase_name)?;

    // Ensure we exit phase even on error
    let result = run_phase_internal(ctx, phase_name);
    ctx.exit_phase(phase_name);

    // Record status in span
    match &result {
        Ok(()) => {
            tracing::Span::current().record("status", "success");
            tracing::info!(phase = phase_name, "lifecycle phase completed");
        }
        Err(e) => {
            tracing::Span::current().record("status", "error");
            tracing::error!(phase = phase_name, error = %e, "lifecycle phase failed");
        }
    }

    result
}

/// Internal phase execution (called after recursion check)
fn run_phase_internal(ctx: &Context, phase_name: &str) -> Result<()> {
    let phase = ctx
        .make
        .lifecycle
        .get(phase_name)
        .ok_or_else(|| LifecycleError::phase_not_found(phase_name))?;

    // Run before hooks first
    run_before_hooks(ctx, phase_name)?;

    // Print phase start message for CLI output (after hooks)
    crate::alert_info!(&format!("Running phase: {}", phase_name));

    // Get commands for this phase using new Phase::commands() method
    let cmds = phase.commands();
    if cmds.is_empty() {
        tracing::warn!(phase = %phase_name, "Phase has no commands");
        return Ok(());
    }

    // Generate cache key
    let key = cache_key(phase_name, &cmds, &ctx.env, &[]);

    // Execute phase commands
    let started = current_time_ms()?;
    let timer = Instant::now();

    tracing::info!(phase = %phase_name, "Starting phase execution");
    for cmd in &cmds {
        tracing::debug!(phase = %phase_name, command = %cmd, "Executing command");
        execute_command(cmd, &ctx.root, &ctx.env)?;
    }

    let duration = timer.elapsed().as_millis();

    // Record duration in parent span
    tracing::Span::current().record("duration_ms", duration);

    tracing::info!(
        phase = %phase_name,
        duration_ms = duration,
        "Phase completed successfully"
    );

    // Update state with validation (poka-yoke)
    let mut state = load_state(&ctx.state_path)?;
    state.record_run(phase_name.to_string(), started, duration, true);
    state.add_cache_key(phase_name.to_string(), key);

    // Validate state before saving (poka-yoke)
    use super::state_validation::ValidatedLifecycleState;
    let validated_state = ValidatedLifecycleState::new(state.clone())
        .map_err(|e| LifecycleError::Other(format!("State validation failed: {}", e)))?;

    save_state(&ctx.state_path, validated_state.state())?;

    // Run after hooks
    run_after_hooks(ctx, phase_name)?;

    Ok(())
}

/// Run a pipeline of phases sequentially
#[tracing::instrument(name = "ggen.lifecycle.pipeline", skip(ctx), fields(phases = ?phases, phase_count = phases.len()))]
pub fn run_pipeline(ctx: &Context, phases: &[String]) -> Result<()> {
    tracing::info!(phases = ?phases, "starting lifecycle pipeline");
    if let Some(workspaces) = &ctx.make.workspace {
        // Check if parallel execution is requested
        let parallel = phases
            .first()
            .and_then(|p| ctx.make.lifecycle.get(p))
            .and_then(|ph| ph.parallel)
            .unwrap_or(false);

        if parallel {
            // PRODUCTION FIX: Bounded thread pool to prevent resource exhaustion
            use rayon::prelude::*;
            use rayon::ThreadPoolBuilder;

            // Limit to max 8 threads to prevent fork bomb with many workspaces
            let max_threads = 8.min(num_cpus::get());
            let pool = ThreadPoolBuilder::new()
                .num_threads(max_threads)
                .build()
                .map_err(|e| {
                    LifecycleError::Other(format!("Failed to create thread pool: {}", e))
                })?;

            let results: Vec<Result<()>> = pool.install(|| {
                workspaces
                    .par_iter()
                    .map(|(ws_name, workspace)| {
                        tracing::info!(workspace = %ws_name, "Processing workspace");
                        let ws_ctx = create_workspace_context(
                            &ctx.root, ws_name, workspace, &ctx.make, &ctx.env,
                        )?;

                        for phase in phases {
                            run_phase(&ws_ctx, phase)?;
                        }
                        Ok(())
                    })
                    .collect()
            });

            // Check for errors
            for result in results {
                result?;
            }
        } else {
            // Sequential execution
            for (ws_name, workspace) in workspaces {
                tracing::info!(workspace = %ws_name, "Processing workspace");
                let ws_ctx =
                    create_workspace_context(&ctx.root, ws_name, workspace, &ctx.make, &ctx.env)?;

                for phase in phases {
                    run_phase(&ws_ctx, phase)?;
                }
            }
        }
    } else {
        // No workspaces, run directly
        for phase in phases {
            run_phase(ctx, phase)?;
        }
    }

    Ok(())
}

/// Create workspace context with security validation
///
/// SECURITY: Validates workspace paths to prevent directory traversal attacks
fn create_workspace_context(
    root: &Path, ws_name: &str, workspace: &super::model::Workspace, root_make: &Arc<Make>,
    env: &[(String, String)],
) -> Result<Context> {
    let ws_path = root.join(&workspace.path);

    // SECURITY: Canonicalize and validate paths to prevent traversal
    let canonical_root = root
        .canonicalize()
        .map_err(|e| LifecycleError::Other(format!("Failed to canonicalize root path: {}", e)))?;

    let canonical_ws = ws_path.canonicalize().map_err(|e| {
        LifecycleError::Other(format!(
            "Failed to canonicalize workspace '{}' path: {}",
            ws_name, e
        ))
    })?;

    // SECURITY: Ensure workspace path is within project root
    if !canonical_ws.starts_with(&canonical_root) {
        return Err(LifecycleError::Other(format!(
            "Security violation: workspace '{}' path '{}' is outside project root",
            ws_name, workspace.path
        )));
    }

    let ws_make_path = canonical_ws.join("make.toml");

    // Load workspace-specific make.toml if it exists, otherwise use root
    let ws_make = if ws_make_path.exists() {
        Arc::new(load_make(&ws_make_path)?)
    } else {
        Arc::clone(root_make)
    };

    let ws_state_path = canonical_ws.join(".ggen/state.json");

    Ok(Context::new(
        canonical_ws,
        ws_make,
        ws_state_path,
        env.to_vec(),
    ))
}

/// Run before hooks for a phase
///
/// **DfLSS Fix**: Now supports both predefined phases (backward compatibility)
/// and custom phases via dynamic HashMap lookup.
fn run_before_hooks(ctx: &Context, phase_name: &str) -> Result<()> {
    if let Some(hooks) = &ctx.make.hooks {
        // Global before_all
        if let Some(before_all) = &hooks.before_all {
            for hook_phase in before_all {
                run_phase(ctx, hook_phase)?;
            }
        }

        // Phase-specific before hooks
        // First check explicit fields (backward compatibility)
        let before_hooks = match phase_name {
            "init" => hooks.before_init.as_ref(),
            "setup" => hooks.before_setup.as_ref(),
            "build" => hooks.before_build.as_ref(),
            "test" => hooks.before_test.as_ref(),
            "deploy" => hooks.before_deploy.as_ref(),
            _ => None,
        };

        // If not found in explicit fields, try dynamic lookup (supports custom phases)
        let hooks_list = before_hooks.or_else(|| {
            let hook_key = format!("before_{}", phase_name);
            hooks.phase_hooks.get(&hook_key)
        });

        if let Some(hooks_list) = hooks_list {
            for hook_phase in hooks_list {
                run_phase(ctx, hook_phase)?;
            }
        }
    }

    Ok(())
}

/// Run after hooks for a phase
///
/// **DfLSS Fix**: Now supports both predefined phases (backward compatibility)
/// and custom phases via dynamic HashMap lookup.
fn run_after_hooks(ctx: &Context, phase_name: &str) -> Result<()> {
    if let Some(hooks) = &ctx.make.hooks {
        // Phase-specific after hooks
        // First check explicit fields (backward compatibility)
        let after_hooks = match phase_name {
            "init" => hooks.after_init.as_ref(),
            "setup" => hooks.after_setup.as_ref(),
            "build" => hooks.after_build.as_ref(),
            "test" => hooks.after_test.as_ref(),
            "deploy" => hooks.after_deploy.as_ref(),
            _ => None,
        };

        // If not found in explicit fields, try dynamic lookup (supports custom phases)
        let hooks_list = after_hooks.or_else(|| {
            let hook_key = format!("after_{}", phase_name);
            hooks.phase_hooks.get(&hook_key)
        });

        if let Some(hooks_list) = hooks_list {
            for hook_phase in hooks_list {
                run_phase(ctx, hook_phase)?;
            }
        }

        // Global after_all
        if let Some(after_all) = &hooks.after_all {
            for hook_phase in after_all {
                run_phase(ctx, hook_phase)?;
            }
        }
    }

    Ok(())
}

/// Execute a shell command with streaming output and timeout
///
/// PRODUCTION FIX: Commands now have 5-minute timeout to prevent hung processes
fn execute_command(cmd: &str, cwd: &Path, env: &[(String, String)]) -> Result<()> {
    use std::time::Duration;

    let mut command = if cfg!(target_os = "windows") {
        let mut c = Command::new("cmd");
        c.arg("/C");
        c
    } else {
        let mut c = Command::new("sh");
        c.arg("-c"); // Changed from -lc to -c (don't load profile - faster)
        c
    };

    command.current_dir(cwd).arg(cmd);

    for (key, value) in env {
        command.env(key, value);
    }

    // PRODUCTION FIX: Stream output to user (80/20 - visibility over capture)
    let mut child = command
        .stdout(std::process::Stdio::inherit()) // Show stdout in real-time
        .stderr(std::process::Stdio::inherit()) // Show stderr in real-time
        .spawn()
        .map_err(|e| LifecycleError::command_spawn("unknown", cmd, e))?;

    // PRODUCTION FIX: Implement timeout to prevent hung processes
    /// Default command execution timeout: 5 minutes
    const DEFAULT_COMMAND_TIMEOUT_SECS: u64 = 300;

    let timeout = Duration::from_secs(DEFAULT_COMMAND_TIMEOUT_SECS);
    let start = Instant::now();

    loop {
        match child
            .try_wait()
            .map_err(|e| LifecycleError::command_spawn("unknown", cmd, e))?
        {
            Some(status) => {
                if !status.success() {
                    let exit_code = status.code().unwrap_or(-1);
                    return Err(LifecycleError::command_failed(
                        "unknown",
                        cmd,
                        exit_code,
                        "Command output shown above".to_string(),
                    ));
                }
                return Ok(());
            }
            None if start.elapsed() > timeout => {
                // Timeout exceeded - kill the process
                let _ = child.kill(); // Ignore kill errors
                return Err(LifecycleError::Other(format!(
                    "Command timeout after {}s: {}",
                    timeout.as_secs(),
                    cmd
                )));
            }
            None => {
                // Still running, sleep briefly and check again
                std::thread::sleep(Duration::from_millis(100));
            }
        }
    }
}

/// Get current time in milliseconds since epoch
fn current_time_ms() -> Result<u128> {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis())
        .map_err(|_| LifecycleError::Other("System clock error: time is before UNIX epoch".into()))
}

// Unit tests removed - covered by integration_test.rs:
// - test_phase_commands_extraction (tests both single and multiple commands)
// This provides better coverage with actual make.toml parsing