agnosai 1.1.0

Provider-agnostic AI orchestration framework
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
//! Crew lifecycle: assemble → execute → aggregate.
//!
//! Replaces CrewAI's Crew class with a Rust-native implementation.
//!
//! Flow:
//! 1. Load agent definitions from crew spec
//! 2. Build task dependency graph
//! 3. Score agents for each task, pick best match
//! 4. Execute tasks respecting ProcessMode (Sequential / Parallel / DAG / Hierarchical)
//! 5. Track status transitions: Pending → Queued → Running → Completed/Failed
//! 6. Aggregate results into CrewState

use std::collections::{HashMap, HashSet};
use std::fmt::Write as _;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Instant;

use crate::core::agent::AgentDefinition;
use crate::core::crew::{CrewProfile, CrewSpec, CrewState, CrewStatus};
use crate::core::task::{ProcessMode, Task, TaskId, TaskResult, TaskStatus};
use crate::llm::{
    AuditChain, CostTracker, HooshClient, InferenceRequest, Message, ProviderType, ResponseCache,
    Role, cache_key,
};
use crate::server::sse::CrewEvent;
use tokio::sync::Semaphore;
use tokio::sync::broadcast;
use tracing::{debug, info, warn};

use crate::orchestrator::scoring;

/// Orchestrates the full crew lifecycle.
pub struct CrewRunner {
    spec: CrewSpec,
    event_tx: Option<broadcast::Sender<CrewEvent>>,
    /// LLM client for real inference. When `None`, falls back to placeholder.
    llm: Option<Arc<HooshClient>>,
    /// Shared response cache for inference results.
    cache: Arc<ResponseCache>,
    /// Cost tracker for inference cost accounting.
    cost_tracker: Arc<CostTracker>,
    /// Cancellation flag — when `true`, the runner stops scheduling new tasks.
    cancelled: Arc<AtomicBool>,
    /// Optional audit chain for tamper-proof event logging.
    audit: Option<Arc<AuditChain>>,
}

impl CrewRunner {
    /// Create a new crew runner for the given specification.
    pub fn new(spec: CrewSpec) -> Self {
        Self {
            spec,
            event_tx: None,
            llm: None,
            cache: Arc::new(ResponseCache::new(Default::default())),
            cost_tracker: Arc::new(CostTracker::new()),
            cancelled: Arc::new(AtomicBool::new(false)),
            audit: None,
        }
    }

    /// Attach a shared response cache.
    pub fn with_cache(mut self, cache: Arc<ResponseCache>) -> Self {
        self.cache = cache;
        self
    }

    /// Attach a shared cost tracker.
    pub fn with_cost_tracker(mut self, tracker: Arc<CostTracker>) -> Self {
        self.cost_tracker = tracker;
        self
    }

    /// Attach an LLM client for real inference.
    pub fn with_llm(mut self, client: Arc<HooshClient>) -> Self {
        self.llm = Some(client);
        self
    }

    /// Attach a cancellation token shared with the orchestrator.
    pub fn with_cancel_token(mut self, token: Arc<AtomicBool>) -> Self {
        self.cancelled = token;
        self
    }

    /// Attach an audit chain for crew/task event logging.
    pub fn with_audit(mut self, audit: Arc<AuditChain>) -> Self {
        self.audit = Some(audit);
        self
    }

    /// Attach an event sender for SSE streaming.
    pub fn with_events(mut self, tx: broadcast::Sender<CrewEvent>) -> Self {
        self.event_tx = Some(tx);
        self
    }

    /// Check whether cancellation has been requested.
    #[inline]
    fn is_cancelled(&self) -> bool {
        self.cancelled.load(Ordering::Acquire)
    }

    /// Record an audit event if an audit chain is attached.
    fn audit_record(&self, event: &str, level: &str, message: &str, metadata: serde_json::Value) {
        if let Some(ref audit) = self.audit {
            audit.record(event, level, message, None, None, Some(metadata));
        }
    }

    /// Emit a crew event if an event sender is configured.
    fn emit(&self, event_type: &str, data: serde_json::Value) {
        if let Some(ref tx) = self.event_tx {
            let _ = tx.send(CrewEvent {
                crew_id: self.spec.id.to_string(),
                event_type: event_type.to_string(),
                data,
            });
        }
    }

    /// Execute the crew according to its `ProcessMode`.
    ///
    /// Tasks are executed via LLM inference when a client is configured, or
    /// fall back to placeholder output (useful for tests). Orchestration
    /// handles dependency resolution, agent assignment, status tracking,
    /// result aggregation, and profiling.
    #[tracing::instrument(skip(self), fields(crew_id = %self.spec.id, process = ?self.spec.process))]
    pub async fn run(&mut self) -> crate::core::Result<CrewState> {
        let crew_start = Instant::now();
        info!(crew_id = %self.spec.id, name = %self.spec.name, "starting crew run");

        self.emit(
            "crew_started",
            serde_json::json!({
                "name": self.spec.name,
                "task_count": self.spec.tasks.len(),
            }),
        );

        let results = match self.spec.process {
            ProcessMode::Sequential => self.run_sequential().await?,
            ProcessMode::Parallel { max_concurrency } => self.run_parallel(max_concurrency).await?,
            ProcessMode::Dag => self.run_dag().await?,
            ProcessMode::Hierarchical { .. } => {
                // Phase 2: manager delegation. For now, fall back to sequential.
                warn!("hierarchical mode not yet implemented, falling back to sequential");
                self.run_sequential().await?
            }
        };

        let wall_ms = crew_start.elapsed().as_millis() as u64;
        let status = if self.is_cancelled() {
            CrewStatus::Cancelled
        } else if results.iter().all(|r| r.status == TaskStatus::Completed) {
            CrewStatus::Completed
        } else {
            CrewStatus::Failed
        };

        // Build profile from per-task latency and cost metadata.
        let task_ms: HashMap<TaskId, u64> = results
            .iter()
            .filter_map(|r| {
                r.metadata
                    .get("task_duration_ms")
                    .and_then(|v| v.as_u64())
                    .map(|ms| (r.task_id, ms))
            })
            .collect();
        // Per-task cost breakdown.
        let task_cost_usd: HashMap<TaskId, f64> = results
            .iter()
            .filter_map(|r| {
                r.metadata
                    .get("cost_usd")
                    .and_then(|v| v.as_f64())
                    .filter(|&c| c > 0.0)
                    .map(|c| (r.task_id, c))
            })
            .collect();
        let cost_usd: f64 = task_cost_usd.values().sum();

        // Per-agent cost breakdown.
        let mut agent_cost_usd: HashMap<String, f64> = HashMap::new();
        for r in &results {
            if let Some(cost) = r.metadata.get("cost_usd").and_then(|v| v.as_f64())
                && cost > 0.0
                && let Some(agent_key) = r.metadata.get("agent").and_then(|v| v.as_str())
            {
                *agent_cost_usd.entry(agent_key.to_string()).or_default() += cost;
            }
        }

        // Compute kavach sandbox strength score for the crew's isolation level.
        #[cfg(feature = "kavach")]
        let sandbox_strength = {
            let sandbox_policy = crate::sandbox::policy::SandboxPolicy::process();
            Some(crate::sandbox::kavach_bridge::strength_for_policy(&sandbox_policy).value())
        };
        #[cfg(not(feature = "kavach"))]
        let sandbox_strength: Option<u8> = None;

        let profile = CrewProfile {
            wall_ms,
            task_count: results.len(),
            task_ms,
            cost_usd,
            agent_cost_usd,
            task_cost_usd,
            sandbox_strength,
        };

        info!(
            crew_id = %self.spec.id,
            ?status,
            wall_ms,
            "crew run finished"
        );

        self.emit(
            "crew_completed",
            serde_json::json!({
                "status": format!("{status:?}"),
                "task_count": results.len(),
                "wall_ms": wall_ms,
            }),
        );

        Ok(CrewState {
            crew_id: self.spec.id,
            status,
            results,
            profile: Some(profile),
        })
    }

