argentor-orchestrator 1.4.7

Multi-agent orchestration, task queues, and deployment for Argentor
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
//! Development team orchestration module.
//!
//! Provides pre-configured development team compositions and workflows for
//! common coding tasks: implement feature, fix bug, refactor, security audit,
//! add tests, code review, optimize, and write documentation.
//!
//! Each workflow defines a sequence of [`WorkflowStep`]s with role assignments,
//! quality gates, and handoff rules. The [`DevTeam`] struct coordinates these
//! workflows using the existing [`AgentRole`](crate::types::AgentRole) and
//! [`AgentProfile`](crate::types::AgentProfile) infrastructure.

use serde::{Deserialize, Serialize};
use std::fmt;

/// A role in a development team.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DevRole {
    /// Plans architecture and decomposes tasks.
    Architect,
    /// Writes production code.
    Implementer,
    /// Writes and runs tests.
    Tester,
    /// Reviews code for quality, security, and style.
    Reviewer,
    /// Diagnoses and fixes bugs.
    Debugger,
    /// Handles CI/CD, deployment, infrastructure.
    DevOps,
    /// Audits for security vulnerabilities.
    SecurityAuditor,
    /// Writes documentation.
    Documenter,
}

impl fmt::Display for DevRole {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Architect => write!(f, "Architect"),
            Self::Implementer => write!(f, "Implementer"),
            Self::Tester => write!(f, "Tester"),
            Self::Reviewer => write!(f, "Reviewer"),
            Self::Debugger => write!(f, "Debugger"),
            Self::DevOps => write!(f, "DevOps"),
            Self::SecurityAuditor => write!(f, "SecurityAuditor"),
            Self::Documenter => write!(f, "Documenter"),
        }
    }
}

/// A development workflow template.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DevWorkflow {
    /// Full feature implementation: plan -> code -> test -> review.
    ImplementFeature,
    /// Bug fix: diagnose -> fix -> test -> review.
    FixBug,
    /// Code refactoring: analyze -> refactor -> test -> review.
    Refactor,
    /// Add test coverage: analyze -> write tests -> verify.
    AddTests,
    /// Security audit: scan -> report -> remediate -> verify.
    SecurityAudit,
    /// Code review: review -> feedback -> iterate.
    CodeReview,
    /// Performance optimization: profile -> optimize -> benchmark -> review.
    Optimize,
    /// Documentation: analyze -> write -> review.
    WriteDocumentation,
}

impl fmt::Display for DevWorkflow {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ImplementFeature => write!(f, "Implement Feature"),
            Self::FixBug => write!(f, "Fix Bug"),
            Self::Refactor => write!(f, "Refactor"),
            Self::AddTests => write!(f, "Add Tests"),
            Self::SecurityAudit => write!(f, "Security Audit"),
            Self::CodeReview => write!(f, "Code Review"),
            Self::Optimize => write!(f, "Optimize"),
            Self::WriteDocumentation => write!(f, "Write Documentation"),
        }
    }
}

/// All available workflow variants.
const ALL_WORKFLOWS: [DevWorkflow; 8] = [
    DevWorkflow::ImplementFeature,
    DevWorkflow::FixBug,
    DevWorkflow::Refactor,
    DevWorkflow::AddTests,
    DevWorkflow::SecurityAudit,
    DevWorkflow::CodeReview,
    DevWorkflow::Optimize,
    DevWorkflow::WriteDocumentation,
];

/// A step in a workflow with role assignment and handoff rules.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowStep {
    /// Step number (1-based).
    pub order: usize,
    /// Which role executes this step.
    pub role: DevRole,
    /// What this step does.
    pub action: String,
    /// Detailed instructions for the agent.
    pub instructions: String,
    /// What the agent should produce (input for next step).
    pub expected_output: String,
    /// Quality gate: minimum conditions to proceed.
    pub gate: Option<QualityGate>,
    /// Whether this step can be retried on failure.
    pub retryable: bool,
    /// Maximum retries.
    pub max_retries: usize,
}

/// Quality gate that must pass before proceeding.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QualityGate {
    /// Human-readable description of the gate.
    pub description: String,
    /// Type of check.
    pub check_type: GateCheck,
}

/// The type of quality check for a gate.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GateCheck {
    /// All tests must pass.
    TestsPass,
    /// Code review score must be above threshold (0-100).
    ReviewScore {
        /// Minimum acceptable score.
        min_score: u32,
    },
    /// No security findings above a severity.
    NoSecurityFindings {
        /// Maximum acceptable severity (e.g. "low", "medium", "high", "critical").
        max_severity: String,
    },
    /// Code compiles without errors.
    CompileSuccess,
    /// Custom check description.
    Custom {
        /// Description of the custom check.
        check: String,
    },
}

/// Configuration for a development team.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DevTeamConfig {
    /// Team name.
    pub name: String,
    /// Roles present in the team.
    pub roles: Vec<DevRole>,
    /// Model tier preference per role.
    pub role_models: Vec<(DevRole, String)>,
    /// Maximum concurrent steps.
    pub max_parallel: usize,
    /// Whether to enforce quality gates.
    pub enforce_gates: bool,
    /// Maximum total iterations across all workflow steps.
    pub max_iterations: usize,
}

/// Result of running a workflow.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowResult {
    /// The workflow that was executed.
    pub workflow: DevWorkflow,
    /// Final status of the workflow.
    pub status: WorkflowStatus,
    /// Number of steps completed.
    pub steps_completed: usize,
    /// Total number of steps in the workflow.
    pub steps_total: usize,
    /// Artifacts produced by the workflow.
    pub artifacts: Vec<WorkflowArtifact>,
    /// Quality scores collected during execution (label, score).
    pub quality_scores: Vec<(String, f32)>,
    /// Human-readable notes about the execution.
    pub notes: Vec<String>,
}

/// Status of a workflow execution.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WorkflowStatus {
    /// Workflow completed successfully.
    Completed,
    /// Workflow failed at a specific step.
    Failed {
        /// Step number where the failure occurred.
        step: usize,
        /// Reason for the failure.
        reason: String,
    },
    /// Workflow blocked by a quality gate.
    GateBlocked {
        /// Step number where the gate blocked progress.
        step: usize,
        /// Description of the gate that blocked.
        gate: String,
    },
    /// Workflow was cancelled.
    Cancelled,
}

