cuenv-core 0.40.6

Core types and error handling for the cuenv ecosystem
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
//! Contributor engine for task DAG injection
//!
//! Contributors are CUE-defined task injectors that modify the task DAG before execution.
//! The engine evaluates activation conditions and injects tasks with proper naming.
//!
//! ## Data Flow
//!
//! 1. CUE evaluation produces Projects with Tasks (initial DAG)
//! 2. ContributorEngine applies contributors:
//!    - Evaluates `when` conditions (workspaceMember, command patterns)
//!    - Injects contributor tasks with `cuenv:contributor:*` prefix
//!    - Auto-associates user tasks with contributor setup tasks
//!    - Loops until no changes (stable DAG)
//! 3. Final DAG passed to executor (CLI or CI)
//!
//! ## Task Naming Convention
//!
//! Contributor tasks use the format: `cuenv:contributor:{contributor}.{task}`
//! Example: `cuenv:contributor:bun.workspace.install`

use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::Path;

use serde::{Deserialize, Serialize};

use crate::Result;
use crate::tasks::{Input, Task, TaskDependency, TaskNode};

/// Prefix for all contributor-injected tasks
pub const CONTRIBUTOR_TASK_PREFIX: &str = "cuenv:contributor:";

/// Context provided to contributors for activation condition evaluation
#[derive(Debug, Clone, Default)]
pub struct ContributorContext {
    /// Detected workspace membership (e.g., "bun", "npm", "cargo")
    pub workspace_member: Option<String>,

    /// Path to workspace root (if member of a workspace)
    pub workspace_root: Option<std::path::PathBuf>,

    /// All commands used by tasks in the project (for command-based activation)
    pub task_commands: HashSet<String>,

    /// Commands used by services in the project (for service command-based activation)
    pub service_commands: HashSet<String>,

    /// Whether the project has any services defined
    pub has_services: bool,
}

impl ContributorContext {
    /// Create context by detecting workspace from project root
    #[must_use]
    pub fn detect(project_root: &Path) -> Self {
        let mut ctx = Self::default();

        // Use cuenv-workspaces for detection
        if let Ok(managers) = cuenv_workspaces::detect_package_managers(project_root)
            && let Some(first) = managers.first()
        {
            ctx.workspace_member = Some(workspace_name_for_manager(*first).to_string());
        }

        ctx
    }

    /// Add task commands from a project's tasks
    pub fn with_task_commands(mut self, tasks: &HashMap<String, TaskNode>) -> Self {
        for node in tasks.values() {
            collect_commands_from_node(node, &mut self.task_commands);
        }
        self
    }

    /// Add service commands from a project's services
    pub fn with_services(mut self, services: &HashMap<String, crate::manifest::Service>) -> Self {
        self.has_services = !services.is_empty();
        for service in services.values() {
            if let Some(cmd) = service.primary_command()
                && let Some(cmd_name) = cuenv_workspaces::command_name(cmd)
            {
                self.service_commands.insert(cmd_name);
            }
        }
        self
    }
}

/// Returns the canonical workspace name for a package manager
fn workspace_name_for_manager(manager: cuenv_workspaces::PackageManager) -> &'static str {
    match manager {
        cuenv_workspaces::PackageManager::Npm => "npm",
        cuenv_workspaces::PackageManager::Bun => "bun",
        cuenv_workspaces::PackageManager::Pnpm => "pnpm",
        cuenv_workspaces::PackageManager::YarnClassic
        | cuenv_workspaces::PackageManager::YarnModern => "yarn",
        cuenv_workspaces::PackageManager::Cargo => "cargo",
        cuenv_workspaces::PackageManager::Deno => "deno",
    }
}

/// Collect all commands from a task node recursively
fn collect_commands_from_node(node: &TaskNode, commands: &mut HashSet<String>) {
    match node {
        TaskNode::Task(task) => {
            if !task.command.is_empty()
                && let Some(cmd) = cuenv_workspaces::command_name(&task.command)
            {
                commands.insert(cmd);
            }
        }
        TaskNode::Group(group) => {
            for sub in group.children.values() {
                collect_commands_from_node(sub, commands);
            }
        }
        TaskNode::Sequence(steps) => {
            for sub in steps {
                collect_commands_from_node(sub, commands);
            }
        }
    }
}