    // ── Sequential ──────────────────────────────────────────────────────

    #[tracing::instrument(skip(self), fields(crew_id = %self.spec.id, task_count = self.spec.tasks.len()))]
    async fn run_sequential(&mut self) -> crate::core::Result<Vec<TaskResult>> {
        let mut results = Vec::with_capacity(self.spec.tasks.len());

        for i in 0..self.spec.tasks.len() {
            if self.is_cancelled() {
                info!(crew_id = %self.spec.id, "crew cancelled — stopping sequential execution");
                break;
            }
            let agent = pick_best_agent(&self.spec.agents, &self.spec.tasks[i]);
            self.spec.tasks[i].status = TaskStatus::Queued;

            let agent_key = agent.as_ref().map(|a| a.agent_key.clone());

            if let Some(ref a) = agent {
                debug!(task_id = %self.spec.tasks[i].id, agent = %a.agent_key, "assigned");
            }

            self.emit(
                "task_started",
                serde_json::json!({
                    "task_id": self.spec.tasks[i].id.to_string(),
                    "description": self.spec.tasks[i].description,
                    "agent": agent_key,
                }),
            );

            self.spec.tasks[i].status = TaskStatus::Running;
            let result = execute_task(
                &self.spec.tasks[i],
                agent.as_ref(),
                self.llm.as_ref(),
                &self.cache,
                &self.cost_tracker,
                self.event_tx.as_ref(),
            )
            .await;
            self.spec.tasks[i].status = result.status;

            self.emit(
                "task_completed",
                serde_json::json!({
                    "task_id": result.task_id.to_string(),
                    "status": format!("{:?}", result.status),
                }),
            );

            let task_level = if result.status == TaskStatus::Completed {
                "info"
            } else {
                "error"
            };
            self.audit_record(
                "task_completed",
                task_level,
                &self.spec.tasks[i].description,
                serde_json::json!({
                    "crew_id": self.spec.id.to_string(),
                    "task_id": result.task_id.to_string(),
                    "status": format!("{:?}", result.status),
                    "agent": agent_key,
                }),
            );

            results.push(result);
        }

        Ok(results)
    }

    // ── Parallel ────────────────────────────────────────────────────────

    #[tracing::instrument(skip(self), fields(crew_id = %self.spec.id, max_concurrency))]
    async fn run_parallel(
        &mut self,
        max_concurrency: usize,
    ) -> crate::core::Result<Vec<TaskResult>> {
        let semaphore = std::sync::Arc::new(Semaphore::new(max_concurrency));

        // Mark all tasks as Queued up-front.
        for task in &mut self.spec.tasks {
            task.status = TaskStatus::Queued;
        }

        // Snapshot tasks and agent assignments before spawning.
        let task_snapshots: Vec<(Task, Option<AgentDefinition>)> = self
            .spec
            .tasks
            .iter()
            .map(|t| {
                let agent = pick_best_agent(&self.spec.agents, t);
                (t.clone(), agent)
            })
            .collect();

        // Emit task_started for all tasks.
        for (task, agent) in &task_snapshots {
            self.emit(
                "task_started",
                serde_json::json!({
                    "task_id": task.id.to_string(),
                    "description": task.description,
                    "agent": agent.as_ref().map(|a| &a.agent_key),
                }),
            );
        }

        let mut join_set = tokio::task::JoinSet::new();

        for (task, agent) in task_snapshots {
            let permit = semaphore.clone();
            let llm = self.llm.clone();
            let cache = Arc::clone(&self.cache);
            let cost_tracker = Arc::clone(&self.cost_tracker);
            let cancel = Arc::clone(&self.cancelled);
            join_set.spawn(async move {
                // Check cancellation before acquiring permit (avoids blocking for nothing).
                if cancel.load(Ordering::Acquire) {
                    return TaskResult {
                        task_id: task.id,
                        status: TaskStatus::Failed,
                        output: "crew cancelled".into(),
                        metadata: Default::default(),
                    };
                }
                let _permit = match permit.acquire().await {
                    Ok(p) => p,
                    Err(_) => {
                        return TaskResult {
                            task_id: task.id,
                            status: TaskStatus::Failed,
                            output: "internal error: concurrency semaphore closed".into(),
                            metadata: Default::default(),
                        };
                    }
                };
                // Check again after acquiring permit.
                if cancel.load(Ordering::Acquire) {
                    return TaskResult {
                        task_id: task.id,
                        status: TaskStatus::Failed,
                        output: "crew cancelled".into(),
                        metadata: Default::default(),
                    };
                }
                execute_task(
                    &task,
                    agent.as_ref(),
                    llm.as_ref(),
                    &cache,
                    &cost_tracker,
                    None,
                )
                .await
            });
        }

        let mut results = Vec::with_capacity(self.spec.tasks.len());
        while let Some(res) = join_set.join_next().await {
            match res {
                Ok(task_result) => {
                    self.emit(
                        "task_completed",
                        serde_json::json!({
                            "task_id": task_result.task_id.to_string(),
                            "status": format!("{:?}", task_result.status),
                        }),
                    );
                    results.push(task_result);
                }
                Err(e) => {
                    warn!(error = %e, "task join error (task panicked)");
                    // Synthesize a Failed result so the crew status correctly reflects the failure.
                    results.push(TaskResult {
                        task_id: uuid::Uuid::nil(),
                        output: format!("task panicked: {e}"),
                        status: TaskStatus::Failed,
                        metadata: Default::default(),
                    });
                }
            }
        }

        // Update spec task statuses from results.
        let status_map: HashMap<TaskId, TaskStatus> =
            results.iter().map(|r| (r.task_id, r.status)).collect();
        for task in &mut self.spec.tasks {
            if let Some(&s) = status_map.get(&task.id) {
                task.status = s;
            }
        }

        Ok(results)
    }