/// An artifact produced during a workflow step.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowArtifact {
    /// Name of the artifact.
    pub name: String,
    /// Type of artifact.
    pub artifact_type: ArtifactType,
    /// Content of the artifact.
    pub content: String,
}

/// The type of artifact produced by a workflow step.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ArtifactType {
    /// Architecture or implementation plan.
    Plan,
    /// Source code.
    Code,
    /// Test execution results.
    TestResults,
    /// Code review report.
    ReviewReport,
    /// Security audit report.
    SecurityReport,
    /// Written documentation.
    Documentation,
    /// Code diff.
    Diff,
}

/// The development team orchestrator.
///
/// Coordinates a team of agents with specific roles through predefined
/// workflows. Each workflow defines a sequence of steps with quality gates
/// and handoff rules between agents.
#[derive(Debug, Clone)]
pub struct DevTeam {
    config: DevTeamConfig,
}

impl DevTeam {
    /// Create a default full-stack development team with all roles.
    pub fn full_stack() -> Self {
        Self {
            config: DevTeamConfig {
                name: "Full-Stack Team".to_string(),
                roles: vec![
                    DevRole::Architect,
                    DevRole::Implementer,
                    DevRole::Tester,
                    DevRole::Reviewer,
                    DevRole::Debugger,
                    DevRole::DevOps,
                    DevRole::SecurityAuditor,
                    DevRole::Documenter,
                ],
                role_models: default_role_models(),
                max_parallel: 3,
                enforce_gates: true,
                max_iterations: 50,
            },
        }
    }

    /// Create a minimal team (implementer + tester).
    pub fn minimal() -> Self {
        Self {
            config: DevTeamConfig {
                name: "Minimal Team".to_string(),
                roles: vec![DevRole::Implementer, DevRole::Tester],
                role_models: vec![
                    (DevRole::Implementer, "balanced".to_string()),
                    (DevRole::Tester, "balanced".to_string()),
                ],
                max_parallel: 1,
                enforce_gates: false,
                max_iterations: 20,
            },
        }
    }

    /// Create a security-focused team.
    pub fn security_team() -> Self {
        Self {
            config: DevTeamConfig {
                name: "Security Team".to_string(),
                roles: vec![
                    DevRole::SecurityAuditor,
                    DevRole::Implementer,
                    DevRole::Reviewer,
                    DevRole::Tester,
                ],
                role_models: vec![
                    (DevRole::SecurityAuditor, "powerful".to_string()),
                    (DevRole::Implementer, "balanced".to_string()),
                    (DevRole::Reviewer, "powerful".to_string()),
                    (DevRole::Tester, "balanced".to_string()),
                ],
                max_parallel: 2,
                enforce_gates: true,
                max_iterations: 40,
            },
        }
    }

    /// Create with custom config.
    pub fn with_config(config: DevTeamConfig) -> Self {
        Self { config }
    }

    /// Get the workflow steps for a given workflow type.
    pub fn workflow_steps(&self, workflow: DevWorkflow) -> Vec<WorkflowStep> {
        match workflow {
            DevWorkflow::ImplementFeature => build_implement_feature_steps(),
            DevWorkflow::FixBug => build_fix_bug_steps(),
            DevWorkflow::Refactor => build_refactor_steps(),
            DevWorkflow::AddTests => build_add_tests_steps(),
            DevWorkflow::SecurityAudit => build_security_audit_steps(),
            DevWorkflow::CodeReview => build_code_review_steps(),
            DevWorkflow::Optimize => build_optimize_steps(),
            DevWorkflow::WriteDocumentation => build_write_documentation_steps(),
        }
    }

    /// Get the roles needed for a workflow.
    pub fn required_roles(&self, workflow: DevWorkflow) -> Vec<DevRole> {
        let steps = self.workflow_steps(workflow);
        let mut roles: Vec<DevRole> = Vec::new();
        for step in &steps {
            if !roles.contains(&step.role) {
                roles.push(step.role);
            }
        }
        roles
    }

    /// Check if the team has all required roles for a workflow.
    pub fn can_run_workflow(&self, workflow: DevWorkflow) -> bool {
        let required = self.required_roles(workflow);
        required.iter().all(|r| self.config.roles.contains(r))
    }

    /// Get the model tier recommendation for a role.
    pub fn model_for_role(&self, role: DevRole) -> String {
        self.config
            .role_models
            .iter()
            .find(|(r, _)| *r == role)
            .map(|(_, m)| m.clone())
            .unwrap_or_else(|| default_model_tier(role).to_string())
    }

    /// Validate a workflow result against quality gates.
    ///
    /// Returns `true` if the step's gate conditions are satisfied by the
    /// provided artifacts, or if the step has no gate. When gates are not
    /// enforced in the team config, always returns `true`.
    pub fn validate_gates(
        &self,
        workflow: DevWorkflow,
        step: usize,
        artifacts: &[WorkflowArtifact],
    ) -> bool {
        if !self.config.enforce_gates {
            return true;
        }

        let steps = self.workflow_steps(workflow);
        let target_step = steps.iter().find(|s| s.order == step);

        let target_step = match target_step {
            Some(s) => s,
            None => return false,
        };

        let gate = match &target_step.gate {
            Some(g) => g,
            None => return true,
        };

        match &gate.check_type {
            GateCheck::TestsPass => artifacts
                .iter()
                .any(|a| a.artifact_type == ArtifactType::TestResults),
            GateCheck::ReviewScore { min_score } => {
                // A review report artifact must exist.
                // In a real implementation, we'd parse the score from the content.
                // Here we check that a review report exists and that min_score
                // is within the valid range.
                *min_score <= 100
                    && artifacts
                        .iter()
                        .any(|a| a.artifact_type == ArtifactType::ReviewReport)
            }
            GateCheck::NoSecurityFindings { .. } => artifacts
                .iter()
                .any(|a| a.artifact_type == ArtifactType::SecurityReport),
            GateCheck::CompileSuccess => artifacts
                .iter()
                .any(|a| a.artifact_type == ArtifactType::Code),
            GateCheck::Custom { .. } => {
                // Custom gates pass if any artifact is present.
                !artifacts.is_empty()
            }
        }
    }

