prodigy 0.4.4

Turn ad-hoc Claude sessions into reproducible development pipelines with parallel AI agents
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
//! Workflow composer for building complex workflows from components

use super::{
    ComposableWorkflow, ComposedWorkflow, CompositionMetadata, DependencyInfo, DependencyType,
    ParameterDefinitions, TemplateRegistry, TemplateSource, WorkflowImport, WorkflowTemplate,
};
use anyhow::{Context, Result};
use serde_json::Value;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// Handles workflow composition from multiple sources
pub struct WorkflowComposer {
    loader: WorkflowLoader,
    template_registry: Arc<TemplateRegistry>,
    resolver: DependencyResolver,
    aliases: std::sync::RwLock<HashMap<String, ComposableWorkflow>>,
}

impl WorkflowComposer {
    /// Create a new workflow composer
    pub fn new(template_registry: Arc<TemplateRegistry>) -> Self {
        Self {
            loader: WorkflowLoader::new(),
            template_registry,
            resolver: DependencyResolver::new(),
            aliases: std::sync::RwLock::new(HashMap::new()),
        }
    }

    /// Compose a workflow from source with parameters
    pub async fn compose(
        &self,
        source: &Path,
        params: HashMap<String, Value>,
    ) -> Result<ComposedWorkflow> {
        // Load base workflow
        let mut workflow = self
            .loader
            .load(source)
            .await
            .with_context(|| format!("Failed to load workflow from {:?}", source))?;

        let mut metadata = CompositionMetadata {
            sources: vec![source.to_path_buf()],
            templates: Vec::new(),
            parameters: params.clone(),
            composed_at: chrono::Utc::now(),
            dependencies: Vec::new(),
        };

        // Process imports
        if let Some(imports) = workflow.imports.clone() {
            self.process_imports(&mut workflow, &imports, &mut metadata)
                .await
                .context("Failed to process imports")?;
        }

        // Apply inheritance
        if let Some(base_name) = workflow.extends.clone() {
            self.apply_inheritance(&mut workflow, &base_name, &mut metadata)
                .await
                .context("Failed to apply inheritance")?;
        }

        // Apply template
        if let Some(template) = workflow.template.clone() {
            self.apply_template(&mut workflow, &template, &mut metadata)
                .await
                .context("Failed to apply template")?;
        }

        // Validate and apply parameters
        workflow
            .validate_parameters(&params)
            .context("Parameter validation failed")?;
        self.apply_parameters(&mut workflow, &params)?;

        // Resolve sub-workflows
        if let Some(sub_workflows) = &workflow.workflows {
            self.validate_sub_workflows(sub_workflows)
                .context("Sub-workflow validation failed")?;
        }

        // Apply defaults
        if let Some(defaults) = workflow.defaults.clone() {
            self.apply_defaults(&mut workflow, &defaults)?;
        }

        // Validate final composition
        self.validate_composition(&workflow, &metadata)
            .context("Composition validation failed")?;

        Ok(ComposedWorkflow { workflow, metadata })
    }

    async fn process_imports(
        &self,
        workflow: &mut ComposableWorkflow,
        imports: &[WorkflowImport],
        metadata: &mut CompositionMetadata,
    ) -> Result<()> {
        for import in imports {
            let imported = self
                .loader
                .load(&import.path)
                .await
                .with_context(|| format!("Failed to load import from {:?}", import.path))?;

            metadata.sources.push(import.path.clone());
            metadata.dependencies.push(DependencyInfo {
                source: import.path.clone(),
                dep_type: DependencyType::Import,
                resolved: import.path.display().to_string(),
            });

            if let Some(alias) = &import.alias {
                // Import with alias - store for reference
                tracing::debug!("Importing {:?} as alias '{}'", import.path, alias);
                let mut aliases = self.aliases.write().unwrap();
                aliases.insert(alias.clone(), imported);
            } else if !import.selective.is_empty() {
                // Selective import
                self.import_selective(workflow, imported, &import.selective)?;
            } else {
                // Import all
                self.merge_workflows(workflow, imported)?;
            }
        }

        Ok(())
    }