    // ── DAG ─────────────────────────────────────────────────────────────

    #[tracing::instrument(skip(self), fields(crew_id = %self.spec.id, task_count = self.spec.tasks.len()))]
    async fn run_dag(&mut self) -> crate::core::Result<Vec<TaskResult>> {
        let order = topological_sort(&self.spec.tasks)?;

        // Build dependency sets for quick lookup.
        let dep_sets: HashMap<TaskId, HashSet<TaskId>> = self
            .spec
            .tasks
            .iter()
            .map(|t| (t.id, t.dependencies.iter().copied().collect()))
            .collect();

        let task_map: HashMap<TaskId, usize> = self
            .spec
            .tasks
            .iter()
            .enumerate()
            .map(|(i, t)| (t.id, i))
            .collect();

        let mut completed: HashSet<TaskId> = HashSet::new();
        let mut results: Vec<TaskResult> = Vec::with_capacity(self.spec.tasks.len());

        // Walk topological layers: tasks whose deps are all completed are "ready".
        let mut remaining: Vec<TaskId> = order;

        while !remaining.is_empty() {
            if self.is_cancelled() {
                info!(crew_id = %self.spec.id, "crew cancelled — stopping DAG execution");
                break;
            }
            // Collect the ready front.
            let (ready, not_ready): (Vec<TaskId>, Vec<TaskId>) =
                remaining.into_iter().partition(|id| {
                    dep_sets
                        .get(id)
                        .is_none_or(|deps| deps.is_subset(&completed))
                });

            if ready.is_empty() {
                // Should not happen after successful topo sort, but guard anyway.
                return Err(crate::core::AgnosaiError::Scheduling(
                    "DAG deadlock: no ready tasks but remaining exist".into(),
                ));
            }

            // Run the ready wave concurrently.
            let mut join_set = tokio::task::JoinSet::new();

            for id in &ready {
                let idx = task_map[id];
                self.spec.tasks[idx].status = TaskStatus::Queued;
                let agent = pick_best_agent(&self.spec.agents, &self.spec.tasks[idx]);

                self.emit(
                    "task_started",
                    serde_json::json!({
                        "task_id": self.spec.tasks[idx].id.to_string(),
                        "description": self.spec.tasks[idx].description,
                        "agent": agent.as_ref().map(|a| &a.agent_key),
                    }),
                );

                let task_snap = self.spec.tasks[idx].clone();
                let llm = self.llm.clone();
                let cache = Arc::clone(&self.cache);
                let cost_tracker = Arc::clone(&self.cost_tracker);

                join_set.spawn(async move {
                    execute_task(
                        &task_snap,
                        agent.as_ref(),
                        llm.as_ref(),
                        &cache,
                        &cost_tracker,
                        None,
                    )
                    .await
                });
            }

            let mut wave_failed = false;
            while let Some(res) = join_set.join_next().await {
                match res {
                    Ok(tr) => {
                        if let Some(&idx) = task_map.get(&tr.task_id) {
                            self.spec.tasks[idx].status = tr.status;
                        }
                        self.emit(
                            "task_completed",
                            serde_json::json!({
                                "task_id": tr.task_id.to_string(),
                                "status": format!("{:?}", tr.status),
                            }),
                        );
                        if tr.status == TaskStatus::Failed {
                            wave_failed = true;
                            warn!(task_id = %tr.task_id, "DAG task failed — downstream tasks will be skipped");
                        } else {
                            // Only mark successful tasks as completed for dependency resolution.
                            completed.insert(tr.task_id);
                        }
                        results.push(tr);
                    }
                    Err(e) => {
                        warn!(error = %e, "DAG task join error");
                        wave_failed = true;
                    }
                }
            }

            remaining = not_ready;

            // If any task in this wave failed and there are remaining tasks
            // that depend on the failed one, stop early to prevent cascading.
            if wave_failed && !remaining.is_empty() {
                // Check if any remaining task can still run (all deps in completed set).
                let any_runnable = remaining.iter().any(|id| {
                    dep_sets
                        .get(id)
                        .is_none_or(|deps| deps.is_subset(&completed))
                });
                if !any_runnable {
                    warn!("DAG execution halted: no runnable tasks after failure");
                    break;
                }
            }
        }

        Ok(results)
    }
}

// ── Helpers ─────────────────────────────────────────────────────────────────

/// Pick the agent with the highest score for a task, or `None` if the roster
/// is empty.
fn pick_best_agent(agents: &[AgentDefinition], task: &Task) -> Option<AgentDefinition> {
    if agents.is_empty() {
        return None;
    }
    let mut ranked: Vec<(&AgentDefinition, f64)> = agents
        .iter()
        .map(|a| (a, scoring::score_agent(a, task)))
        .collect();
    ranked.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
    if let Some((agent, score)) = ranked.first() {
        debug!(
            agent_key = %agent.agent_key,
            score,
            task_id = %task.id,
            "picked best agent for task"
        );
    }
    ranked.first().map(|(a, _)| (*a).clone())
}