    /// Generate the system prompt for an agent in a specific role.
    pub fn role_system_prompt(&self, role: DevRole) -> String {
        match role {
            DevRole::Architect => "You are a senior software architect. \
                Analyze requirements, decompose into tasks, identify dependencies, \
                estimate effort, and assess risk. Produce detailed implementation \
                plans with clear step-by-step instructions. Focus on modularity, \
                scalability, and maintainability."
                .to_string(),
            DevRole::Implementer => "You are a senior software engineer. \
                Write clean, efficient, well-tested code. Follow existing \
                conventions and project patterns. Minimize changes to reduce \
                risk. Never introduce security vulnerabilities. Use proper error \
                handling and avoid panics in production code."
                .to_string(),
            DevRole::Tester => "You are a senior QA engineer. Write comprehensive \
                tests covering happy paths, edge cases, error conditions, and \
                boundary values. Use descriptive test names following the pattern \
                test_<function>_<scenario>. Ensure tests are deterministic, fast, \
                and independent of each other."
                .to_string(),
            DevRole::Reviewer => "You are a code reviewer. Evaluate code across \
                correctness, security, performance, style, and test coverage. \
                Provide specific, actionable feedback with line references. Score \
                the review on a 0-100 scale. Flag blocking issues that must be \
                addressed before merging."
                .to_string(),
            DevRole::Debugger => "You are a senior debugger and diagnostician. \
                Reproduce bugs systematically, identify root causes through \
                careful analysis, and propose minimal targeted fixes. Document \
                the investigation process and findings clearly. Always verify \
                that proposed fixes address the root cause, not just symptoms."
                .to_string(),
            DevRole::DevOps => "You are a DevOps engineer. Handle CI/CD pipelines, \
                deployment configurations, infrastructure as code, and operational \
                tooling. Follow best practices for reproducibility, security \
                hardening, and monitoring. Ensure rollback safety."
                .to_string(),
            DevRole::SecurityAuditor => "You are a security auditor. Scan code \
                for vulnerabilities including OWASP Top 10, insecure dependencies, \
                secrets exposure, improper access controls, and cryptographic \
                weaknesses. Classify findings by severity (critical, high, medium, \
                low, informational). Provide actionable remediation guidance."
                .to_string(),
            DevRole::Documenter => "You are a technical writer. Write clear, \
                accurate, and comprehensive documentation. Include code examples, \
                API references, usage patterns, and architecture overviews. Follow \
                the project's existing documentation style and conventions."
                .to_string(),
        }
    }

    /// Get the handoff message between steps (what to pass from step N to N+1).
    pub fn handoff_message(
        &self,
        from_step: &WorkflowStep,
        artifacts: &[WorkflowArtifact],
    ) -> String {
        let artifact_summary: Vec<String> = artifacts
            .iter()
            .map(|a| format!("[{}] {}", a.name, truncate_content(&a.content, 200)))
            .collect();

        format!(
            "Handoff from Step {} ({}, {}): {}\n\nArtifacts produced:\n{}",
            from_step.order,
            from_step.role,
            from_step.action,
            from_step.expected_output,
            if artifact_summary.is_empty() {
                "  (none)".to_string()
            } else {
                artifact_summary
                    .iter()
                    .map(|s| format!("  - {s}"))
                    .collect::<Vec<_>>()
                    .join("\n")
            }
        )
    }

    /// Get all available workflows.
    pub fn available_workflows(&self) -> Vec<DevWorkflow> {
        ALL_WORKFLOWS.to_vec()
    }

    /// Describe a workflow in human-readable form.
    pub fn describe_workflow(&self, workflow: DevWorkflow) -> String {
        let steps = self.workflow_steps(workflow);
        let step_descriptions: Vec<String> = steps
            .iter()
            .map(|s| {
                let gate_info = s
                    .gate
                    .as_ref()
                    .map(|g| format!(" [Gate: {}]", g.description))
                    .unwrap_or_default();
                format!(
                    "  {}. {} ({}): {}{}",
                    s.order, s.role, s.action, s.expected_output, gate_info
                )
            })
            .collect();

        format!(
            "Workflow: {workflow}\nSteps ({}):\n{}",
            steps.len(),
            step_descriptions.join("\n")
        )
    }

    /// Get team summary.
    pub fn summary(&self) -> String {
        let roles: Vec<String> = self
            .config
            .roles
            .iter()
            .map(std::string::ToString::to_string)
            .collect();
        let runnable: Vec<String> = ALL_WORKFLOWS
            .iter()
            .filter(|w| self.can_run_workflow(**w))
            .map(std::string::ToString::to_string)
            .collect();

        format!(
            "Team: {}\nRoles ({}): {}\nMax parallel: {}\nEnforce gates: {}\nMax iterations: {}\nRunnable workflows ({}): {}",
            self.config.name,
            self.config.roles.len(),
            roles.join(", "),
            self.config.max_parallel,
            self.config.enforce_gates,
            self.config.max_iterations,
            runnable.len(),
            runnable.join(", "),
        )
    }
}

// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------

/// Truncate content to a maximum length, appending "..." if truncated.
fn truncate_content(content: &str, max_len: usize) -> String {
    if content.len() <= max_len {
        content.to_string()
    } else {
        let truncated: String = content.chars().take(max_len).collect();
        format!("{truncated}...")
    }
}

/// Default model tier for each role.
fn default_model_tier(role: DevRole) -> &'static str {
    match role {
        DevRole::Architect => "powerful",
        DevRole::Implementer => "balanced",
        DevRole::Tester => "balanced",
        DevRole::Reviewer => "powerful",
        DevRole::Debugger => "powerful",
        DevRole::DevOps => "fast",
        DevRole::SecurityAuditor => "powerful",
        DevRole::Documenter => "fast",
    }
}

/// Default role-to-model-tier mapping for all roles.
fn default_role_models() -> Vec<(DevRole, String)> {
    vec![
        (DevRole::Architect, "powerful".to_string()),
        (DevRole::Implementer, "balanced".to_string()),
        (DevRole::Tester, "balanced".to_string()),
        (DevRole::Reviewer, "powerful".to_string()),
        (DevRole::Debugger, "powerful".to_string()),
        (DevRole::DevOps, "fast".to_string()),
        (DevRole::SecurityAuditor, "powerful".to_string()),
        (DevRole::Documenter, "fast".to_string()),
    ]
}

// ---------------------------------------------------------------------------
// Workflow step builders
// ---------------------------------------------------------------------------