    async fn apply_inheritance(
        &self,
        workflow: &mut ComposableWorkflow,
        base_name: &str,
        metadata: &mut CompositionMetadata,
    ) -> Result<()> {
        // Load base workflow
        let base_path = self.resolve_base_path(base_name)?;
        let base = self
            .loader
            .load(&base_path)
            .await
            .with_context(|| format!("Failed to load base workflow '{}'", base_name))?;

        metadata.dependencies.push(DependencyInfo {
            source: base_path.clone(),
            dep_type: DependencyType::Extends,
            resolved: base_name.to_string(),
        });

        // Merge base into current (current overrides base)
        self.merge_workflows_with_inheritance(workflow, base)?;

        Ok(())
    }

    async fn apply_template(
        &self,
        workflow: &mut ComposableWorkflow,
        template: &WorkflowTemplate,
        metadata: &mut CompositionMetadata,
    ) -> Result<()> {
        metadata.templates.push(template.name.clone());

        // Load template based on source
        let template_workflow = match &template.source {
            TemplateSource::File(path) => self.loader.load(path).await?,
            TemplateSource::Registry(name) => self
                .template_registry
                .get(name)
                .await
                .with_context(|| format!("Template '{}' not found in registry", name))?,
            TemplateSource::Url(url) => {
                anyhow::bail!("URL template sources not yet implemented: {}", url);
            }
        };

        // Apply parameters
        let mut instantiated = template_workflow;
        if let Some(params) = &template.with {
            self.apply_template_params(&mut instantiated, params)?;
        }

        // Apply overrides
        if let Some(overrides) = &template.override_field {
            self.apply_overrides(&mut instantiated, overrides)?;
        }

        // Merge with current workflow
        self.merge_workflows(workflow, instantiated)?;

        Ok(())
    }

    fn apply_parameters(
        &self,
        workflow: &mut ComposableWorkflow,
        params: &HashMap<String, Value>,
    ) -> Result<()> {
        // Apply parameters throughout the workflow
        // This would involve variable substitution in commands
        tracing::debug!("Applying {} parameters to workflow", params.len());

        // Substitute parameters in all commands
        for command in &mut workflow.config.commands {
            substitute_parameters_in_command(command, params)
                .context("Failed to substitute parameters in command")?;
        }

        Ok(())
    }

    fn apply_defaults(
        &self,
        workflow: &mut ComposableWorkflow,
        defaults: &HashMap<String, Value>,
    ) -> Result<()> {
        tracing::debug!("Applying {} default values", defaults.len());

        // Apply defaults to environment variables (ensure map exists first)
        let env = workflow.config.env.get_or_insert_with(HashMap::new);

        // Build new entries for missing keys, then extend env
        let new_entries: Vec<(String, String)> = defaults
            .iter()
            .filter(|(key, _)| !env.contains_key(*key))
            .map(|(key, value)| {
                let value_str = match value {
                    Value::String(s) => s.clone(),
                    Value::Number(n) => n.to_string(),
                    Value::Bool(b) => b.to_string(),
                    _ => value.to_string(),
                };
                (key.clone(), value_str)
            })
            .collect();

        env.extend(new_entries);

        // Apply parameter defaults if defined
        if let Some(params) = &mut workflow.parameters {
            params
                .optional
                .iter_mut()
                .filter(|param| param.default.is_none())
                .for_each(|param| {
                    if let Some(default_value) = defaults.get(&param.name) {
                        param.default = Some(default_value.clone());
                    }
                });
        }

        Ok(())
    }

    fn validate_composition(
        &self,
        workflow: &ComposableWorkflow,
        metadata: &CompositionMetadata,
    ) -> Result<()> {
        // Check for circular dependencies
        self.resolver.check_circular_deps(&metadata.dependencies)?;

        // Validate sub-workflow references
        if let Some(sub_workflows) = &workflow.workflows {
            self.validate_sub_workflows(sub_workflows)?;
        }

        Ok(())
    }