/// Infer the provider type from a model name for cost tracking.
fn infer_provider(model: &str) -> ProviderType {
    let m = model.to_lowercase();
    if m.starts_with("gpt-") || m.starts_with("o1") || m.starts_with("o3") {
        ProviderType::OpenAi
    } else if m.starts_with("claude") {
        ProviderType::Anthropic
    } else if m.starts_with("deepseek") {
        ProviderType::DeepSeek
    } else if m.starts_with("gemini") {
        ProviderType::Google
    } else if m.starts_with("grok") {
        ProviderType::Grok
    } else if m.starts_with("mistral") || m.starts_with("mixtral") {
        ProviderType::Mistral
    } else {
        // Local models (llama, etc.) — free via Ollama.
        ProviderType::Ollama
    }
}

/// Execute a task via LLM inference, or fall back to placeholder if no client.
#[tracing::instrument(skip(task, agent, llm, cache, cost_tracker, event_tx), fields(task_id = %task.id, agent = agent.map(|a| a.agent_key.as_str()).unwrap_or("none")))]
async fn execute_task(
    task: &Task,
    agent: Option<&AgentDefinition>,
    llm: Option<&Arc<HooshClient>>,
    cache: &Arc<ResponseCache>,
    cost_tracker: &Arc<CostTracker>,
    event_tx: Option<&broadcast::Sender<CrewEvent>>,
) -> TaskResult {
    let task_start = Instant::now();
    let agent_label = agent.map(|a| a.agent_key.as_str()).unwrap_or("unassigned");

    // If no LLM client, fall back to placeholder (useful for tests).
    let Some(client) = llm else {
        debug!(task_id = %task.id, agent = agent_label, "executing task (placeholder — no LLM client)");
        tokio::task::yield_now().await;
        let mut metadata = HashMap::new();
        metadata.insert(
            "task_duration_ms".into(),
            serde_json::json!(task_start.elapsed().as_millis() as u64),
        );
        return TaskResult {
            task_id: task.id,
            output: task.description.clone(),
            status: TaskStatus::Completed,
            metadata,
        };
    };

    debug!(task_id = %task.id, agent = agent_label, "executing task via LLM");

    // Build system prompt from agent definition, wrapped with anti-injection boundary.
    let raw_system = build_system_prompt(agent);
    let system_prompt = crate::server::prompt_guard::wrap_system_prompt(&raw_system);

    // Choose model: agent override → router-based tier selection.
    let model = select_model(agent);

    // Build messages: include any context as a preamble (sanitized).
    let mut messages = Vec::new();
    if !task.context.is_empty() {
        let ctx_json = serde_json::to_string_pretty(&task.context).unwrap_or_default();
        let sanitized_ctx = crate::server::prompt_guard::sanitize(&ctx_json, "context");
        messages.push(Message::new(Role::User, sanitized_ctx));
        messages.push(Message::new(
            Role::Assistant,
            "Understood, I have the context.",
        ));
    }

    // The task description is the main user message (sanitized).
    let mut user_msg = crate::server::prompt_guard::sanitize(&task.description, "task_description");
    if let Some(ref expected) = task.expected_output {
        let sanitized_expected = crate::server::prompt_guard::sanitize(expected, "expected_output");
        let _ = write!(user_msg, "\n\n{sanitized_expected}");
    }

    // Base temperature — may be adjusted by personality mood.
    let mut temperature = 0.7;

    if let Some(agent) = agent
        && let Some(ref profile) = agent.personality
    {
        temperature = mood_adjusted_temperature(profile, temperature);
    }

    let request = InferenceRequest {
        model: model.to_string(),
        prompt: user_msg,
        system: Some(system_prompt),
        messages,
        max_tokens: Some(4096),
        temperature: Some(temperature),
        ..Default::default()
    };

    // Check cache before calling the LLM.
    let ck = cache_key(&request.model, &request.messages);
    if let Some(cached) = cache.get(&ck) {
        let task_duration_ms = task_start.elapsed().as_millis() as u64;
        let mut metadata = HashMap::new();
        metadata.insert(
            "model".into(),
            serde_json::Value::String(request.model.clone()),
        );
        metadata.insert("cached".into(), serde_json::json!(true));
        metadata.insert(
            "task_duration_ms".into(),
            serde_json::json!(task_duration_ms),
        );

        debug!(
            task_id = %task.id,
            agent = agent_label,
            model = %request.model,
            "task completed from cache"
        );

        return TaskResult {
            task_id: task.id,
            output: (*cached).clone(),
            status: TaskStatus::Completed,
            metadata,
        };
    }

    // Primary path: non-streaming inference (gives full metadata).
    // When an event sender is present, also attempt streaming for token-by-token
    // SSE delivery, then fall back to the non-streaming response for metadata.
    //
    // If the task has an output_schema, validate the response and retry with
    // error feedback on failure (up to MAX_VALIDATION_RETRIES attempts).
    let mut current_request = request;
    let original_prompt = current_request.prompt.clone();
    let retry_config = crate::llm::retry::RetryConfig::default();
    let task_id_str = task.id.to_string();

    let mut final_response = {
        let req = &current_request;
        crate::llm::retry::with_retry(&retry_config, &task_id_str, || client.infer(req)).await
    };

    if let Some(ref schema) = task.output_schema {
        for attempt in 1..=crate::orchestrator::output_validation::MAX_VALIDATION_RETRIES {
            let Ok(ref resp) = final_response else { break };
            let (extracted, result) =
                crate::orchestrator::output_validation::extract_and_validate(&resp.text, schema);
            match result {
                crate::orchestrator::output_validation::ValidationResult::Valid => break,
                crate::orchestrator::output_validation::ValidationResult::Invalid(err) => {
                    crate::orchestrator::output_validation::log_retry(
                        &task.id.to_string(),
                        attempt,
                        &err,
                    );
                    // Build retry prompt from the ORIGINAL prompt (not accumulated)
                    // to prevent exponential growth. Sanitize the failed output to
                    // prevent prompt injection via error feedback.
                    let sanitized_output =
                        crate::server::prompt_guard::sanitize(&extracted, "failed_output");
                    current_request.prompt =
                        crate::orchestrator::output_validation::build_retry_prompt(
                            &original_prompt,
                            &sanitized_output,
                            &err,
                            schema,
                        );
                    current_request.temperature = Some(0.1);
                    final_response = {
                        let req = &current_request;
                        crate::llm::retry::with_retry(&retry_config, &task_id_str, || {
                            client.infer(req)
                        })
                        .await
                    };
                }
            }
        }
    }

    match final_response {
        Ok(response) => {
            // Cache the successful response.
            cache.insert(ck, response.text.clone());

            // If streaming was requested, emit the completed response as a
            // single token event. This avoids a redundant second inference call
            // that would double latency and cost.
            if let Some(tx) = event_tx {
                let _ = tx.send(CrewEvent {
                    crew_id: task
                        .context
                        .get("crew_id")
                        .and_then(|v| v.as_str())
                        .unwrap_or("unknown")
                        .to_string(),
                    event_type: "token".into(),
                    data: serde_json::json!({
                        "task_id": task.id.to_string(),
                        "token": response.text,
                        "complete": true,
                    }),
                });
            }

            let task_duration_ms = task_start.elapsed().as_millis() as u64;
            let provider = infer_provider(&response.model);

            // Record cost using actual token counts from the response.
            let cost_usd = cost_tracker.record(provider, "hoosh", &response.model, &response.usage);

            // Record Prometheus metrics.
            crate::llm::llm_metrics::record_request(
                &provider.to_string(),
                &response.model,
                "success",
                task_duration_ms as f64 / 1000.0,
                response.usage.prompt_tokens,
                response.usage.completion_tokens,
            );

            let mut metadata = HashMap::new();
            metadata.insert(
                "model".into(),
                serde_json::Value::String(response.model.clone()),
            );
            metadata.insert(
                "provider".into(),
                serde_json::Value::String(response.provider.clone()),
            );
            metadata.insert("latency_ms".into(), serde_json::json!(response.latency_ms));
            metadata.insert(
                "tokens".into(),
                serde_json::json!({
                    "prompt": response.usage.prompt_tokens,
                    "completion": response.usage.completion_tokens,
                    "total": response.usage.total_tokens,
                }),
            );
            metadata.insert("cost_usd".into(), serde_json::json!(cost_usd));
            metadata.insert(
                "task_duration_ms".into(),
                serde_json::json!(task_duration_ms),
            );

            info!(
                task_id = %task.id,
                agent = agent_label,
                model = %response.model,
                latency_ms = response.latency_ms,
                task_duration_ms,
                tokens = response.usage.total_tokens,
                cost_usd,
                "task completed via LLM"
            );

            TaskResult {
                task_id: task.id,
                output: response.text,
                status: TaskStatus::Completed,
                metadata,
            }
        }
        Err(e) => {
            let task_duration_ms = task_start.elapsed().as_millis() as u64;

            crate::llm::llm_metrics::record_request(
                "hoosh",
                &current_request.model,
                "error",
                task_duration_ms as f64 / 1000.0,
                0,
                0,
            );

            warn!(
                task_id = %task.id,
                agent = agent_label,
                task_duration_ms,
                error = %e,
                "LLM inference failed"
            );
            let mut metadata = HashMap::new();
            metadata.insert("error".into(), serde_json::Value::String(e.to_string()));
            metadata.insert(
                "task_duration_ms".into(),
                serde_json::json!(task_duration_ms),
            );

            let mut output = String::from("LLM error: ");
            let _ = write!(output, "{e}");
            TaskResult {
                task_id: task.id,
                output,
                status: TaskStatus::Failed,
                metadata,
            }
        }
    }
}