/// Activation condition for contributors
///
/// All specified conditions must be true (AND logic)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct ContributorActivation {
    /// Always active (no conditions)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub always: Option<bool>,

    /// Workspace membership detection (active if project is member of these workspace types)
    /// Values: "npm", "bun", "pnpm", "yarn", "cargo", "deno"
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub workspace_member: Vec<String>,

    /// Command detection for auto-association (active if any task uses these commands)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub command: Vec<String>,

    /// Service command detection (active if any service uses these commands)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub service_command: Vec<String>,

    /// Service presence (active if project has any services defined)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub has_service: Option<bool>,
}

/// Auto-association rules for contributors
///
/// Defines how user tasks are automatically connected to contributor tasks
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct AutoAssociate {
    /// Commands that trigger auto-association (e.g., ["bun", "bunx"])
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub command: Vec<String>,

    /// Task to inject as dependency (e.g., "cuenv:contributor:bun.workspace.setup")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inject_dependency: Option<String>,
}

/// A task contributed by a contributor
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct ContributorTask {
    /// Task identifier (will be prefixed with contributor namespace)
    pub id: String,

    /// Shell command to execute
    #[serde(skip_serializing_if = "Option::is_none")]
    pub command: Option<String>,

    /// Command arguments
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub args: Vec<String>,

    /// Multi-line script (alternative to command)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub script: Option<String>,

    /// Input files/patterns for caching
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub inputs: Vec<String>,

    /// Output files/patterns for caching
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub outputs: Vec<String>,

    /// Whether task requires hermetic execution
    #[serde(default)]
    pub hermetic: bool,

    /// Dependencies on other tasks (within contributor namespace)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub depends_on: Vec<String>,

    /// Human-readable description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

/// Contributor definition
///
/// Contributors inject tasks into the DAG based on activation conditions
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Contributor {
    /// Contributor identifier (e.g., "bun.workspace")
    pub id: String,

    /// Activation condition (defaults to always active)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub when: Option<ContributorActivation>,

    /// Tasks to contribute when active
    pub tasks: Vec<ContributorTask>,

    /// Auto-association rules for user tasks
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auto_associate: Option<AutoAssociate>,
}

/// Engine that applies contributors to modify the task DAG
pub struct ContributorEngine<'a> {
    contributors: &'a [Contributor],
    context: ContributorContext,
}

impl<'a> ContributorEngine<'a> {
    /// Create a new contributor engine
    #[must_use]
    pub fn new(contributors: &'a [Contributor], context: ContributorContext) -> Self {
        Self {
            contributors,
            context,
        }
    }

    /// Apply all active contributors to the task DAG
    ///
    /// Loops until no contributor makes changes (stable DAG).
    /// Returns the number of tasks injected.
    pub fn apply(&self, tasks: &mut HashMap<String, TaskNode>) -> Result<usize> {
        let mut total_injected = 0;
        let max_iterations = 10; // Safety limit to prevent infinite loops

        for iteration in 0..max_iterations {
            let mut changed = false;

            for contributor in self.contributors {
                if self.is_active(contributor) {
                    let injected = self.inject_tasks(contributor, tasks);
                    if injected > 0 {
                        changed = true;
                        total_injected += injected;
                        tracing::debug!(
                            contributor = %contributor.id,
                            injected,
                            "Contributor injected tasks"
                        );
                    }

                    // Apply auto-association rules
                    if let Some(auto_assoc) = &contributor.auto_associate {
                        self.apply_auto_association(auto_assoc, tasks);
                    }
                }
            }

            if !changed {
                tracing::debug!(
                    iterations = iteration + 1,
                    total_injected,
                    "Contributor loop stabilized"
                );
                break;
            }
        }

        Ok(total_injected)
    }