    fn merge_workflows(
        &self,
        target: &mut ComposableWorkflow,
        source: ComposableWorkflow,
    ) -> Result<()> {
        // Merge commands
        target.config.commands.extend(source.config.commands);

        // Merge parameters
        if let Some(source_params) = source.parameters {
            if target.parameters.is_none() {
                target.parameters = Some(ParameterDefinitions {
                    required: Vec::new(),
                    optional: Vec::new(),
                });
            }

            if let Some(target_params) = &mut target.parameters {
                target_params.required.extend(source_params.required);
                target_params.optional.extend(source_params.optional);
            }
        }

        // Merge defaults
        if let Some(source_defaults) = source.defaults {
            if target.defaults.is_none() {
                target.defaults = Some(HashMap::new());
            }

            if let Some(target_defaults) = &mut target.defaults {
                for (key, value) in source_defaults {
                    target_defaults.entry(key).or_insert(value);
                }
            }
        }

        // Merge sub-workflows
        if let Some(source_workflows) = source.workflows {
            if target.workflows.is_none() {
                target.workflows = Some(HashMap::new());
            }

            if let Some(target_workflows) = &mut target.workflows {
                target_workflows.extend(source_workflows);
            }
        }

        Ok(())
    }

    fn merge_workflows_with_inheritance(
        &self,
        child: &mut ComposableWorkflow,
        parent: ComposableWorkflow,
    ) -> Result<()> {
        // In inheritance, child overrides parent
        // Start with parent as base
        let mut merged = parent;

        // Override with child values
        if !child.config.commands.is_empty() {
            merged.config.commands = child.config.commands.clone();
        }

        if child.parameters.is_some() {
            merged.parameters = child.parameters.clone();
        }

        if child.defaults.is_some() {
            merged.defaults = child.defaults.clone();
        }

        if child.workflows.is_some() {
            merged.workflows = child.workflows.clone();
        }

        // Replace child with merged
        *child = merged;

        Ok(())
    }

    fn import_selective(
        &self,
        target: &mut ComposableWorkflow,
        source: ComposableWorkflow,
        selective: &[String],
    ) -> Result<()> {
        // Import only selected items
        for item in selective {
            tracing::debug!("Selectively importing '{}'", item);

            // Check if it's a command by index (e.g., "0", "1", "2")
            if let Ok(index) = item.parse::<usize>() {
                if let Some(command) = source.config.commands.get(index) {
                    target.config.commands.push(command.clone());
                    continue;
                }
            }

            // Check if it's a sub-workflow name
            if let Some(workflows) = &source.workflows {
                if let Some(workflow) = workflows.get(item) {
                    if target.workflows.is_none() {
                        target.workflows = Some(HashMap::new());
                    }
                    target
                        .workflows
                        .as_mut()
                        .unwrap()
                        .insert(item.clone(), workflow.clone());
                    continue;
                }
            }

            // Check if it's a parameter
            if let Some(params) = &source.parameters {
                let found_required = params.required.iter().find(|p| p.name == *item);

                let found_optional = params.optional.iter().find(|p| p.name == *item);

                if found_required.is_some() || found_optional.is_some() {
                    if target.parameters.is_none() {
                        target.parameters = Some(ParameterDefinitions {
                            required: Vec::new(),
                            optional: Vec::new(),
                        });
                    }

                    if let Some(target_params) = &mut target.parameters {
                        if let Some(param) = found_required {
                            target_params.required.push(param.clone());
                        } else if let Some(param) = found_optional {
                            target_params.optional.push(param.clone());
                        }
                    }
                    continue;
                }
            }

            anyhow::bail!("Item '{}' not found in source workflow", item);
        }

        Ok(())
    }