/// Build steps for the ImplementFeature workflow.
fn build_implement_feature_steps() -> Vec<WorkflowStep> {
    vec![
        WorkflowStep {
            order: 1,
            role: DevRole::Architect,
            action: "Analyze requirements and create implementation plan".to_string(),
            instructions: "Review the feature requirements thoroughly. Break them down \
                into concrete implementation tasks with clear acceptance criteria. \
                Identify dependencies between tasks, estimate effort for each, and \
                assess technical risks. Produce a detailed plan with step-by-step \
                instructions for the implementer."
                .to_string(),
            expected_output:
                "Detailed implementation plan with tasks, dependencies, and risk assessment"
                    .to_string(),
            gate: Some(QualityGate {
                description: "Implementation plan produced".to_string(),
                check_type: GateCheck::Custom {
                    check: "Plan document exists and contains task breakdown".to_string(),
                },
            }),
            retryable: true,
            max_retries: 2,
        },
        WorkflowStep {
            order: 2,
            role: DevRole::Implementer,
            action: "Write production code following the plan".to_string(),
            instructions: "Implement the feature following the architect's plan. \
                Write clean, idiomatic code that follows project conventions. \
                Handle errors properly, avoid panics, and ensure the code compiles \
                without warnings."
                .to_string(),
            expected_output: "Production code that compiles and implements the planned feature"
                .to_string(),
            gate: Some(QualityGate {
                description: "Code compiles successfully".to_string(),
                check_type: GateCheck::CompileSuccess,
            }),
            retryable: true,
            max_retries: 3,
        },
        WorkflowStep {
            order: 3,
            role: DevRole::Tester,
            action: "Write and run tests for the new feature".to_string(),
            instructions: "Write comprehensive tests for the implemented feature. \
                Cover happy paths, edge cases, error conditions, and boundary values. \
                Ensure tests are deterministic and independent. Run all tests and \
                verify they pass."
                .to_string(),
            expected_output: "Test suite with passing results and coverage report".to_string(),
            gate: Some(QualityGate {
                description: "All tests pass".to_string(),
                check_type: GateCheck::TestsPass,
            }),
            retryable: true,
            max_retries: 3,
        },
        WorkflowStep {
            order: 4,
            role: DevRole::Reviewer,
            action: "Review code for quality, security, and style".to_string(),
            instructions: "Review the implementation and tests for correctness, \
                security vulnerabilities, performance issues, coding style, and \
                test coverage. Provide specific, actionable feedback with line \
                references. Score the review on a 0-100 scale."
                .to_string(),
            expected_output: "Review report with score, findings, and recommendations".to_string(),
            gate: Some(QualityGate {
                description: "Review score >= 70".to_string(),
                check_type: GateCheck::ReviewScore { min_score: 70 },
            }),
            retryable: false,
            max_retries: 0,
        },
        WorkflowStep {
            order: 5,
            role: DevRole::Documenter,
            action: "Update documentation for the new feature".to_string(),
            instructions: "Write or update documentation for the newly implemented \
                feature. Include API references, usage examples, and any \
                configuration changes. Follow the project's documentation style."
                .to_string(),
            expected_output: "Updated documentation covering the new feature".to_string(),
            gate: None,
            retryable: true,
            max_retries: 1,
        },
    ]
}

/// Build steps for the FixBug workflow.
fn build_fix_bug_steps() -> Vec<WorkflowStep> {
    vec![
        WorkflowStep {
            order: 1,
            role: DevRole::Debugger,
            action: "Reproduce and diagnose the root cause".to_string(),
            instructions: "Reproduce the bug systematically. Analyze logs, stack traces, \
                and code paths to identify the root cause. Document the reproduction \
                steps and root cause analysis clearly."
                .to_string(),
            expected_output: "Root cause analysis with reproduction steps".to_string(),
            gate: Some(QualityGate {
                description: "Root cause identified".to_string(),
                check_type: GateCheck::Custom {
                    check: "Root cause analysis document produced".to_string(),
                },
            }),
            retryable: true,
            max_retries: 2,
        },
        WorkflowStep {
            order: 2,
            role: DevRole::Implementer,
            action: "Apply the fix with minimal changes".to_string(),
            instructions: "Implement a targeted fix based on the root cause analysis. \
                Minimize code changes to reduce regression risk. Ensure the fix \
                addresses the root cause, not just symptoms. The code must compile \
                without errors or warnings."
                .to_string(),
            expected_output: "Bug fix code that compiles successfully".to_string(),
            gate: Some(QualityGate {
                description: "Code compiles successfully".to_string(),
                check_type: GateCheck::CompileSuccess,
            }),
            retryable: true,
            max_retries: 3,
        },
        WorkflowStep {
            order: 3,
            role: DevRole::Tester,
            action: "Write regression test and verify fix".to_string(),
            instructions: "Write a regression test that reproduces the original bug \
                and verifies the fix. Run the full test suite to check for regressions. \
                The regression test must fail without the fix and pass with it."
                .to_string(),
            expected_output: "Regression test and full test suite results".to_string(),
            gate: Some(QualityGate {
                description: "All tests pass".to_string(),
                check_type: GateCheck::TestsPass,
            }),
            retryable: true,
            max_retries: 3,
        },
        WorkflowStep {
            order: 4,
            role: DevRole::Reviewer,
            action: "Review the fix for correctness and side effects".to_string(),
            instructions: "Review the bug fix for correctness, potential side effects, \
                and regression risk. Verify the fix addresses the root cause. Check \
                that the regression test is adequate. Score on a 0-100 scale."
                .to_string(),
            expected_output: "Review report with assessment of fix quality".to_string(),
            gate: Some(QualityGate {
                description: "Review score >= 70".to_string(),
                check_type: GateCheck::ReviewScore { min_score: 70 },
            }),
            retryable: false,
            max_retries: 0,
        },
    ]
}