    /// Check if a contributor should be active based on its conditions
    fn is_active(&self, contributor: &Contributor) -> bool {
        let Some(when) = &contributor.when else {
            // No conditions means always active
            return true;
        };

        // Check always flag
        if when.always == Some(true) {
            return true;
        }

        // Check workspace membership (OR within, AND with other conditions)
        if !when.workspace_member.is_empty() {
            let has_match = self.context.workspace_member.as_ref().is_some_and(|ws| {
                when.workspace_member
                    .iter()
                    .any(|w| w.eq_ignore_ascii_case(ws))
            });
            if !has_match {
                return false;
            }
        }

        // Check command usage (OR within, AND with other conditions)
        if !when.command.is_empty() {
            let has_match = when
                .command
                .iter()
                .any(|cmd| self.context.task_commands.contains(cmd));
            if !has_match {
                return false;
            }
        }

        // Check service command usage (OR within, AND with other conditions)
        if !when.service_command.is_empty() {
            let has_match = when
                .service_command
                .iter()
                .any(|cmd| self.context.service_commands.contains(cmd));
            if !has_match {
                return false;
            }
        }

        // Check service presence
        if when.has_service == Some(true) && !self.context.has_services {
            return false;
        }
        if when.has_service == Some(false) && self.context.has_services {
            return false;
        }

        true
    }

    /// Inject tasks from a contributor into the DAG
    ///
    /// Returns the number of tasks injected
    fn inject_tasks(
        &self,
        contributor: &Contributor,
        tasks: &mut HashMap<String, TaskNode>,
    ) -> usize {
        let mut injected = 0;

        for contrib_task in &contributor.tasks {
            // Build the full task ID with prefix
            let task_id = if contrib_task.id.starts_with(CONTRIBUTOR_TASK_PREFIX) {
                contrib_task.id.clone()
            } else {
                format!("{}{}", CONTRIBUTOR_TASK_PREFIX, contrib_task.id)
            };

            // Skip if already exists
            if tasks.contains_key(&task_id) {
                continue;
            }

            // Convert ContributorTask to TaskNode
            let task = Task {
                command: contrib_task.command.clone().unwrap_or_default(),
                args: contrib_task.args.clone(),
                script: contrib_task.script.clone(),
                inputs: contrib_task
                    .inputs
                    .iter()
                    .map(|s| Input::Path(s.clone()))
                    .collect(),
                outputs: contrib_task.outputs.clone(),
                hermetic: contrib_task.hermetic,
                depends_on: contrib_task
                    .depends_on
                    .iter()
                    .map(|dep| {
                        // Prefix dependencies if they don't already have it
                        let name =
                            if dep.starts_with(CONTRIBUTOR_TASK_PREFIX) || dep.starts_with('#') {
                                dep.clone()
                            } else {
                                format!("{}{}", CONTRIBUTOR_TASK_PREFIX, dep)
                            };
                        TaskDependency::from_name(name)
                    })
                    .collect(),
                description: contrib_task.description.clone(),
                ..Default::default()
            };

            tasks.insert(task_id.clone(), TaskNode::Task(Box::new(task)));
            injected += 1;

            tracing::trace!(task = %task_id, "Injected contributor task");
        }

        injected
    }

    /// Apply auto-association rules to existing tasks
    fn apply_auto_association(
        &self,
        auto_assoc: &AutoAssociate,
        tasks: &mut HashMap<String, TaskNode>,
    ) {
        let Some(inject_dep) = &auto_assoc.inject_dependency else {
            return;
        };

        // Verify the dependency task exists
        if !tasks.contains_key(inject_dep) {
            return;
        }

        // Collect task names to modify (can't modify while iterating)
        let task_names: Vec<String> = tasks.keys().cloned().collect();

        for task_name in task_names {
            // Skip contributor tasks
            if task_name.starts_with(CONTRIBUTOR_TASK_PREFIX) {
                continue;
            }

            let Some(node) = tasks.get_mut(&task_name) else {
                continue;
            };

            Self::auto_associate_node(node, &auto_assoc.command, inject_dep);
        }
    }

    /// Recursively apply auto-association to a task node
    fn auto_associate_node(node: &mut TaskNode, commands: &[String], inject_dep: &str) {
        match node {
            TaskNode::Task(task) => {
                // Check if task command matches any auto-associate command
                let Some(base_cmd) = cuenv_workspaces::command_name(&task.command) else {
                    return;
                };

                if commands.iter().any(|c| c == &base_cmd) {
                    // Add dependency if not already present
                    if !task.depends_on.iter().any(|d| d.task_name() == inject_dep) {
                        task.depends_on.push(TaskDependency::from_name(inject_dep));
                        tracing::trace!(
                            command = %task.command,
                            dependency = %inject_dep,
                            "Auto-associated task with contributor"
                        );
                    }
                }
            }
            TaskNode::Group(group) => {
                for sub in group.children.values_mut() {
                    Self::auto_associate_node(sub, commands, inject_dep);
                }
            }
            TaskNode::Sequence(steps) => {
                for sub in steps {
                    Self::auto_associate_node(sub, commands, inject_dep);
                }
            }
        }
    }
}