    fn apply_template_params(
        &self,
        template: &mut ComposableWorkflow,
        params: &HashMap<String, Value>,
    ) -> Result<()> {
        // Apply parameters to template
        tracing::debug!("Applying {} parameters to template", params.len());

        use regex::Regex;
        let param_regex =
            Regex::new(r"\$\{([^}]+)\}").context("Failed to create parameter regex")?;

        // Apply parameters to commands
        for command in &mut template.config.commands {
            substitute_parameters_in_command(command, params)?;
        }

        // Apply parameters to environment variables
        if let Some(env) = &mut template.config.env {
            for (_key, value) in env.iter_mut() {
                *value = self.substitute_params_in_string(&param_regex, value, params)?;
            }
        }

        // Apply parameters to merge workflow
        if let Some(merge) = &mut template.config.merge {
            for step in &mut merge.commands {
                self.substitute_params_in_workflow_step(&param_regex, step, params)?;
            }
        }

        Ok(())
    }

    fn substitute_params_in_string(
        &self,
        param_regex: &regex::Regex,
        text: &str,
        params: &HashMap<String, Value>,
    ) -> Result<String> {
        let mut result = text.to_string();

        for cap in param_regex.captures_iter(text) {
            let param_expr = &cap[1];

            // Support nested property access: ${item.location.file}
            let value = self.resolve_param_expression(param_expr, params)?;

            let placeholder = format!("${{{}}}", param_expr);
            result = result.replace(&placeholder, &value);
        }

        Ok(result)
    }

    fn resolve_param_expression(
        &self,
        expr: &str,
        params: &HashMap<String, Value>,
    ) -> Result<String> {
        // Split on dots for nested access
        let parts: Vec<&str> = expr.split('.').collect();

        let mut current = params
            .get(parts[0])
            .with_context(|| format!("Parameter '{}' not found", parts[0]))?;

        // Navigate nested structure
        for part in &parts[1..] {
            current = current.get(part).with_context(|| {
                format!("Property '{}' not found in parameter '{}'", part, expr)
            })?;
        }

        // Convert to string
        Ok(match current {
            Value::String(s) => s.clone(),
            Value::Number(n) => n.to_string(),
            Value::Bool(b) => b.to_string(),
            _ => current.to_string(),
        })
    }

    fn substitute_params_in_workflow_step(
        &self,
        param_regex: &regex::Regex,
        step: &mut crate::cook::workflow::WorkflowStep,
        params: &HashMap<String, Value>,
    ) -> Result<()> {
        // Substitute in claude commands
        if let Some(cmd) = &mut step.claude {
            *cmd = self.substitute_params_in_string(param_regex, cmd, params)?;
        }

        // Substitute in shell commands
        if let Some(cmd) = &mut step.shell {
            *cmd = self.substitute_params_in_string(param_regex, cmd, params)?;
        }

        // Substitute in legacy command field
        if let Some(cmd) = &mut step.command {
            *cmd = self.substitute_params_in_string(param_regex, cmd, params)?;
        }

        Ok(())
    }

    fn apply_overrides(
        &self,
        template: &mut ComposableWorkflow,
        overrides: &HashMap<String, Value>,
    ) -> Result<()> {
        // Apply overrides to template
        tracing::debug!("Applying {} overrides to template", overrides.len());

        for (key, value) in overrides {
            match key.as_str() {
                "commands" => {
                    // Override commands array
                    if let Value::Array(commands) = value {
                        template.config.commands = self.parse_commands(commands)?;
                    }
                }

                "env" => {
                    // Override environment variables
                    if let Value::Object(env) = value {
                        let env_map: HashMap<String, String> = env
                            .iter()
                            .map(|(k, v)| (k.clone(), v.to_string()))
                            .collect();
                        template.config.env = Some(env_map);
                    }
                }

                "merge" => {
                    // Override merge workflow
                    if let Value::Object(merge) = value {
                        template.config.merge = Some(self.parse_merge_config(merge)?);
                    }
                }

                // Support dot notation for nested overrides
                key if key.contains('.') => {
                    self.apply_nested_override(template, key, value)?;
                }

                _ => {
                    tracing::warn!("Unknown override key: {}", key);
                }
            }
        }

        Ok(())
    }