/// Build steps for the Refactor workflow.
fn build_refactor_steps() -> Vec<WorkflowStep> {
    vec![
        WorkflowStep {
            order: 1,
            role: DevRole::Architect,
            action: "Analyze code structure and plan refactoring approach".to_string(),
            instructions: "Analyze the current code structure and identify areas for \
                improvement. Design the refactoring approach with clear goals \
                (readability, performance, modularity). Ensure the plan preserves \
                existing behavior."
                .to_string(),
            expected_output: "Refactoring plan with goals and approach".to_string(),
            gate: Some(QualityGate {
                description: "Refactoring plan produced".to_string(),
                check_type: GateCheck::Custom {
                    check: "Plan document exists with refactoring strategy".to_string(),
                },
            }),
            retryable: true,
            max_retries: 2,
        },
        WorkflowStep {
            order: 2,
            role: DevRole::Implementer,
            action: "Apply refactoring changes".to_string(),
            instructions: "Apply the planned refactoring changes. Make incremental, \
                verifiable changes. Preserve all existing behavior. The code must \
                compile without errors or warnings."
                .to_string(),
            expected_output: "Refactored code that compiles successfully".to_string(),
            gate: Some(QualityGate {
                description: "Code compiles successfully".to_string(),
                check_type: GateCheck::CompileSuccess,
            }),
            retryable: true,
            max_retries: 3,
        },
        WorkflowStep {
            order: 3,
            role: DevRole::Tester,
            action: "Run existing tests and verify no regressions".to_string(),
            instructions: "Run the full test suite to verify the refactoring did not \
                introduce any regressions. All existing tests must continue to pass. \
                Add tests for any new code paths introduced by the refactoring."
                .to_string(),
            expected_output: "Test results showing no regressions".to_string(),
            gate: Some(QualityGate {
                description: "All tests pass".to_string(),
                check_type: GateCheck::TestsPass,
            }),
            retryable: true,
            max_retries: 3,
        },
        WorkflowStep {
            order: 4,
            role: DevRole::Reviewer,
            action: "Review refactoring for improved design quality".to_string(),
            instructions: "Review the refactored code for design improvement, \
                readability, and correctness. Verify behavior is preserved. \
                Assess whether the refactoring achieved its stated goals. \
                Score on a 0-100 scale."
                .to_string(),
            expected_output: "Review report with design quality assessment".to_string(),
            gate: Some(QualityGate {
                description: "Review score >= 70".to_string(),
                check_type: GateCheck::ReviewScore { min_score: 70 },
            }),
            retryable: false,
            max_retries: 0,
        },
    ]
}

/// Build steps for the AddTests workflow.
fn build_add_tests_steps() -> Vec<WorkflowStep> {
    vec![
        WorkflowStep {
            order: 1,
            role: DevRole::Tester,
            action: "Analyze code and identify untested paths".to_string(),
            instructions: "Analyze the codebase to identify untested code paths, \
                functions, and edge cases. Prioritize critical paths and security- \
                sensitive code. Produce a test plan with identified gaps."
                .to_string(),
            expected_output: "Test plan identifying coverage gaps and priorities".to_string(),
            gate: None,
            retryable: true,
            max_retries: 1,
        },
        WorkflowStep {
            order: 2,
            role: DevRole::Tester,
            action: "Write comprehensive tests".to_string(),
            instructions: "Write tests to fill the identified coverage gaps. Include \
                unit tests, integration tests, and edge case tests. Use descriptive \
                test names and follow project conventions. All tests must pass."
                .to_string(),
            expected_output: "New test suite with passing results".to_string(),
            gate: Some(QualityGate {
                description: "All tests pass".to_string(),
                check_type: GateCheck::TestsPass,
            }),
            retryable: true,
            max_retries: 3,
        },
        WorkflowStep {
            order: 3,
            role: DevRole::Reviewer,
            action: "Review test quality and coverage".to_string(),
            instructions: "Review the new tests for quality, completeness, and \
                correctness. Verify tests actually test meaningful behavior and are \
                not trivial. Assess coverage improvement. Score on a 0-100 scale."
                .to_string(),
            expected_output: "Review report with test quality assessment".to_string(),
            gate: Some(QualityGate {
                description: "Review score >= 60".to_string(),
                check_type: GateCheck::ReviewScore { min_score: 60 },
            }),
            retryable: false,
            max_retries: 0,
        },
    ]
}

/// Build steps for the SecurityAudit workflow.
fn build_security_audit_steps() -> Vec<WorkflowStep> {
    vec![
        WorkflowStep {
            order: 1,
            role: DevRole::SecurityAuditor,
            action: "Scan code for security vulnerabilities".to_string(),
            instructions: "Perform a comprehensive security scan of the codebase. \
                Check for OWASP Top 10 vulnerabilities, insecure dependencies, \
                secrets exposure, improper access controls, and cryptographic \
                weaknesses. Classify findings by severity."
                .to_string(),
            expected_output: "Security scan results with classified findings".to_string(),
            gate: Some(QualityGate {
                description: "Security scan completed".to_string(),
                check_type: GateCheck::Custom {
                    check: "Security scan report produced with findings classified".to_string(),
                },
            }),
            retryable: true,
            max_retries: 2,
        },
        WorkflowStep {
            order: 2,
            role: DevRole::SecurityAuditor,
            action: "Generate detailed security report with findings".to_string(),
            instructions: "Compile a detailed security audit report. Include all \
                findings with severity classification, affected code locations, \
                potential impact, and recommended remediations. Prioritize \
                findings by risk."
                .to_string(),
            expected_output: "Detailed security audit report".to_string(),
            gate: None,
            retryable: true,
            max_retries: 1,
        },
        WorkflowStep {
            order: 3,
            role: DevRole::Implementer,
            action: "Apply security remediations".to_string(),
            instructions: "Implement fixes for the security findings identified in \
                the audit report. Prioritize critical and high severity findings. \
                Follow security best practices and the auditor's recommendations. \
                The code must compile without errors."
                .to_string(),
            expected_output: "Remediated code that compiles successfully".to_string(),
            gate: Some(QualityGate {
                description: "Code compiles successfully".to_string(),
                check_type: GateCheck::CompileSuccess,
            }),
            retryable: true,
            max_retries: 3,
        },
        WorkflowStep {
            order: 4,
            role: DevRole::SecurityAuditor,
            action: "Verify remediations and re-scan".to_string(),
            instructions: "Re-scan the remediated code to verify that security fixes \
                are effective. Confirm that no new vulnerabilities were introduced. \
                Verify there are no remaining critical or high severity findings."
                .to_string(),
            expected_output: "Verification report confirming remediations".to_string(),
            gate: Some(QualityGate {
                description: "No critical security findings".to_string(),
                check_type: GateCheck::NoSecurityFindings {
                    max_severity: "high".to_string(),
                },
            }),
            retryable: true,
            max_retries: 2,
        },
    ]
}