/// Build a system prompt from an agent's definition.
fn build_system_prompt(agent: Option<&AgentDefinition>) -> String {
    let Some(agent) = agent else {
        return "You are a helpful AI assistant executing tasks within a crew.".into();
    };

    let mut prompt = format!(
        "You are {name}, a {role}.\n\nGoal: {goal}",
        name = agent.name,
        role = agent.role,
        goal = agent.goal,
    );

    if let Some(ref backstory) = agent.backstory {
        let _ = write!(prompt, "\n\nBackstory: {backstory}");
    }

    if let Some(ref domain) = agent.domain {
        let _ = write!(prompt, "\n\nDomain expertise: {domain}");
    }

    if !agent.tools.is_empty() {
        let _ = write!(prompt, "\n\nAvailable tools: {}", agent.tools.join(", "));
    }

    if let Some(ref profile) = agent.personality {
        let disposition = profile.compose_prompt();
        if !disposition.is_empty() {
            prompt.push('\n');
            prompt.push_str(&disposition);
        }
    }

    prompt
}

/// Strip provider prefixes (e.g. `ollama/`, `openai/`) from a model string.
///
/// LiteLLM-style identifiers use `provider/model` notation but most inference
/// endpoints (including Ollama's OpenAI-compatible API) expect just the model
/// name.  This helper normalises both forms.
fn strip_provider_prefix(model: &str) -> &str {
    // Common litellm prefixes: ollama/, openai/, anthropic/, etc.
    if let Some(idx) = model.find('/') {
        let prefix = &model[..idx];
        // Only strip known provider prefixes, not arbitrary path components.
        match prefix {
            "ollama" | "openai" | "anthropic" | "groq" | "deepseek" | "mistral" | "together"
            | "fireworks" | "anyscale" | "perplexity" | "bedrock" | "azure" => &model[idx + 1..],
            _ => model,
        }
    } else {
        model
    }
}

/// Select a model for a task: agent override, or route by complexity tier.
fn select_model(agent: Option<&AgentDefinition>) -> &str {
    if let Some(agent) = agent {
        // Agent-specific model override takes priority.
        if let Some(ref model) = agent.llm_model {
            return strip_provider_prefix(model.as_str());
        }

        // Route by agent complexity.
        let complexity = crate::llm::parse_complexity(&agent.complexity);
        let profile = crate::llm::TaskProfile {
            task_type: crate::llm::TaskType::Reason,
            complexity,
        };
        let tier = crate::llm::router::route(&profile);
        return crate::llm::default_model(tier);
    }

    // No agent — use capable tier default.
    crate::llm::default_model(crate::llm::ModelTier::Capable)
}