    fn parse_commands(&self, commands: &[Value]) -> Result<Vec<crate::config::WorkflowCommand>> {
        commands
            .iter()
            .map(|v| {
                serde_json::from_value(v.clone()).context("Failed to parse command from override")
            })
            .collect()
    }

    fn parse_merge_config(
        &self,
        merge: &serde_json::Map<String, Value>,
    ) -> Result<crate::config::mapreduce::MergeWorkflow> {
        serde_json::from_value(Value::Object(merge.clone()))
            .context("Failed to parse merge config from override")
    }

    fn apply_nested_override(
        &self,
        _template: &mut ComposableWorkflow,
        path: &str,
        _value: &Value,
    ) -> Result<()> {
        // Support paths like "commands[0].timeout"
        // This is a simplified implementation - full path navigation
        // would require more complex logic
        tracing::debug!("Applying nested override at path: {}", path);

        // Note: Full implementation of nested path navigation would be complex
        // and is left for future enhancement if needed

        Ok(())
    }

    fn resolve_base_path(&self, base_name: &str) -> Result<PathBuf> {
        // Resolve base workflow path using functional search
        ["bases", "templates", "workflows", ""]
            .into_iter()
            .map(|dir| {
                if dir.is_empty() {
                    PathBuf::from(format!("{}.yml", base_name))
                } else {
                    PathBuf::from(format!("{}/{}.yml", dir, base_name))
                }
            })
            .find(|path| path.exists())
            .ok_or_else(|| anyhow::anyhow!("Base workflow '{}' not found", base_name))
    }

    fn validate_sub_workflows(
        &self,
        sub_workflows: &HashMap<String, super::SubWorkflow>,
    ) -> Result<()> {
        // Check for missing sources using functional approach
        sub_workflows
            .iter()
            .find(|(_, sub)| !sub.source.exists())
            .map_or(Ok(()), |(name, sub)| {
                anyhow::bail!(
                    "Sub-workflow '{}' source does not exist: {:?}",
                    name,
                    sub.source
                )
            })
    }
}

/// Loads workflows from various sources
struct WorkflowLoader {
    cache: std::sync::Mutex<HashMap<PathBuf, ComposableWorkflow>>,
}

impl WorkflowLoader {
    fn new() -> Self {
        Self {
            cache: std::sync::Mutex::new(HashMap::new()),
        }
    }

    async fn load(&self, path: &Path) -> Result<ComposableWorkflow> {
        // Check cache
        {
            let cache = self.cache.lock().unwrap();
            if let Some(cached) = cache.get(path) {
                return Ok(cached.clone());
            }
        }

        // Load from file
        let content = tokio::fs::read_to_string(path)
            .await
            .with_context(|| format!("Failed to read workflow file: {:?}", path))?;

        let workflow: ComposableWorkflow = serde_yaml::from_str(&content)
            .with_context(|| format!("Failed to parse workflow YAML: {:?}", path))?;

        // Cache for future use
        {
            let mut cache = self.cache.lock().unwrap();
            cache.insert(path.to_path_buf(), workflow.clone());
        }

        Ok(workflow)
    }
}

/// Resolves and validates workflow dependencies
struct DependencyResolver;

impl DependencyResolver {
    fn new() -> Self {
        Self
    }

    fn check_circular_deps(&self, dependencies: &[DependencyInfo]) -> Result<()> {
        // Build dependency graph using functional approach
        let graph: HashMap<String, Vec<String>> =
            dependencies.iter().fold(HashMap::new(), |mut acc, dep| {
                acc.entry(dep.source.display().to_string())
                    .or_default()
                    .push(dep.resolved.clone());
                acc
            });

        // Check for cycles using DFS with functional iteration
        let mut visited = HashSet::new();
        let mut rec_stack = HashSet::new();

        let has_cycle = graph.keys().any(|node| {
            !visited.contains(node)
                && Self::has_cycle(&graph, node, &mut visited, &mut rec_stack).unwrap_or(false)
        });

        if has_cycle {
            anyhow::bail!("Circular dependency detected in workflow composition");
        }

        Ok(())
    }