/// Result of applying contributors
#[derive(Debug, Clone, Default)]
pub struct ContributorResult {
    /// Number of tasks injected
    pub tasks_injected: usize,

    /// Contributors that were activated
    pub active_contributors: Vec<String>,
}

// =============================================================================
// Built-in Workspace Contributors
// =============================================================================

/// Create the built-in bun workspace contributor
#[must_use]
pub fn bun_workspace_contributor() -> Contributor {
    Contributor {
        id: "bun.workspace".to_string(),
        when: Some(ContributorActivation {
            workspace_member: vec!["bun".to_string()],
            ..Default::default()
        }),
        tasks: vec![
            ContributorTask {
                id: "bun.workspace.install".to_string(),
                command: Some("bun".to_string()),
                args: vec!["install".to_string(), "--frozen-lockfile".to_string()],
                inputs: vec!["package.json".to_string(), "bun.lock".to_string()],
                outputs: vec!["node_modules".to_string()],
                hermetic: false,
                description: Some("Install Bun dependencies".to_string()),
                ..Default::default()
            },
            ContributorTask {
                id: "bun.workspace.setup".to_string(),
                script: Some("true".to_string()),
                hermetic: false,
                depends_on: vec!["bun.workspace.install".to_string()],
                description: Some("Bun workspace setup complete".to_string()),
                ..Default::default()
            },
        ],
        auto_associate: Some(AutoAssociate {
            command: vec!["bun".to_string(), "bunx".to_string()],
            inject_dependency: Some(format!("{}bun.workspace.setup", CONTRIBUTOR_TASK_PREFIX)),
        }),
    }
}

/// Create the built-in npm workspace contributor
#[must_use]
pub fn npm_workspace_contributor() -> Contributor {
    Contributor {
        id: "npm.workspace".to_string(),
        when: Some(ContributorActivation {
            workspace_member: vec!["npm".to_string()],
            ..Default::default()
        }),
        tasks: vec![
            ContributorTask {
                id: "npm.workspace.install".to_string(),
                command: Some("npm".to_string()),
                args: vec!["ci".to_string()],
                inputs: vec!["package.json".to_string(), "package-lock.json".to_string()],
                outputs: vec!["node_modules".to_string()],
                hermetic: false,
                description: Some("Install npm dependencies".to_string()),
                ..Default::default()
            },
            ContributorTask {
                id: "npm.workspace.setup".to_string(),
                script: Some("true".to_string()),
                hermetic: false,
                depends_on: vec!["npm.workspace.install".to_string()],
                description: Some("npm workspace setup complete".to_string()),
                ..Default::default()
            },
        ],
        auto_associate: Some(AutoAssociate {
            command: vec!["npm".to_string(), "npx".to_string()],
            inject_dependency: Some(format!("{}npm.workspace.setup", CONTRIBUTOR_TASK_PREFIX)),
        }),
    }
}

/// Create the built-in pnpm workspace contributor
#[must_use]
pub fn pnpm_workspace_contributor() -> Contributor {
    Contributor {
        id: "pnpm.workspace".to_string(),
        when: Some(ContributorActivation {
            workspace_member: vec!["pnpm".to_string()],
            ..Default::default()
        }),
        tasks: vec![
            ContributorTask {
                id: "pnpm.workspace.install".to_string(),
                command: Some("pnpm".to_string()),
                args: vec!["install".to_string(), "--frozen-lockfile".to_string()],
                inputs: vec!["package.json".to_string(), "pnpm-lock.yaml".to_string()],
                outputs: vec!["node_modules".to_string()],
                hermetic: false,
                description: Some("Install pnpm dependencies".to_string()),
                ..Default::default()
            },
            ContributorTask {
                id: "pnpm.workspace.setup".to_string(),
                script: Some("true".to_string()),
                hermetic: false,
                depends_on: vec!["pnpm.workspace.install".to_string()],
                description: Some("pnpm workspace setup complete".to_string()),
                ..Default::default()
            },
        ],
        auto_associate: Some(AutoAssociate {
            command: vec!["pnpm".to_string(), "pnpx".to_string()],
            inject_dependency: Some(format!("{}pnpm.workspace.setup", CONTRIBUTOR_TASK_PREFIX)),
        }),
    }
}