/// Adjust inference temperature based on personality traits.
///
/// Maps personality dimensions to temperature modifiers:
/// - High creativity / curiosity → higher temperature (more diverse output)
/// - High precision / low risk tolerance → lower temperature (more focused output)
/// - High confidence → slightly lower (more deterministic)
///
/// Returns a temperature clamped to 0.1–1.5.
fn mood_adjusted_temperature(profile: &bhava::traits::PersonalityProfile, base: f64) -> f64 {
    use bhava::traits::TraitKind;

    let creativity = profile.get_trait(TraitKind::Creativity).normalized() as f64;
    let curiosity = profile.get_trait(TraitKind::Curiosity).normalized() as f64;
    let precision = profile.get_trait(TraitKind::Precision).normalized() as f64;
    let risk = profile.get_trait(TraitKind::RiskTolerance).normalized() as f64;
    let confidence = profile.get_trait(TraitKind::Confidence).normalized() as f64;

    // Creativity and curiosity push temperature up; precision and confidence pull down
    let delta = (creativity * 0.15) + (curiosity * 0.1) + (risk * 0.1)
        - (precision * 0.15)
        - (confidence * 0.05);

    (base + delta).clamp(0.1, 1.5)
}

/// Kahn's algorithm for topological sort. Delegates to the shared implementation
/// in [`crate::orchestrator::scheduler::topological_sort_tasks`].
fn topological_sort(tasks: &[Task]) -> crate::core::Result<Vec<TaskId>> {
    crate::orchestrator::scheduler::topological_sort_tasks(tasks)
}

// ── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::agent::AgentDefinition;
    use crate::core::crew::CrewSpec;
    use crate::core::task::{ProcessMode, Task};
    use uuid::Uuid;

    fn test_agent(key: &str) -> AgentDefinition {
        AgentDefinition {
            agent_key: key.into(),
            name: key.into(),
            role: "tester".into(),
            goal: "test things".into(),
            backstory: None,
            domain: None,
            tools: vec![],
            complexity: "medium".into(),
            llm_model: None,
            gpu_required: false,
            gpu_preferred: false,
            gpu_memory_min_mb: None,
            hardware: None,
            personality: None,
        }
    }

    fn test_task(desc: &str) -> Task {
        Task::new(desc)
    }

    fn test_spec(tasks: Vec<Task>, process: ProcessMode) -> CrewSpec {
        CrewSpec {
            id: Uuid::new_v4(),
            name: "test-crew".into(),
            agents: vec![test_agent("agent-a"), test_agent("agent-b")],
            tasks,
            process,
            metadata: Default::default(),
            trust_level: "basic".into(),
        }
    }

    // ── Sequential ──────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_sequential_execution() {
        let tasks = vec![
            test_task("step one"),
            test_task("step two"),
            test_task("step three"),
        ];
        let spec = test_spec(tasks, ProcessMode::Sequential);
        let mut runner = CrewRunner::new(spec);

        let state = runner.run().await.unwrap();

        assert_eq!(state.status, CrewStatus::Completed);
        assert_eq!(state.results.len(), 3);

        // Sequential preserves order.
        assert_eq!(state.results[0].output, "step one");
        assert_eq!(state.results[1].output, "step two");
        assert_eq!(state.results[2].output, "step three");

        // All tasks should be Completed.
        for r in &state.results {
            assert_eq!(r.status, TaskStatus::Completed);
        }
    }

    #[tokio::test]
    async fn test_sequential_empty() {
        let spec = test_spec(vec![], ProcessMode::Sequential);
        let mut runner = CrewRunner::new(spec);
        let state = runner.run().await.unwrap();
        assert_eq!(state.status, CrewStatus::Completed);
        assert!(state.results.is_empty());
    }

    // ── Parallel ────────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_parallel_execution() {
        let tasks = vec![
            test_task("par one"),
            test_task("par two"),
            test_task("par three"),
            test_task("par four"),
        ];
        let spec = test_spec(tasks, ProcessMode::Parallel { max_concurrency: 2 });
        let mut runner = CrewRunner::new(spec);

        let state = runner.run().await.unwrap();

        assert_eq!(state.status, CrewStatus::Completed);
        assert_eq!(state.results.len(), 4);

        // All completed (order may vary in parallel).
        let outputs: HashSet<String> = state.results.iter().map(|r| r.output.clone()).collect();
        assert!(outputs.contains("par one"));
        assert!(outputs.contains("par two"));
        assert!(outputs.contains("par three"));
        assert!(outputs.contains("par four"));
    }

    #[tokio::test]
    async fn test_parallel_single_concurrency() {
        let tasks = vec![test_task("a"), test_task("b")];
        let spec = test_spec(tasks, ProcessMode::Parallel { max_concurrency: 1 });
        let mut runner = CrewRunner::new(spec);
        let state = runner.run().await.unwrap();
        assert_eq!(state.status, CrewStatus::Completed);
        assert_eq!(state.results.len(), 2);
    }

    // ── DAG ─────────────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_dag_execution_with_dependencies() {
        // Graph: A → B → C  (C depends on B, B depends on A)
        let task_a = test_task("task A");
        let mut task_b = test_task("task B");
        let mut task_c = test_task("task C");

        task_b.dependencies.push(task_a.id);
        task_c.dependencies.push(task_b.id);

        let spec = test_spec(vec![task_a, task_b, task_c], ProcessMode::Dag);
        let mut runner = CrewRunner::new(spec);

        let state = runner.run().await.unwrap();

        assert_eq!(state.status, CrewStatus::Completed);
        assert_eq!(state.results.len(), 3);

        // Verify dependency order: A must come before B, B before C.
        let pos = |desc: &str| state.results.iter().position(|r| r.output == desc).unwrap();
        assert!(pos("task A") < pos("task B"));
        assert!(pos("task B") < pos("task C"));
    }

    #[tokio::test]
    async fn test_dag_diamond() {
        // Diamond: A → B, A → C, B → D, C → D
        let a = test_task("A");
        let mut b = test_task("B");
        let mut c = test_task("C");
        let mut d = test_task("D");

        b.dependencies.push(a.id);
        c.dependencies.push(a.id);
        d.dependencies.push(b.id);
        d.dependencies.push(c.id);

        let spec = test_spec(vec![a, b, c, d], ProcessMode::Dag);
        let mut runner = CrewRunner::new(spec);
        let state = runner.run().await.unwrap();

        assert_eq!(state.status, CrewStatus::Completed);
        assert_eq!(state.results.len(), 4);

        let pos = |desc: &str| state.results.iter().position(|r| r.output == desc).unwrap();
        assert!(pos("A") < pos("B"));
        assert!(pos("A") < pos("C"));
        assert!(pos("B") < pos("D"));
        assert!(pos("C") < pos("D"));
    }

    #[tokio::test]
    async fn test_dag_no_deps_runs_all() {
        // All independent tasks — should all run in the first wave.
        let tasks = vec![test_task("x"), test_task("y"), test_task("z")];
        let spec = test_spec(tasks, ProcessMode::Dag);
        let mut runner = CrewRunner::new(spec);
        let state = runner.run().await.unwrap();
        assert_eq!(state.status, CrewStatus::Completed);
        assert_eq!(state.results.len(), 3);
    }

    // ── Topological sort ────────────────────────────────────────────────

    #[test]
    fn test_topo_sort_detects_cycle() {
        let mut a = test_task("a");
        let mut b = test_task("b");
        a.dependencies.push(b.id);
        b.dependencies.push(a.id);

        let err = topological_sort(&[a, b]);
        assert!(err.is_err());
    }

    // ── Agent selection ─────────────────────────────────────────────────

    #[test]
    fn test_pick_best_agent_empty_roster() {
        let task = test_task("something");
        assert!(pick_best_agent(&[], &task).is_none());
    }

    #[test]
    fn test_pick_best_agent_returns_some() {
        let task = test_task("something");
        let agents = vec![test_agent("a1")];
        let picked = pick_best_agent(&agents, &task);
        assert!(picked.is_some());
        assert_eq!(picked.unwrap().agent_key, "a1");
    }

    // ── Hierarchical fallback ───────────────────────────────────────────

    #[tokio::test]
    async fn test_hierarchical_falls_back_to_sequential() {
        let tasks = vec![test_task("h1"), test_task("h2")];
        let spec = test_spec(
            tasks,
            ProcessMode::Hierarchical {
                manager: Uuid::new_v4(),
            },
        );
        let mut runner = CrewRunner::new(spec);
        let state = runner.run().await.unwrap();
        assert_eq!(state.status, CrewStatus::Completed);
        assert_eq!(state.results.len(), 2);
        // Sequential order preserved in fallback.
        assert_eq!(state.results[0].output, "h1");
        assert_eq!(state.results[1].output, "h2");
    }

    // ── System prompt building ──────────────────────────────────────────

    #[test]
    fn test_build_system_prompt_no_agent() {
        let prompt = build_system_prompt(None);
        assert!(prompt.contains("helpful AI assistant"));
    }

    #[test]
    fn test_build_system_prompt_full_agent() {
        let mut agent = test_agent("qa");
        agent.name = "QA Lead".into();
        agent.role = "quality assurance".into();
        agent.goal = "ensure zero defects".into();
        agent.backstory = Some("10 years in QA".into());
        agent.domain = Some("testing".into());
        agent.tools = vec!["selenium".into(), "pytest".into()];

        let prompt = build_system_prompt(Some(&agent));
        assert!(prompt.contains("QA Lead"));
        assert!(prompt.contains("quality assurance"));
        assert!(prompt.contains("ensure zero defects"));
        assert!(prompt.contains("10 years in QA"));
        assert!(prompt.contains("testing"));
        assert!(prompt.contains("selenium, pytest"));
    }

    #[test]
    fn test_build_system_prompt_minimal_agent() {
        let agent = test_agent("min");
        let prompt = build_system_prompt(Some(&agent));
        assert!(prompt.contains("min")); // name
        assert!(prompt.contains("tester")); // role
        assert!(prompt.contains("test things")); // goal
        assert!(!prompt.contains("Backstory"));
        assert!(!prompt.contains("Domain"));
        assert!(!prompt.contains("Available tools"));
    }

    // ── Model selection ─────────────────────────────────────────────────

    #[test]
    fn test_select_model_no_agent() {
        let model = select_model(None);
        // Should use capable tier default.
        assert_eq!(model, "llama3:70b");
    }

    #[test]
    fn test_select_model_agent_override() {
        let mut agent = test_agent("a");
        agent.llm_model = Some("gpt-4o".into());
        assert_eq!(select_model(Some(&agent)), "gpt-4o");
    }

    #[test]
    fn test_select_model_strips_provider_prefix() {
        let mut agent = test_agent("a");
        agent.llm_model = Some("ollama/llama3.2:1b".into());
        assert_eq!(select_model(Some(&agent)), "llama3.2:1b");

        agent.llm_model = Some("openai/gpt-4o".into());
        assert_eq!(select_model(Some(&agent)), "gpt-4o");

        agent.llm_model = Some("anthropic/claude-sonnet-4-20250514".into());
        assert_eq!(select_model(Some(&agent)), "claude-sonnet-4-20250514");
    }

    #[test]
    fn test_strip_provider_prefix_preserves_unknown() {
        // Unknown prefix should be kept as-is.
        assert_eq!(strip_provider_prefix("custom/model"), "custom/model");
        assert_eq!(strip_provider_prefix("llama3:70b"), "llama3:70b");
    }

    #[test]
    fn test_select_model_routes_by_complexity() {
        let mut low = test_agent("low");
        low.complexity = "low".into();
        // Low complexity + Reason → Capable → llama3:70b
        assert_eq!(select_model(Some(&low)), "llama3:70b");

        let mut high = test_agent("high");
        high.complexity = "high".into();
        // High complexity + Reason → Premium → llama3:405b
        assert_eq!(select_model(Some(&high)), "llama3:405b");
    }

    // ── Placeholder fallback (no LLM client) ────────────────────────────

    #[tokio::test]
    async fn test_execute_task_placeholder_when_no_llm() {
        let task = test_task("do something");
        let agent = test_agent("a");
        let cache = Arc::new(ResponseCache::new(Default::default()));
        let cost_tracker = Arc::new(CostTracker::new());
        let result = execute_task(&task, Some(&agent), None, &cache, &cost_tracker, None).await;
        assert_eq!(result.status, TaskStatus::Completed);
        assert_eq!(result.output, "do something");
    }

    // ── SSE event emission ──────────────────────────────────────────────

    #[tokio::test]
    async fn test_events_emitted_during_sequential_run() {
        let tasks = vec![test_task("step one"), test_task("step two")];
        let spec = test_spec(tasks, ProcessMode::Sequential);
        let (tx, mut rx) = broadcast::channel::<CrewEvent>(64);
        let mut runner = CrewRunner::new(spec).with_events(tx);

        let state = runner.run().await.unwrap();
        assert_eq!(state.status, CrewStatus::Completed);

        // Collect all events.
        let mut events = Vec::new();
        while let Ok(ev) = rx.try_recv() {
            events.push(ev);
        }

        // Should have: crew_started, task_started x2, task_completed x2, crew_completed
        let types: Vec<&str> = events.iter().map(|e| e.event_type.as_str()).collect();
        assert!(types.contains(&"crew_started"));
        assert!(types.contains(&"crew_completed"));
        assert_eq!(types.iter().filter(|&&t| t == "task_started").count(), 2);
        assert_eq!(types.iter().filter(|&&t| t == "task_completed").count(), 2);
    }

    // ── Provider inference ──────────────────────────────────────────────

    #[test]
    fn test_infer_provider_openai() {
        assert_eq!(infer_provider("gpt-4o"), ProviderType::OpenAi);
        assert_eq!(infer_provider("gpt-4o-mini"), ProviderType::OpenAi);
        assert_eq!(infer_provider("o1"), ProviderType::OpenAi);
        assert_eq!(infer_provider("o3-mini"), ProviderType::OpenAi);
    }

    #[test]
    fn test_infer_provider_anthropic() {
        assert_eq!(infer_provider("claude-sonnet-4"), ProviderType::Anthropic);
        assert_eq!(
            infer_provider("claude-sonnet-4-20250514"),
            ProviderType::Anthropic
        );
        assert_eq!(infer_provider("claude-opus-4"), ProviderType::Anthropic);
    }

    #[test]
    fn test_infer_provider_deepseek() {
        assert_eq!(infer_provider("deepseek-chat"), ProviderType::DeepSeek);
        assert_eq!(infer_provider("deepseek-coder"), ProviderType::DeepSeek);
    }

    #[test]
    fn test_infer_provider_local_models() {
        assert_eq!(infer_provider("llama3"), ProviderType::Ollama);
        assert_eq!(infer_provider("llama3:70b"), ProviderType::Ollama);
        assert_eq!(infer_provider("phi3"), ProviderType::Ollama);
        assert_eq!(infer_provider("unknown-model"), ProviderType::Ollama);
    }

    #[test]
    fn test_infer_provider_case_insensitive() {
        assert_eq!(infer_provider("GPT-4o"), ProviderType::OpenAi);
        assert_eq!(infer_provider("Claude-Opus-4"), ProviderType::Anthropic);
        assert_eq!(infer_provider("DEEPSEEK-CHAT"), ProviderType::DeepSeek);
    }

    // ── Cost tracking in placeholder mode ───────────────────────────────

    #[tokio::test]
    async fn test_execute_task_placeholder_has_duration() {
        let task = test_task("check duration");
        let agent = test_agent("a");
        let cache = Arc::new(ResponseCache::new(Default::default()));
        let cost_tracker = Arc::new(CostTracker::new());
        let result = execute_task(&task, Some(&agent), None, &cache, &cost_tracker, None).await;
        assert!(result.metadata.contains_key("task_duration_ms"));
        // Placeholder mode should not record any cost.
        assert!(cost_tracker.total_cost() == 0.0);
    }

    // ── CrewProfile cost aggregation ────────────────────────────────────

    #[tokio::test]
    async fn test_crew_profile_includes_cost() {
        let tasks = vec![test_task("task a")];
        let spec = test_spec(tasks, ProcessMode::Sequential);
        let mut runner = CrewRunner::new(spec);
        let state = runner.run().await.unwrap();
        // Placeholder mode: cost should be 0.
        let profile = state.profile.unwrap();
        assert_eq!(profile.cost_usd, 0.0);
        assert_eq!(profile.task_count, 1);
        assert!(profile.wall_ms < 1000); // should be sub-millisecond
    }

    // ── Parallel execution tracks per-task durations ────────────────────

    #[tokio::test]
    async fn test_parallel_tasks_have_duration_metadata() {
        let tasks = vec![test_task("par a"), test_task("par b"), test_task("par c")];
        let spec = test_spec(tasks, ProcessMode::Parallel { max_concurrency: 3 });
        let mut runner = CrewRunner::new(spec);
        let state = runner.run().await.unwrap();
        assert_eq!(state.status, CrewStatus::Completed);
        let profile = state.profile.unwrap();
        assert_eq!(profile.task_count, 3);
        // All tasks should have duration metadata.
        assert_eq!(profile.task_ms.len(), 3);
    }

    // ── DAG execution tracks per-task durations ─────────────────────────

    #[tokio::test]
    async fn test_dag_tasks_have_duration_metadata() {
        let t1 = test_task("dag root");
        let mut t2 = test_task("dag child");
        t2.dependencies.push(t1.id);
        let tasks = vec![t1, t2];
        let spec = test_spec(tasks, ProcessMode::Dag);
        let mut runner = CrewRunner::new(spec);
        let state = runner.run().await.unwrap();
        assert_eq!(state.status, CrewStatus::Completed);
        let profile = state.profile.unwrap();
        assert_eq!(profile.task_count, 2);
        assert_eq!(profile.task_ms.len(), 2);
    }
}