    fn has_cycle(
        graph: &HashMap<String, Vec<String>>,
        node: &str,
        visited: &mut HashSet<String>,
        rec_stack: &mut HashSet<String>,
    ) -> Result<bool> {
        visited.insert(node.to_string());
        rec_stack.insert(node.to_string());

        if let Some(neighbors) = graph.get(node) {
            for neighbor in neighbors {
                if !visited.contains(neighbor) {
                    if Self::has_cycle(graph, neighbor, visited, rec_stack)? {
                        return Ok(true);
                    }
                } else if rec_stack.contains(neighbor) {
                    return Ok(true);
                }
            }
        }

        rec_stack.remove(node);
        Ok(false)
    }
}

/// Substitute parameters in a workflow command
fn substitute_parameters_in_command(
    command: &mut crate::config::WorkflowCommand,
    params: &HashMap<String, Value>,
) -> Result<()> {
    use crate::config::WorkflowCommand;
    use regex::Regex;

    // Create regex for ${param} pattern matching
    let param_regex = Regex::new(r"\$\{([^}]+)\}").context("Failed to create parameter regex")?;

    match command {
        WorkflowCommand::Simple(ref mut cmd) => {
            *cmd = substitute_params(&param_regex, cmd, params)?;
        }
        WorkflowCommand::Structured(ref mut boxed_cmd) => {
            // Handle structured command fields
            substitute_params_in_structured(&param_regex, boxed_cmd, params)?;
        }
        WorkflowCommand::WorkflowStep(ref mut boxed_step) => {
            // Handle workflow step command fields
            if let Some(ref mut claude) = boxed_step.claude {
                *claude = substitute_params(&param_regex, claude, params)?;
            }
            if let Some(ref mut shell) = boxed_step.shell {
                *shell = substitute_params(&param_regex, shell, params)?;
            }
            // Handle other fields that might contain parameters
            if let Some(ref mut id) = boxed_step.id {
                *id = substitute_params(&param_regex, id, params)?;
            }
        }
        WorkflowCommand::SimpleObject(ref mut simple) => {
            simple.name = substitute_params(&param_regex, &simple.name, params)?;
            if let Some(ref mut args) = simple.args {
                for arg in args {
                    *arg = substitute_params(&param_regex, arg, params)?;
                }
            }
        }
    }

    Ok(())
}

/// Substitute parameters in structured command
fn substitute_params_in_structured(
    regex: &regex::Regex,
    cmd: &mut crate::config::Command,
    params: &HashMap<String, Value>,
) -> Result<()> {
    // Substitute in command name
    cmd.name = substitute_params(regex, &cmd.name, params)?;

    // Substitute in args (args is a Vec, not Option<Vec>)
    for arg in &mut cmd.args {
        if let Some(name) = arg.as_literal_mut() {
            *name = substitute_params(regex, name, params)?;
        }
    }

    Ok(())
}

/// Helper to access CommandArg literal value mutably
trait CommandArgExt {
    fn as_literal_mut(&mut self) -> Option<&mut String>;
}

impl CommandArgExt for crate::config::CommandArg {
    fn as_literal_mut(&mut self) -> Option<&mut String> {
        match self {
            crate::config::CommandArg::Literal(ref mut s) => Some(s),
            crate::config::CommandArg::Variable(_) => None,
        }
    }
}