/// Create the built-in yarn workspace contributor
#[must_use]
pub fn yarn_workspace_contributor() -> Contributor {
    Contributor {
        id: "yarn.workspace".to_string(),
        when: Some(ContributorActivation {
            workspace_member: vec!["yarn".to_string()],
            ..Default::default()
        }),
        tasks: vec![
            ContributorTask {
                id: "yarn.workspace.install".to_string(),
                command: Some("yarn".to_string()),
                args: vec!["install".to_string(), "--immutable".to_string()],
                inputs: vec!["package.json".to_string(), "yarn.lock".to_string()],
                outputs: vec!["node_modules".to_string()],
                hermetic: false,
                description: Some("Install Yarn dependencies".to_string()),
                ..Default::default()
            },
            ContributorTask {
                id: "yarn.workspace.setup".to_string(),
                script: Some("true".to_string()),
                hermetic: false,
                depends_on: vec!["yarn.workspace.install".to_string()],
                description: Some("Yarn workspace setup complete".to_string()),
                ..Default::default()
            },
        ],
        auto_associate: Some(AutoAssociate {
            command: vec!["yarn".to_string()],
            inject_dependency: Some(format!("{}yarn.workspace.setup", CONTRIBUTOR_TASK_PREFIX)),
        }),
    }
}

/// Returns all built-in workspace contributors
#[must_use]
pub fn builtin_workspace_contributors() -> Vec<Contributor> {
    vec![
        bun_workspace_contributor(),
        npm_workspace_contributor(),
        pnpm_workspace_contributor(),
        yarn_workspace_contributor(),
    ]
}

/// Build a map of expected task dependencies for DAG verification
#[must_use]
pub fn build_expected_dag(tasks: &HashMap<String, TaskNode>) -> BTreeMap<String, Vec<String>> {
    let mut dag = BTreeMap::new();

    for (name, node) in tasks {
        let deps = collect_deps_from_node(node);
        dag.insert(name.clone(), deps);
    }

    dag
}