/// Build steps for the CodeReview workflow.
fn build_code_review_steps() -> Vec<WorkflowStep> {
    vec![
        WorkflowStep {
            order: 1,
            role: DevRole::Reviewer,
            action: "Perform detailed code review across all dimensions".to_string(),
            instructions: "Review the code across correctness, security, performance, \
                readability, maintainability, and test coverage. Provide specific, \
                actionable feedback with file and line references. Identify blocking \
                and non-blocking issues."
                .to_string(),
            expected_output: "Detailed code review with categorized feedback".to_string(),
            gate: None,
            retryable: true,
            max_retries: 1,
        },
        WorkflowStep {
            order: 2,
            role: DevRole::Implementer,
            action: "Address review feedback".to_string(),
            instructions: "Address all blocking issues from the code review. Apply \
                non-blocking suggestions where appropriate. Explain any feedback \
                that was intentionally not addressed. The code must compile."
                .to_string(),
            expected_output: "Updated code addressing review feedback".to_string(),
            gate: Some(QualityGate {
                description: "Code compiles successfully".to_string(),
                check_type: GateCheck::CompileSuccess,
            }),
            retryable: true,
            max_retries: 3,
        },
        WorkflowStep {
            order: 3,
            role: DevRole::Reviewer,
            action: "Verify changes address all feedback".to_string(),
            instructions: "Re-review the updated code to verify all blocking feedback \
                has been addressed. Check that fixes are correct and no new issues \
                were introduced. Score on a 0-100 scale."
                .to_string(),
            expected_output: "Final review report with approval status".to_string(),
            gate: Some(QualityGate {
                description: "Review score >= 80".to_string(),
                check_type: GateCheck::ReviewScore { min_score: 80 },
            }),
            retryable: false,
            max_retries: 0,
        },
    ]
}

/// Build steps for the Optimize workflow.
fn build_optimize_steps() -> Vec<WorkflowStep> {
    vec![
        WorkflowStep {
            order: 1,
            role: DevRole::Architect,
            action: "Profile and identify performance bottlenecks".to_string(),
            instructions: "Analyze the codebase to identify performance bottlenecks. \
                Profile critical paths, measure latencies, and identify hot spots. \
                Prioritize optimization targets by impact and effort."
                .to_string(),
            expected_output: "Performance analysis with identified bottlenecks".to_string(),
            gate: None,
            retryable: true,
            max_retries: 1,
        },
        WorkflowStep {
            order: 2,
            role: DevRole::Implementer,
            action: "Apply optimizations".to_string(),
            instructions: "Implement performance optimizations for the identified \
                bottlenecks. Use algorithmic improvements, caching, batching, or \
                concurrency as appropriate. Maintain code readability and correctness. \
                The code must compile."
                .to_string(),
            expected_output: "Optimized code that compiles successfully".to_string(),
            gate: Some(QualityGate {
                description: "Code compiles successfully".to_string(),
                check_type: GateCheck::CompileSuccess,
            }),
            retryable: true,
            max_retries: 3,
        },
        WorkflowStep {
            order: 3,
            role: DevRole::Tester,
            action: "Run benchmarks and verify improvement".to_string(),
            instructions: "Run performance benchmarks comparing before and after the \
                optimization. Verify that the optimization improves performance \
                without introducing regressions. Run the full test suite."
                .to_string(),
            expected_output: "Benchmark results and test suite results".to_string(),
            gate: Some(QualityGate {
                description: "All tests pass".to_string(),
                check_type: GateCheck::TestsPass,
            }),
            retryable: true,
            max_retries: 2,
        },
        WorkflowStep {
            order: 4,
            role: DevRole::Reviewer,
            action: "Review optimizations for correctness".to_string(),
            instructions: "Review the optimization changes for correctness and \
                maintainability. Verify the performance improvement is real and \
                not a measurement artifact. Check for algorithmic correctness. \
                Score on a 0-100 scale."
                .to_string(),
            expected_output: "Review report assessing optimization quality".to_string(),
            gate: Some(QualityGate {
                description: "Review score >= 70".to_string(),
                check_type: GateCheck::ReviewScore { min_score: 70 },
            }),
            retryable: false,
            max_retries: 0,
        },
    ]
}