/// Substitute parameter references in a string
fn substitute_params(
    regex: &regex::Regex,
    text: &str,
    params: &HashMap<String, Value>,
) -> Result<String> {
    // Collect all parameter substitutions (or errors)
    let substitutions: Vec<Result<(String, String)>> = regex
        .captures_iter(text)
        .map(|cap| {
            let param_name = cap[1].to_string();
            params
                .get(&param_name)
                .map(|value| {
                    let value_str = match value {
                        Value::String(s) => s.clone(),
                        Value::Number(n) => n.to_string(),
                        Value::Bool(b) => b.to_string(),
                        Value::Array(_) | Value::Object(_) => {
                            serde_json::to_string(value).unwrap_or_default()
                        }
                        Value::Null => String::new(),
                    };
                    (format!("${{{}}}", param_name), value_str)
                })
                .ok_or_else(|| anyhow::anyhow!("Parameter '{}' not found", param_name))
        })
        .collect();

    // Separate successes from failures
    let (successes, failures): (Vec<_>, Vec<_>) =
        substitutions.into_iter().partition(Result::is_ok);

    // Check for errors
    if !failures.is_empty() {
        let error_msgs: Vec<String> = failures
            .into_iter()
            .filter_map(|r| r.err().map(|e| e.to_string()))
            .collect();
        anyhow::bail!("Parameter substitution errors: {}", error_msgs.join(", "));
    }

    // Apply all substitutions
    Ok(successes
        .into_iter()
        .filter_map(Result::ok)
        .fold(text.to_string(), |acc, (placeholder, value)| {
            acc.replace(&placeholder, &value)
        }))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_workflow_composer_creation() {
        let registry = Arc::new(TemplateRegistry::new());
        let _composer = WorkflowComposer::new(registry);

        // Basic test to ensure composer can be created
        // Composer created successfully
    }

    #[test]
    fn test_dependency_resolver() {
        let resolver = DependencyResolver::new();

        let deps = vec![
            DependencyInfo {
                source: PathBuf::from("a.yml"),
                dep_type: DependencyType::Import,
                resolved: "b.yml".to_string(),
            },
            DependencyInfo {
                source: PathBuf::from("b.yml"),
                dep_type: DependencyType::Import,
                resolved: "c.yml".to_string(),
            },
        ];

        // Should not detect cycle in linear dependencies
        assert!(resolver.check_circular_deps(&deps).is_ok());

        let circular_deps = vec![
            DependencyInfo {
                source: PathBuf::from("a.yml"),
                dep_type: DependencyType::Import,
                resolved: "b.yml".to_string(),
            },
            DependencyInfo {
                source: PathBuf::from("b.yml"),
                dep_type: DependencyType::Import,
                resolved: "a.yml".to_string(),
            },
        ];

        // Should detect circular dependency
        assert!(resolver.check_circular_deps(&circular_deps).is_err());
    }

    #[test]
    fn test_substitute_params() {
        use regex::Regex;

        let mut params = HashMap::new();
        params.insert("target".to_string(), Value::String("app.js".to_string()));
        params.insert(
            "count".to_string(),
            Value::Number(serde_json::Number::from(42)),
        );
        params.insert("enabled".to_string(), Value::Bool(true));

        let regex = Regex::new(r"\$\{([^}]+)\}").unwrap();

        // Test simple string substitution
        let result = substitute_params(&regex, "Process ${target}", &params);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "Process app.js");

        // Test number substitution
        let result = substitute_params(&regex, "Count: ${count}", &params);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "Count: 42");

        // Test boolean substitution
        let result = substitute_params(&regex, "Enabled: ${enabled}", &params);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "Enabled: true");

        // Test multiple substitutions
        let result =
            substitute_params(&regex, "${target} has ${count} items (${enabled})", &params);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "app.js has 42 items (true)");

        // Test missing parameter
        let result = substitute_params(&regex, "Missing: ${missing}", &params);
        assert!(result.is_err());
    }

    #[test]
    fn test_substitute_parameters_in_command() {
        use crate::config::WorkflowCommand;

        let mut params = HashMap::new();
        params.insert("file".to_string(), Value::String("main.rs".to_string()));

        // Test with Simple command
        let mut cmd = WorkflowCommand::Simple("claude: /refactor ${file}".to_string());
        let result = substitute_parameters_in_command(&mut cmd, &params);
        assert!(result.is_ok());
        match cmd {
            WorkflowCommand::Simple(s) => assert_eq!(s, "claude: /refactor main.rs"),
            _ => panic!("Expected Simple command"),
        }
    }
}