/// Collect dependencies from a task node as string names
fn collect_deps_from_node(node: &TaskNode) -> Vec<String> {
    match node {
        TaskNode::Task(task) => task
            .depends_on
            .iter()
            .map(|d| d.task_name().to_string())
            .collect(),
        TaskNode::Group(group) => group
            .depends_on
            .iter()
            .map(|d| d.task_name().to_string())
            .collect(),
        TaskNode::Sequence(_) => Vec::new(), // Sequences don't have top-level deps
    }
}

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

    fn create_test_contributor(id: &str, workspace_member: Vec<&str>) -> Contributor {
        Contributor {
            id: id.to_string(),
            when: Some(ContributorActivation {
                workspace_member: workspace_member.into_iter().map(String::from).collect(),
                ..Default::default()
            }),
            tasks: vec![
                ContributorTask {
                    id: format!("{id}.install"),
                    command: Some("test-cmd".to_string()),
                    args: vec!["install".to_string()],
                    inputs: vec!["package.json".to_string()],
                    outputs: vec!["node_modules".to_string()],
                    hermetic: false,
                    depends_on: vec![],
                    script: None,
                    description: Some(format!("Install {id} dependencies")),
                },
                ContributorTask {
                    id: format!("{id}.setup"),
                    command: None,
                    args: vec![],
                    script: Some("true".to_string()),
                    inputs: vec![],
                    outputs: vec![],
                    hermetic: false,
                    depends_on: vec![format!("{id}.install")],
                    description: Some(format!("{id} setup complete")),
                },
            ],
            auto_associate: Some(AutoAssociate {
                command: vec!["test-cmd".to_string()],
                inject_dependency: Some(format!("{CONTRIBUTOR_TASK_PREFIX}{id}.setup")),
            }),
        }
    }

    #[test]
    fn test_contributor_activation_workspace_member() {
        let contrib = create_test_contributor("bun.workspace", vec!["bun"]);

        // Should activate when workspace matches
        let ctx = ContributorContext {
            workspace_member: Some("bun".to_string()),
            ..Default::default()
        };
        let contributors = [contrib.clone()];
        let engine = ContributorEngine::new(&contributors, ctx);
        assert!(engine.is_active(&contrib));

        // Should not activate when workspace doesn't match
        let ctx = ContributorContext {
            workspace_member: Some("npm".to_string()),
            ..Default::default()
        };
        let contributors = [contrib.clone()];
        let engine = ContributorEngine::new(&contributors, ctx);
        assert!(!engine.is_active(&contrib));

        // Should not activate when no workspace
        let ctx = ContributorContext::default();
        let contributors = [contrib.clone()];
        let engine = ContributorEngine::new(&contributors, ctx);
        assert!(!engine.is_active(&contrib));
    }

    #[test]
    fn test_contributor_injects_tasks() {
        let contrib = create_test_contributor("bun.workspace", vec!["bun"]);
        let ctx = ContributorContext {
            workspace_member: Some("bun".to_string()),
            ..Default::default()
        };

        let contributors = [contrib];
        let engine = ContributorEngine::new(&contributors, ctx);
        let mut tasks: HashMap<String, TaskNode> = HashMap::new();

        let injected = engine.apply(&mut tasks).unwrap();

        assert_eq!(injected, 2);
        assert!(tasks.contains_key("cuenv:contributor:bun.workspace.install"));
        assert!(tasks.contains_key("cuenv:contributor:bun.workspace.setup"));
    }

    #[test]
    fn test_contributor_auto_association() {
        let contrib = create_test_contributor("bun.workspace", vec!["bun"]);
        let ctx = ContributorContext {
            workspace_member: Some("bun".to_string()),
            workspace_root: None,
            task_commands: ["test-cmd".to_string()].into_iter().collect(),
            ..Default::default()
        };

        // Create a user task that uses the matching command
        let user_task = Task {
            command: "test-cmd".to_string(),
            args: vec!["run".to_string(), "dev".to_string()],
            ..Default::default()
        };

        let mut tasks: HashMap<String, TaskNode> = HashMap::new();
        tasks.insert("dev".to_string(), TaskNode::Task(Box::new(user_task)));

        let contributors = [contrib];
        let engine = ContributorEngine::new(&contributors, ctx);
        engine.apply(&mut tasks).unwrap();

        // User task should now depend on the contributor setup task
        let dev_task = tasks.get("dev").unwrap();
        if let TaskNode::Task(task) = dev_task {
            assert!(
                task.depends_on
                    .iter()
                    .any(|d| d.task_name() == "cuenv:contributor:bun.workspace.setup")
            );
        } else {
            panic!("Expected single task");
        }
    }

    #[test]
    fn test_contributor_auto_association_with_env_prefixed_command() {
        let contrib = create_test_contributor("bun.workspace", vec!["bun"]);
        let ctx = ContributorContext {
            workspace_member: Some("bun".to_string()),
            workspace_root: None,
            task_commands: ["test-cmd".to_string()].into_iter().collect(),
            ..Default::default()
        };

        let user_task = Task {
            command: "env TEST_MODE=1 test-cmd run dev".to_string(),
            ..Default::default()
        };

        let mut tasks: HashMap<String, TaskNode> = HashMap::new();
        tasks.insert("dev".to_string(), TaskNode::Task(Box::new(user_task)));

        let contributors = [contrib];
        let engine = ContributorEngine::new(&contributors, ctx);
        engine.apply(&mut tasks).unwrap();

        let dev_task = tasks.get("dev").unwrap();
        if let TaskNode::Task(task) = dev_task {
            assert!(
                task.depends_on
                    .iter()
                    .any(|d| d.task_name() == "cuenv:contributor:bun.workspace.setup")
            );
        } else {
            panic!("Expected single task");
        }
    }

    #[test]
    fn test_idempotent_injection() {
        let contrib = create_test_contributor("bun.workspace", vec!["bun"]);
        let ctx = ContributorContext {
            workspace_member: Some("bun".to_string()),
            ..Default::default()
        };

        let contributors = [contrib];
        let engine = ContributorEngine::new(&contributors, ctx);
        let mut tasks: HashMap<String, TaskNode> = HashMap::new();

        // First application
        let first_injected = engine.apply(&mut tasks).unwrap();
        assert_eq!(first_injected, 2);

        // Second application should inject nothing (already exists)
        let second_injected = engine.apply(&mut tasks).unwrap();
        assert_eq!(second_injected, 0);

        // Should still have exactly 2 tasks
        assert_eq!(tasks.len(), 2);
    }

    #[test]
    fn test_always_active_contributor() {
        let contrib = Contributor {
            id: "always-on".to_string(),
            when: Some(ContributorActivation {
                always: Some(true),
                ..Default::default()
            }),
            tasks: vec![ContributorTask {
                id: "always-on.task".to_string(),
                command: Some("echo".to_string()),
                args: vec!["always".to_string()],
                ..Default::default()
            }],
            auto_associate: None,
        };

        // Should activate regardless of context
        let ctx = ContributorContext::default();
        let contributors = [contrib.clone()];
        let engine = ContributorEngine::new(&contributors, ctx);
        assert!(engine.is_active(&contrib));
    }

    #[test]
    fn test_no_condition_means_always_active() {
        let contrib = Contributor {
            id: "no-condition".to_string(),
            when: None, // No condition
            tasks: vec![ContributorTask {
                id: "no-condition.task".to_string(),
                command: Some("echo".to_string()),
                args: vec!["hello".to_string()],
                ..Default::default()
            }],
            auto_associate: None,
        };

        let ctx = ContributorContext::default();
        let contributors = [contrib.clone()];
        let engine = ContributorEngine::new(&contributors, ctx);
        assert!(engine.is_active(&contrib));
    }

    #[test]
    fn test_build_expected_dag() {
        let mut tasks: HashMap<String, TaskNode> = HashMap::new();

        let task_a = Task {
            command: "echo".to_string(),
            args: vec!["a".to_string()],
            ..Default::default()
        };

        let task_b = Task {
            command: "echo".to_string(),
            args: vec!["b".to_string()],
            depends_on: vec![TaskDependency::from_name("a")],
            ..Default::default()
        };

        tasks.insert("a".to_string(), TaskNode::Task(Box::new(task_a)));
        tasks.insert("b".to_string(), TaskNode::Task(Box::new(task_b)));

        let dag = build_expected_dag(&tasks);

        assert_eq!(dag.get("a"), Some(&vec![]));
        assert_eq!(dag.get("b"), Some(&vec!["a".to_string()]));
    }

    #[test]
    fn test_multiple_contributors_active_simultaneously() {
        // Two contributors that both match (different workspace types)
        let bun_contrib = create_test_contributor("bun.workspace", vec!["bun"]);
        let npm_contrib = Contributor {
            id: "npm.workspace".to_string(),
            when: Some(ContributorActivation {
                workspace_member: vec!["npm".to_string()],
                ..Default::default()
            }),
            tasks: vec![ContributorTask {
                id: "npm.workspace.install".to_string(),
                command: Some("npm".to_string()),
                args: vec!["install".to_string()],
                ..Default::default()
            }],
            auto_associate: None,
        };

        // Context where both could theoretically match (we'll test bun only)
        let ctx = ContributorContext {
            workspace_member: Some("bun".to_string()),
            ..Default::default()
        };

        let contributors = [bun_contrib.clone(), npm_contrib.clone()];
        let engine = ContributorEngine::new(&contributors, ctx);
        let mut tasks: HashMap<String, TaskNode> = HashMap::new();

        engine.apply(&mut tasks).unwrap();

        // Only bun tasks should be injected (npm doesn't match)
        assert!(tasks.contains_key("cuenv:contributor:bun.workspace.install"));
        assert!(tasks.contains_key("cuenv:contributor:bun.workspace.setup"));
        assert!(!tasks.contains_key("cuenv:contributor:npm.workspace.install"));
    }

    #[test]
    fn test_auto_association_no_duplicate_deps() {
        let contrib = create_test_contributor("bun.workspace", vec!["bun"]);
        let ctx = ContributorContext {
            workspace_member: Some("bun".to_string()),
            workspace_root: None,
            task_commands: ["test-cmd".to_string()].into_iter().collect(),
            ..Default::default()
        };

        // Create a user task that already has the dependency
        let user_task = Task {
            command: "test-cmd".to_string(),
            args: vec!["run".to_string(), "dev".to_string()],
            depends_on: vec![TaskDependency::from_name(
                "cuenv:contributor:bun.workspace.setup",
            )],
            ..Default::default()
        };

        let mut tasks: HashMap<String, TaskNode> = HashMap::new();
        tasks.insert("dev".to_string(), TaskNode::Task(Box::new(user_task)));

        let contributors = [contrib];
        let engine = ContributorEngine::new(&contributors, ctx);
        engine.apply(&mut tasks).unwrap();

        // Should not have duplicated the dependency
        let dev_task = tasks.get("dev").unwrap();
        if let TaskNode::Task(task) = dev_task {
            let dep_count = task
                .depends_on
                .iter()
                .filter(|d| d.task_name() == "cuenv:contributor:bun.workspace.setup")
                .count();
            assert_eq!(dep_count, 1, "Dependency should not be duplicated");
        } else {
            panic!("Expected single task");
        }
    }

    #[test]
    fn test_command_matching_is_exact() {
        let contrib = create_test_contributor("bun.workspace", vec!["bun"]);
        let ctx = ContributorContext {
            workspace_member: Some("bun".to_string()),
            workspace_root: None,
            task_commands: ["test-cmd".to_string()].into_iter().collect(),
            ..Default::default()
        };

        // Task with a command that is NOT an exact match
        let user_task = Task {
            command: "test-cmd-extra".to_string(), // Different command
            args: vec!["run".to_string()],
            ..Default::default()
        };

        let mut tasks: HashMap<String, TaskNode> = HashMap::new();
        tasks.insert("other".to_string(), TaskNode::Task(Box::new(user_task)));

        let contributors = [contrib];
        let engine = ContributorEngine::new(&contributors, ctx);
        engine.apply(&mut tasks).unwrap();

        // Should NOT have auto-associated (command doesn't match exactly)
        let other_task = tasks.get("other").unwrap();
        if let TaskNode::Task(task) = other_task {
            assert!(
                !task
                    .depends_on
                    .iter()
                    .any(|d| d.task_name() == "cuenv:contributor:bun.workspace.setup"),
                "Non-matching command should not get auto-association"
            );
        } else {
            panic!("Expected single task");
        }
    }

    #[test]
    fn test_contributor_with_empty_tasks() {
        let contrib = Contributor {
            id: "empty".to_string(),
            when: Some(ContributorActivation {
                always: Some(true),
                ..Default::default()
            }),
            tasks: vec![], // No tasks
            auto_associate: None,
        };

        let ctx = ContributorContext::default();
        let contributors = [contrib];
        let engine = ContributorEngine::new(&contributors, ctx);
        let mut tasks: HashMap<String, TaskNode> = HashMap::new();

        let injected = engine.apply(&mut tasks).unwrap();

        // Should inject nothing
        assert_eq!(injected, 0);
        assert!(tasks.is_empty());
    }

    #[test]
    fn test_contributor_task_dependencies_prefixed() {
        // Test that internal dependencies get the prefix too
        let contrib = Contributor {
            id: "test".to_string(),
            when: Some(ContributorActivation {
                always: Some(true),
                ..Default::default()
            }),
            tasks: vec![
                ContributorTask {
                    id: "test.first".to_string(),
                    command: Some("echo".to_string()),
                    args: vec!["first".to_string()],
                    ..Default::default()
                },
                ContributorTask {
                    id: "test.second".to_string(),
                    command: Some("echo".to_string()),
                    args: vec!["second".to_string()],
                    depends_on: vec!["test.first".to_string()], // Reference without prefix
                    ..Default::default()
                },
            ],
            auto_associate: None,
        };

        let ctx = ContributorContext::default();
        let contributors = [contrib];
        let engine = ContributorEngine::new(&contributors, ctx);
        let mut tasks: HashMap<String, TaskNode> = HashMap::new();

        engine.apply(&mut tasks).unwrap();

        // Check that the second task's dependency got prefixed
        let second_task = tasks.get("cuenv:contributor:test.second").unwrap();
        if let TaskNode::Task(task) = second_task {
            assert!(
                task.depends_on
                    .iter()
                    .any(|d| d.task_name() == "cuenv:contributor:test.first"),
                "Internal dependency should be prefixed, got: {:?}",
                task.depends_on
            );
        } else {
            panic!("Expected single task");
        }
    }
}