/// Build steps for the WriteDocumentation workflow.
fn build_write_documentation_steps() -> Vec<WorkflowStep> {
    vec![
        WorkflowStep {
            order: 1,
            role: DevRole::Architect,
            action: "Analyze codebase and identify documentation gaps".to_string(),
            instructions: "Analyze the codebase structure, public APIs, and existing \
                documentation. Identify undocumented or poorly documented areas. \
                Prioritize documentation needs and produce an outline."
                .to_string(),
            expected_output: "Documentation gap analysis and outline".to_string(),
            gate: None,
            retryable: true,
            max_retries: 1,
        },
        WorkflowStep {
            order: 2,
            role: DevRole::Documenter,
            action: "Write comprehensive documentation".to_string(),
            instructions: "Write documentation following the outline. Include API \
                references, usage examples, architecture overviews, and getting \
                started guides. Use proper formatting and follow the project's \
                documentation style."
                .to_string(),
            expected_output: "Comprehensive documentation content".to_string(),
            gate: None,
            retryable: true,
            max_retries: 2,
        },
        WorkflowStep {
            order: 3,
            role: DevRole::Reviewer,
            action: "Review documentation for accuracy and completeness".to_string(),
            instructions: "Review the documentation for technical accuracy, \
                completeness, clarity, and consistency with the actual code. \
                Verify code examples compile and work correctly. Score on a \
                0-100 scale."
                .to_string(),
            expected_output: "Review report on documentation quality".to_string(),
            gate: Some(QualityGate {
                description: "Review score >= 60".to_string(),
                check_type: GateCheck::ReviewScore { min_score: 60 },
            }),
            retryable: false,
            max_retries: 0,
        },
    ]
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    #[test]
    fn test_full_stack_team() {
        let team = DevTeam::full_stack();
        assert_eq!(team.config.name, "Full-Stack Team");
        assert_eq!(team.config.roles.len(), 8);
        assert!(team.config.enforce_gates);
        assert_eq!(team.config.max_parallel, 3);

        // Every DevRole should be present
        assert!(team.config.roles.contains(&DevRole::Architect));
        assert!(team.config.roles.contains(&DevRole::Implementer));
        assert!(team.config.roles.contains(&DevRole::Tester));
        assert!(team.config.roles.contains(&DevRole::Reviewer));
        assert!(team.config.roles.contains(&DevRole::Debugger));
        assert!(team.config.roles.contains(&DevRole::DevOps));
        assert!(team.config.roles.contains(&DevRole::SecurityAuditor));
        assert!(team.config.roles.contains(&DevRole::Documenter));
    }

    #[test]
    fn test_minimal_team() {
        let team = DevTeam::minimal();
        assert_eq!(team.config.name, "Minimal Team");
        assert_eq!(team.config.roles.len(), 2);
        assert!(team.config.roles.contains(&DevRole::Implementer));
        assert!(team.config.roles.contains(&DevRole::Tester));
        assert!(!team.config.enforce_gates);
        assert_eq!(team.config.max_parallel, 1);
    }

    #[test]
    fn test_security_team() {
        let team = DevTeam::security_team();
        assert_eq!(team.config.name, "Security Team");
        assert!(team.config.roles.contains(&DevRole::SecurityAuditor));
        assert!(team.config.roles.contains(&DevRole::Implementer));
        assert!(team.config.roles.contains(&DevRole::Reviewer));
        assert!(team.config.roles.contains(&DevRole::Tester));
        assert!(team.config.enforce_gates);
    }

    #[test]
    fn test_workflow_implement_feature() {
        let team = DevTeam::full_stack();
        let steps = team.workflow_steps(DevWorkflow::ImplementFeature);
        assert_eq!(steps.len(), 5);
        assert_eq!(steps[0].role, DevRole::Architect);
        assert_eq!(steps[1].role, DevRole::Implementer);
        assert_eq!(steps[2].role, DevRole::Tester);
        assert_eq!(steps[3].role, DevRole::Reviewer);
        assert_eq!(steps[4].role, DevRole::Documenter);
        // Last step has no gate
        assert!(steps[4].gate.is_none());
    }

    #[test]
    fn test_workflow_fix_bug() {
        let team = DevTeam::full_stack();
        let steps = team.workflow_steps(DevWorkflow::FixBug);
        assert_eq!(steps.len(), 4);
        assert_eq!(steps[0].role, DevRole::Debugger);
        assert_eq!(steps[1].role, DevRole::Implementer);
        assert_eq!(steps[2].role, DevRole::Tester);
        assert_eq!(steps[3].role, DevRole::Reviewer);
    }

    #[test]
    fn test_workflow_refactor() {
        let team = DevTeam::full_stack();
        let steps = team.workflow_steps(DevWorkflow::Refactor);
        assert_eq!(steps.len(), 4);
        assert_eq!(steps[0].role, DevRole::Architect);
        assert_eq!(steps[1].role, DevRole::Implementer);
        assert_eq!(steps[2].role, DevRole::Tester);
        assert_eq!(steps[3].role, DevRole::Reviewer);
    }

    #[test]
    fn test_workflow_add_tests() {
        let team = DevTeam::full_stack();
        let steps = team.workflow_steps(DevWorkflow::AddTests);
        assert_eq!(steps.len(), 3);
        assert_eq!(steps[0].role, DevRole::Tester);
        assert_eq!(steps[1].role, DevRole::Tester);
        assert_eq!(steps[2].role, DevRole::Reviewer);
        // First step has no gate
        assert!(steps[0].gate.is_none());
    }

    #[test]
    fn test_workflow_security_audit() {
        let team = DevTeam::full_stack();
        let steps = team.workflow_steps(DevWorkflow::SecurityAudit);
        assert_eq!(steps.len(), 4);
        assert_eq!(steps[0].role, DevRole::SecurityAuditor);
        assert_eq!(steps[1].role, DevRole::SecurityAuditor);
        assert_eq!(steps[2].role, DevRole::Implementer);
        assert_eq!(steps[3].role, DevRole::SecurityAuditor);
    }

    #[test]
    fn test_workflow_code_review() {
        let team = DevTeam::full_stack();
        let steps = team.workflow_steps(DevWorkflow::CodeReview);
        assert_eq!(steps.len(), 3);
        assert_eq!(steps[0].role, DevRole::Reviewer);
        assert_eq!(steps[1].role, DevRole::Implementer);
        assert_eq!(steps[2].role, DevRole::Reviewer);
        // Final review gate requires score >= 80
        let gate = steps[2].gate.as_ref().unwrap();
        assert_eq!(gate.check_type, GateCheck::ReviewScore { min_score: 80 });
    }

    #[test]
    fn test_workflow_optimize() {
        let team = DevTeam::full_stack();
        let steps = team.workflow_steps(DevWorkflow::Optimize);
        assert_eq!(steps.len(), 4);
        assert_eq!(steps[0].role, DevRole::Architect);
        assert_eq!(steps[1].role, DevRole::Implementer);
        assert_eq!(steps[2].role, DevRole::Tester);
        assert_eq!(steps[3].role, DevRole::Reviewer);
    }

    #[test]
    fn test_workflow_write_docs() {
        let team = DevTeam::full_stack();
        let steps = team.workflow_steps(DevWorkflow::WriteDocumentation);
        assert_eq!(steps.len(), 3);
        assert_eq!(steps[0].role, DevRole::Architect);
        assert_eq!(steps[1].role, DevRole::Documenter);
        assert_eq!(steps[2].role, DevRole::Reviewer);
        // Review gate requires score >= 60
        let gate = steps[2].gate.as_ref().unwrap();
        assert_eq!(gate.check_type, GateCheck::ReviewScore { min_score: 60 });
    }

    #[test]
    fn test_required_roles_feature() {
        let team = DevTeam::full_stack();
        let roles = team.required_roles(DevWorkflow::ImplementFeature);
        assert!(roles.contains(&DevRole::Architect));
        assert!(roles.contains(&DevRole::Implementer));
        assert!(roles.contains(&DevRole::Tester));
        assert!(roles.contains(&DevRole::Reviewer));
        assert!(roles.contains(&DevRole::Documenter));
        // No duplicates
        assert_eq!(roles.len(), 5);
    }

    #[test]
    fn test_can_run_workflow_true() {
        let team = DevTeam::full_stack();
        assert!(team.can_run_workflow(DevWorkflow::ImplementFeature));
        assert!(team.can_run_workflow(DevWorkflow::FixBug));
        assert!(team.can_run_workflow(DevWorkflow::SecurityAudit));
    }

    #[test]
    fn test_can_run_workflow_false() {
        let team = DevTeam::minimal();
        // Minimal team (Implementer + Tester) cannot run ImplementFeature
        // because it requires Architect, Reviewer, and Documenter
        assert!(!team.can_run_workflow(DevWorkflow::ImplementFeature));
        assert!(!team.can_run_workflow(DevWorkflow::FixBug));
        assert!(!team.can_run_workflow(DevWorkflow::SecurityAudit));
    }

    #[test]
    fn test_model_for_role() {
        let team = DevTeam::full_stack();
        assert_eq!(team.model_for_role(DevRole::Architect), "powerful");
        assert_eq!(team.model_for_role(DevRole::Implementer), "balanced");
        assert_eq!(team.model_for_role(DevRole::Tester), "balanced");
        assert_eq!(team.model_for_role(DevRole::Reviewer), "powerful");
        assert_eq!(team.model_for_role(DevRole::Debugger), "powerful");
        assert_eq!(team.model_for_role(DevRole::DevOps), "fast");
        assert_eq!(team.model_for_role(DevRole::SecurityAuditor), "powerful");
        assert_eq!(team.model_for_role(DevRole::Documenter), "fast");
    }

    #[test]
    fn test_role_system_prompt_not_empty() {
        let team = DevTeam::full_stack();
        let all_roles = [
            DevRole::Architect,
            DevRole::Implementer,
            DevRole::Tester,
            DevRole::Reviewer,
            DevRole::Debugger,
            DevRole::DevOps,
            DevRole::SecurityAuditor,
            DevRole::Documenter,
        ];
        for role in &all_roles {
            let prompt = team.role_system_prompt(*role);
            assert!(
                !prompt.is_empty(),
                "System prompt for {role} should not be empty"
            );
            assert!(
                prompt.len() > 50,
                "System prompt for {role} should be substantive"
            );
        }
    }

    #[test]
    fn test_validate_gates_tests_pass() {
        let team = DevTeam::full_stack();
        let artifacts_with_tests = vec![WorkflowArtifact {
            name: "test_results".to_string(),
            artifact_type: ArtifactType::TestResults,
            content: "All 42 tests passed".to_string(),
        }];
        let artifacts_without = vec![WorkflowArtifact {
            name: "code".to_string(),
            artifact_type: ArtifactType::Code,
            content: "fn main() {}".to_string(),
        }];

        // Step 3 of ImplementFeature has TestsPass gate
        assert!(team.validate_gates(DevWorkflow::ImplementFeature, 3, &artifacts_with_tests));
        assert!(!team.validate_gates(DevWorkflow::ImplementFeature, 3, &artifacts_without));
    }

    #[test]
    fn test_validate_gates_review_score() {
        let team = DevTeam::full_stack();
        let artifacts_with_review = vec![WorkflowArtifact {
            name: "review".to_string(),
            artifact_type: ArtifactType::ReviewReport,
            content: "Score: 85/100. LGTM.".to_string(),
        }];
        let artifacts_without = vec![WorkflowArtifact {
            name: "code".to_string(),
            artifact_type: ArtifactType::Code,
            content: "fn main() {}".to_string(),
        }];

        // Step 4 of ImplementFeature has ReviewScore { min_score: 70 } gate
        assert!(team.validate_gates(DevWorkflow::ImplementFeature, 4, &artifacts_with_review));
        assert!(!team.validate_gates(DevWorkflow::ImplementFeature, 4, &artifacts_without));
    }

    #[test]
    fn test_describe_workflow() {
        let team = DevTeam::full_stack();
        let description = team.describe_workflow(DevWorkflow::ImplementFeature);
        assert!(description.contains("Implement Feature"));
        assert!(description.contains("Architect"));
        assert!(description.contains("Implementer"));
        assert!(description.contains("Tester"));
        assert!(description.contains("Reviewer"));
        assert!(description.contains("Documenter"));
        assert!(description.contains("Steps (5)"));
    }

    #[test]
    fn test_available_workflows() {
        let team = DevTeam::full_stack();
        let workflows = team.available_workflows();
        assert_eq!(workflows.len(), 8);
        assert!(workflows.contains(&DevWorkflow::ImplementFeature));
        assert!(workflows.contains(&DevWorkflow::FixBug));
        assert!(workflows.contains(&DevWorkflow::Refactor));
        assert!(workflows.contains(&DevWorkflow::AddTests));
        assert!(workflows.contains(&DevWorkflow::SecurityAudit));
        assert!(workflows.contains(&DevWorkflow::CodeReview));
        assert!(workflows.contains(&DevWorkflow::Optimize));
        assert!(workflows.contains(&DevWorkflow::WriteDocumentation));
    }

    #[test]
    fn test_team_summary() {
        let team = DevTeam::full_stack();
        let summary = team.summary();
        assert!(summary.contains("Full-Stack Team"));
        assert!(summary.contains("Roles (8)"));
        assert!(summary.contains("Architect"));
        assert!(summary.contains("Enforce gates: true"));
        assert!(summary.contains("Max parallel: 3"));
    }

    #[test]
    fn test_handoff_message() {
        let team = DevTeam::full_stack();
        let steps = team.workflow_steps(DevWorkflow::ImplementFeature);
        let artifacts = vec![WorkflowArtifact {
            name: "implementation_plan".to_string(),
            artifact_type: ArtifactType::Plan,
            content: "1. Create module\n2. Add types\n3. Implement logic".to_string(),
        }];

        let message = team.handoff_message(&steps[0], &artifacts);
        assert!(message.contains("Handoff from Step 1"));
        assert!(message.contains("Architect"));
        assert!(message.contains("implementation_plan"));
        assert!(message.contains("Create module"));
    }

    #[test]
    fn test_workflow_step_ordering() {
        let team = DevTeam::full_stack();
        let all_workflows = team.available_workflows();

        for workflow in &all_workflows {
            let steps = team.workflow_steps(*workflow);
            // Steps should be ordered 1..=N
            for (i, step) in steps.iter().enumerate() {
                assert_eq!(
                    step.order,
                    i + 1,
                    "Step {i} of {workflow} should have order {}",
                    i + 1
                );
            }
            // Steps should not be empty
            assert!(
                !steps.is_empty(),
                "Workflow {workflow} should have at least one step"
            );
        }
    }
}