1use std::future::Future;
37use std::pin::Pin;
38use std::sync::Arc;
39
40use std::collections::HashMap;
41
42use swarm_engine_core::actions::ActionDef;
43use swarm_engine_core::agent::{BatchDecisionRequest, DecisionResponse, WorkerDecisionRequest};
44use swarm_engine_core::exploration::DependencyGraph;
45use swarm_engine_core::types::{LoraConfig, WorkerId};
46
47use crate::decider::{LlmDecider, LlmError};
48
49pub type BatchProcessResult = Vec<(WorkerId, Result<DecisionResponse, BatchProcessError>)>;
55
56#[derive(Debug, Clone, thiserror::Error)]
58pub enum BatchProcessError {
59 #[error("Batch process error (transient): {0}")]
61 Transient(String),
62
63 #[error("Batch process error: {0}")]
65 Permanent(String),
66}
67
68impl BatchProcessError {
69 pub fn transient(message: impl Into<String>) -> Self {
70 Self::Transient(message.into())
71 }
72
73 pub fn permanent(message: impl Into<String>) -> Self {
74 Self::Permanent(message.into())
75 }
76
77 pub fn is_transient(&self) -> bool {
78 matches!(self, Self::Transient(_))
79 }
80
81 pub fn message(&self) -> &str {
82 match self {
83 Self::Transient(msg) => msg,
84 Self::Permanent(msg) => msg,
85 }
86 }
87}
88
89impl From<LlmError> for BatchProcessError {
90 fn from(e: LlmError) -> Self {
91 if e.is_transient() {
92 Self::Transient(e.message().to_string())
93 } else {
94 Self::Permanent(e.message().to_string())
95 }
96 }
97}
98
99impl From<swarm_engine_core::error::SwarmError> for BatchProcessError {
100 fn from(err: swarm_engine_core::error::SwarmError) -> Self {
101 if err.is_transient() {
102 Self::Transient(err.message())
103 } else {
104 Self::Permanent(err.message())
105 }
106 }
107}
108
109impl From<BatchProcessError> for swarm_engine_core::error::SwarmError {
110 fn from(err: BatchProcessError) -> Self {
111 match err {
112 BatchProcessError::Transient(message) => {
113 swarm_engine_core::error::SwarmError::LlmTransient { message }
114 }
115 BatchProcessError::Permanent(message) => {
116 swarm_engine_core::error::SwarmError::LlmPermanent { message }
117 }
118 }
119 }
120}
121
122pub trait BatchProcessor: Send + Sync {
127 fn process(
135 &self,
136 request: BatchDecisionRequest,
137 ) -> Pin<Box<dyn Future<Output = BatchProcessResult> + Send + '_>>;
138
139 fn plan_dependencies(
147 &self,
148 _task: &str,
149 _actions: &[ActionDef],
150 ) -> Pin<Box<dyn Future<Output = Option<DependencyGraph>> + Send + '_>> {
151 Box::pin(async { None })
152 }
153
154 fn is_healthy(&self) -> Pin<Box<dyn Future<Output = bool> + Send + '_>>;
156
157 fn name(&self) -> &str;
159}
160
161#[derive(Debug, Clone)]
167pub struct LlmBatchProcessorConfig {
168 pub parallel: bool,
170 pub max_concurrency: usize,
172 pub max_retries: Option<usize>,
174}
175
176impl Default for LlmBatchProcessorConfig {
177 fn default() -> Self {
178 Self {
179 parallel: true,
180 max_concurrency: 4,
181 max_retries: Some(5),
182 }
183 }
184}
185
186pub struct LlmBatchProcessor<D: LlmDecider> {
191 decider: Arc<D>,
192 config: LlmBatchProcessorConfig,
193}
194
195impl<D: LlmDecider> LlmBatchProcessor<D> {
196 pub fn new(decider: D) -> Self {
198 Self {
199 decider: Arc::new(decider),
200 config: LlmBatchProcessorConfig::default(),
201 }
202 }
203
204 pub fn from_arc(decider: Arc<D>) -> Self {
206 Self {
207 decider,
208 config: LlmBatchProcessorConfig::default(),
209 }
210 }
211
212 pub fn with_config(mut self, config: LlmBatchProcessorConfig) -> Self {
214 self.config = config;
215 self
216 }
217}
218
219impl<D: LlmDecider + 'static> BatchProcessor for LlmBatchProcessor<D> {
220 fn process(
221 &self,
222 request: BatchDecisionRequest,
223 ) -> Pin<Box<dyn Future<Output = BatchProcessResult> + Send + '_>> {
224 Box::pin(async move {
225 if request.requests.is_empty() {
226 return vec![];
227 }
228
229 let requests: Vec<(WorkerId, WorkerDecisionRequest)> = request
231 .requests
232 .into_iter()
233 .map(|r| (r.worker_id, r))
234 .collect();
235
236 if self.config.parallel {
237 self.process_parallel(requests).await
238 } else {
239 self.process_sequential(requests).await
240 }
241 })
242 }
243
244 fn plan_dependencies(
245 &self,
246 task: &str,
247 actions: &[ActionDef],
248 ) -> Pin<Box<dyn Future<Output = Option<DependencyGraph>> + Send + '_>> {
249 let task = task.to_string();
250 let actions: Vec<ActionDef> = actions.to_vec();
251 let decider = Arc::clone(&self.decider);
252
253 Box::pin(async move {
254 use std::time::Instant;
255 use swarm_engine_core::actions::ActionCategory;
256 use swarm_engine_core::exploration::DependencyGraphBuilder;
257
258 let start_time = Instant::now();
259 let action_names: Vec<String> = actions.iter().map(|a| a.name.clone()).collect();
260
261 let discover: Vec<&ActionDef> = actions
263 .iter()
264 .filter(|a| a.category == ActionCategory::NodeExpand)
265 .collect();
266 let not_discover: Vec<&ActionDef> = actions
267 .iter()
268 .filter(|a| a.category == ActionCategory::NodeStateChange)
269 .collect();
270
271 tracing::debug!(
272 discover = ?discover.iter().map(|a| &a.name).collect::<Vec<_>>(),
273 not_discover = ?not_discover.iter().map(|a| &a.name).collect::<Vec<_>>(),
274 "Separated actions by category"
275 );
276
277 let discover_sort_start = Instant::now();
279 let sorted_discover = if discover.len() <= 1 {
280 discover.iter().map(|a| a.name.clone()).collect()
281 } else {
282 binary_sort_actions(&task, &discover, decider.as_ref()).await
283 };
284 let discover_sort_ms = discover_sort_start.elapsed().as_millis();
285
286 tracing::debug!(
287 sorted = ?sorted_discover,
288 elapsed_ms = discover_sort_ms,
289 "Sorted Discover actions via binary comparison"
290 );
291
292 let not_discover_sort_start = Instant::now();
294 let sorted_not_discover = if not_discover.len() <= 1 {
295 not_discover.iter().map(|a| a.name.clone()).collect()
296 } else {
297 binary_sort_actions(&task, ¬_discover, decider.as_ref()).await
298 };
299 let not_discover_sort_ms = not_discover_sort_start.elapsed().as_millis();
300
301 tracing::debug!(
302 sorted = ?sorted_not_discover,
303 elapsed_ms = not_discover_sort_ms,
304 "Sorted NotDiscover actions via binary comparison"
305 );
306
307 let mut builder = DependencyGraphBuilder::new()
309 .task(&task)
310 .available_actions(action_names.clone());
311
312 if !sorted_discover.is_empty() {
314 builder = builder.start_node(&sorted_discover[0]);
315 } else if !sorted_not_discover.is_empty() {
316 builder = builder.start_node(&sorted_not_discover[0]);
318 }
319
320 if let Some(last) = sorted_not_discover.last() {
322 builder = builder.terminal_node(last);
323 } else if !sorted_discover.is_empty() {
324 builder = builder.terminal_node(sorted_discover.last().unwrap());
326 }
327
328 for window in sorted_discover.windows(2) {
330 builder = builder.edge(&window[0], &window[1], 0.9);
331 }
332
333 if !sorted_discover.is_empty() && !sorted_not_discover.is_empty() {
335 builder = builder.edge(
336 sorted_discover.last().unwrap(),
337 &sorted_not_discover[0],
338 0.9,
339 );
340 }
341
342 for window in sorted_not_discover.windows(2) {
344 builder = builder.edge(&window[0], &window[1], 0.9);
345 }
346
347 let mut graph = builder.build();
348 let total_ms = start_time.elapsed().as_millis();
349
350 graph.set_action_order(sorted_discover.clone(), sorted_not_discover.clone());
352
353 {
355 use swarm_engine_core::learn::DependencyGraphRecord;
356
357 let prompt = format!(
359 "Task: {}\n\nAvailable Actions:\n{}",
360 task,
361 action_names
362 .iter()
363 .map(|n| format!("- {}", n))
364 .collect::<Vec<_>>()
365 .join("\n")
366 );
367
368 let response = format!(
370 "discover_order: {:?}\nnot_discover_order: {:?}",
371 sorted_discover, sorted_not_discover
372 );
373
374 let record = DependencyGraphRecord::new(decider.model_name())
375 .prompt(prompt)
376 .response(response)
377 .available_actions(action_names)
378 .discover_order(sorted_discover.clone())
379 .not_discover_order(sorted_not_discover.clone())
380 .endpoint(decider.endpoint())
381 .latency_ms(total_ms as u64);
382
383 graph.set_learn_record(record);
384 }
385
386 tracing::info!(
387 discover_order = ?sorted_discover,
388 not_discover_order = ?sorted_not_discover,
389 edges = graph.edges().len(),
390 discover_sort_ms = discover_sort_ms,
391 not_discover_sort_ms = not_discover_sort_ms,
392 total_ms = total_ms,
393 "DependencyGraph generated via LLM binary sort"
394 );
395
396 Some(graph)
397 })
398 }
399
400 fn is_healthy(&self) -> Pin<Box<dyn Future<Output = bool> + Send + '_>> {
401 let decider = Arc::clone(&self.decider);
402 Box::pin(async move { decider.is_healthy().await })
403 }
404
405 fn name(&self) -> &str {
406 self.decider.model_name()
407 }
408}
409
410impl<D: LlmDecider + 'static> LlmBatchProcessor<D> {
411 async fn process_parallel(
428 &self,
429 requests: Vec<(WorkerId, WorkerDecisionRequest)>,
430 ) -> BatchProcessResult {
431 let grouped = group_by_lora(requests);
433
434 let group_count = grouped.len();
435 if group_count > 1 {
436 tracing::debug!(
437 groups = group_count,
438 "Processing requests in {} LoRA groups",
439 group_count
440 );
441 }
442
443 let mut all_results = Vec::new();
445 for (lora_config, group_requests) in grouped {
446 if group_count > 1 {
447 tracing::trace!(
448 lora = ?lora_config,
449 count = group_requests.len(),
450 "Processing LoRA group"
451 );
452 }
453 let results = self.process_group(group_requests).await;
454 all_results.extend(results);
455 }
456
457 all_results
458 }
459
460 async fn process_group(
462 &self,
463 requests: Vec<(WorkerId, WorkerDecisionRequest)>,
464 ) -> BatchProcessResult {
465 use futures::future::join_all;
466 use tokio::sync::Semaphore;
467
468 let max_concurrency = self
470 .decider
471 .max_concurrency()
472 .await
473 .unwrap_or(self.config.max_concurrency);
474
475 let semaphore = Arc::new(Semaphore::new(max_concurrency));
476
477 let futures: Vec<_> = requests
478 .into_iter()
479 .map(|(worker_id, req)| {
480 let decider = Arc::clone(&self.decider);
481 let sem = Arc::clone(&semaphore);
482 async move {
483 let _permit = sem.acquire().await.expect("Semaphore closed");
485 let result = decider.decide(req).await;
486 (worker_id, result)
487 }
488 })
489 .collect();
490
491 let results = join_all(futures).await;
492
493 results
494 .into_iter()
495 .map(|(worker_id, result)| {
496 let mapped = result.map_err(BatchProcessError::from);
497 (worker_id, mapped)
498 })
499 .collect()
500 }
501
502 async fn process_sequential(
504 &self,
505 requests: Vec<(WorkerId, WorkerDecisionRequest)>,
506 ) -> BatchProcessResult {
507 let mut results = Vec::with_capacity(requests.len());
508
509 for (worker_id, req) in requests {
510 let result = self.decider.decide(req).await;
511 let mapped = result.map_err(BatchProcessError::from);
512 results.push((worker_id, mapped));
513 }
514
515 results
516 }
517}
518
519fn group_by_lora(
524 requests: Vec<(WorkerId, WorkerDecisionRequest)>,
525) -> HashMap<Option<LoraConfig>, Vec<(WorkerId, WorkerDecisionRequest)>> {
526 let mut groups: HashMap<Option<LoraConfig>, Vec<(WorkerId, WorkerDecisionRequest)>> =
527 HashMap::new();
528
529 for (worker_id, req) in requests {
530 let lora_key = req.lora.clone();
531 groups.entry(lora_key).or_default().push((worker_id, req));
532 }
533
534 groups
535}
536
537async fn binary_sort_actions<D: LlmDecider>(
546 task: &str,
547 actions: &[&ActionDef],
548 decider: &D,
549) -> Vec<String> {
550 use futures::future::join_all;
551 use std::collections::HashMap;
552
553 if actions.len() <= 1 {
554 return actions.iter().map(|a| a.name.clone()).collect();
555 }
556
557 let mut requests: Vec<(usize, usize, String, String, String)> = Vec::new();
560 let mut pair_index = 0;
561
562 for i in 0..actions.len() {
563 for j in (i + 1)..actions.len() {
564 let a = actions[i];
565 let b = actions[j];
566 let prompt = format!(
567 "Goal: {}\n- {}: {}\n- {}: {}\nWhich comes first: {} or {}?\nAnswer (one word):",
568 task, a.name, a.description, b.name, b.description, a.name, b.name
569 );
570
571 for vote_idx in 0..3 {
573 requests.push((
574 pair_index,
575 vote_idx,
576 prompt.clone(),
577 a.name.clone(),
578 b.name.clone(),
579 ));
580 }
581 pair_index += 1;
582 }
583 }
584
585 let total_requests = requests.len();
586 tracing::debug!(
587 pairs = pair_index,
588 total_requests = total_requests,
589 "Binary sort: sending batch requests"
590 );
591
592 let futures: Vec<_> = requests
595 .into_iter()
596 .map(|(pair_idx, vote_idx, prompt, a_name, b_name)| {
597 let decider_ref = decider;
598 async move {
599 let result = decider_ref.call_raw(&prompt, None).await;
600 (pair_idx, vote_idx, result, a_name, b_name)
601 }
602 })
603 .collect();
604
605 let results = join_all(futures).await;
606
607 let mut pair_votes: HashMap<usize, (usize, usize, String, String)> = HashMap::new();
610
611 for (pair_idx, _vote_idx, result, a_name, b_name) in results {
612 let entry = pair_votes
613 .entry(pair_idx)
614 .or_insert((0, 0, a_name.clone(), b_name.clone()));
615
616 if let Ok(response) = result {
617 let response_upper = response.to_uppercase();
618 let a_upper = a_name.to_uppercase();
619 let b_upper = b_name.to_uppercase();
620
621 if response_upper.contains(&a_upper) {
622 entry.0 += 1;
623 } else if response_upper.contains(&b_upper) {
624 entry.1 += 1;
625 }
626 }
627 }
628
629 let mut wins: HashMap<String, usize> = HashMap::new();
631 for a in actions {
632 wins.insert(a.name.clone(), 0);
633 }
634
635 for (_pair_idx, (a_count, b_count, a_name, b_name)) in pair_votes {
636 if a_count >= b_count {
638 *wins.get_mut(&b_name).unwrap() += 1;
640 } else {
641 *wins.get_mut(&a_name).unwrap() += 1;
643 }
644 }
645
646 let mut sorted: Vec<_> = wins.into_iter().collect();
648 sorted.sort_by_key(|(_, count)| *count);
649
650 tracing::debug!(
651 sorted = ?sorted.iter().map(|(n, c)| format!("{}:{}", n, c)).collect::<Vec<_>>(),
652 "Binary sort completed"
653 );
654
655 sorted.into_iter().map(|(name, _)| name).collect()
656}
657
658#[cfg(test)]
663mod tests {
664 use super::*;
665
666 #[test]
667 fn test_batch_process_error_transient() {
668 let err = BatchProcessError::transient("connection timeout");
669 assert!(err.is_transient());
670 assert_eq!(err.message(), "connection timeout");
671 }
672
673 #[test]
674 fn test_batch_process_error_permanent() {
675 let err = BatchProcessError::permanent("invalid model");
676 assert!(!err.is_transient());
677 assert_eq!(err.message(), "invalid model");
678 }
679
680 #[test]
681 fn test_batch_process_error_from_llm_error() {
682 let llm_err = LlmError::transient("timeout");
683 let batch_err: BatchProcessError = llm_err.into();
684 assert!(batch_err.is_transient());
685 assert_eq!(batch_err.message(), "timeout");
686 }
687
688 #[test]
689 fn test_ollama_batch_processor_config_default() {
690 let config = LlmBatchProcessorConfig::default();
691 assert!(config.parallel);
692 assert_eq!(config.max_concurrency, 4);
693 }
694
695 use std::collections::HashMap;
700
701 fn binary_sort_sync(
704 actions: &[&str],
705 comparator: impl Fn(&str, &str) -> String,
707 ) -> Vec<String> {
708 if actions.len() <= 1 {
709 return actions.iter().map(|s| s.to_string()).collect();
710 }
711
712 let mut wins: HashMap<String, usize> = HashMap::new();
713 for &a in actions {
714 wins.insert(a.to_string(), 0);
715 }
716
717 for i in 0..actions.len() {
718 for j in (i + 1)..actions.len() {
719 let a = actions[i];
720 let b = actions[j];
721 let winner = comparator(a, b);
722
723 if winner == a {
725 *wins.get_mut(b).unwrap() += 1;
726 } else {
727 *wins.get_mut(a).unwrap() += 1;
728 }
729 }
730 }
731
732 let mut sorted: Vec<_> = wins.into_iter().collect();
733 sorted.sort_by_key(|(_, count)| *count);
734 sorted.into_iter().map(|(name, _)| name).collect()
735 }
736
737 #[test]
738 fn test_binary_sort_two_actions() {
739 let result = binary_sort_sync(
741 &["Fetch", "Summarize"],
742 |a, _b| a.to_string(), );
744 assert_eq!(result, vec!["Fetch", "Summarize"]);
745
746 let result = binary_sort_sync(
748 &["Fetch", "Summarize"],
749 |_a, b| b.to_string(), );
751 assert_eq!(result, vec!["Summarize", "Fetch"]);
752 }
753
754 #[test]
755 fn test_binary_sort_three_actions() {
756 let result = binary_sort_sync(&["Test", "Deploy", "Build"], |a, b| {
759 let order = ["Build", "Test", "Deploy"];
760 let a_idx = order.iter().position(|&x| x == a).unwrap();
761 let b_idx = order.iter().position(|&x| x == b).unwrap();
762 if a_idx < b_idx {
763 a.to_string()
764 } else {
765 b.to_string()
766 }
767 });
768 assert_eq!(result, vec!["Build", "Test", "Deploy"]);
769 }
770
771 #[test]
772 fn test_binary_sort_wins_calculation() {
773 let mut wins: HashMap<String, usize> = HashMap::new();
783 wins.insert("A".to_string(), 0);
784 wins.insert("B".to_string(), 0);
785 wins.insert("C".to_string(), 0);
786
787 *wins.get_mut("B").unwrap() += 1;
789 *wins.get_mut("C").unwrap() += 1;
791 *wins.get_mut("C").unwrap() += 1;
793
794 assert_eq!(wins["A"], 0);
795 assert_eq!(wins["B"], 1);
796 assert_eq!(wins["C"], 2);
797
798 let mut sorted: Vec<_> = wins.into_iter().collect();
799 sorted.sort_by_key(|(_, count)| *count);
800 let result: Vec<_> = sorted.into_iter().map(|(name, _)| name).collect();
801
802 assert_eq!(result, vec!["A", "B", "C"]);
803 }
804
805 fn extract_winner(response: &str, a: &str, b: &str) -> Option<String> {
807 let response_upper = response.to_uppercase();
808 let a_upper = a.to_uppercase();
809 let b_upper = b.to_uppercase();
810
811 if response_upper.contains(&a_upper) {
812 Some(a.to_string())
813 } else if response_upper.contains(&b_upper) {
814 Some(b.to_string())
815 } else {
816 None
817 }
818 }
819
820 #[test]
821 fn test_extract_winner() {
822 assert_eq!(
824 extract_winner("Fetch", "Fetch", "Summarize"),
825 Some("Fetch".to_string())
826 );
827 assert_eq!(
828 extract_winner("Summarize", "Fetch", "Summarize"),
829 Some("Summarize".to_string())
830 );
831
832 assert_eq!(
834 extract_winner(" Fetch", "Fetch", "Summarize"),
835 Some("Fetch".to_string())
836 );
837
838 assert_eq!(
840 extract_winner("fetch", "Fetch", "Summarize"),
841 Some("Fetch".to_string())
842 );
843 assert_eq!(
844 extract_winner("FETCH", "Fetch", "Summarize"),
845 Some("Fetch".to_string())
846 );
847
848 assert_eq!(
850 extract_winner("The answer is Fetch.", "Fetch", "Summarize"),
851 Some("Fetch".to_string())
852 );
853
854 assert_eq!(extract_winner("Unknown", "Fetch", "Summarize"), None);
856
857 assert_eq!(
859 extract_winner("Fetch then Summarize", "Fetch", "Summarize"),
860 Some("Fetch".to_string())
861 );
862 }
863
864 #[test]
865 fn test_vote_majority() {
866 fn vote_majority(responses: &[&str], a: &str, b: &str) -> String {
868 let mut a_count = 0;
869 let mut b_count = 0;
870
871 for response in responses {
872 if let Some(winner) = extract_winner(response, a, b) {
873 if winner == a {
874 a_count += 1;
875 } else {
876 b_count += 1;
877 }
878 }
879 }
880
881 if a_count >= b_count {
882 a.to_string()
883 } else {
884 b.to_string()
885 }
886 }
887
888 assert_eq!(
890 vote_majority(&["Fetch", "Fetch", "Fetch"], "Fetch", "Summarize"),
891 "Fetch"
892 );
893
894 assert_eq!(
896 vote_majority(&["Fetch", "Summarize", "Fetch"], "Fetch", "Summarize"),
897 "Fetch"
898 );
899
900 assert_eq!(
902 vote_majority(&["Summarize", "Summarize", "Fetch"], "Fetch", "Summarize"),
903 "Summarize"
904 );
905
906 assert_eq!(
908 vote_majority(&["Fetch", "Summarize", "Unknown"], "Fetch", "Summarize"),
909 "Fetch"
910 );
911 }
912
913 use swarm_engine_core::context::{ContextTarget, GlobalContext, ResolvedContext};
918
919 fn create_test_request(
920 worker_id: usize,
921 lora: Option<LoraConfig>,
922 ) -> (WorkerId, WorkerDecisionRequest) {
923 let global = GlobalContext {
924 tick: 0,
925 max_ticks: 100,
926 progress: 0.0,
927 success_rate: 0.0,
928 task_description: Some("test".to_string()),
929 hint: None,
930 };
931 let context = ResolvedContext::new(global, ContextTarget::Worker(WorkerId(worker_id)));
932
933 (
934 WorkerId(worker_id),
935 WorkerDecisionRequest {
936 worker_id: WorkerId(worker_id),
937 query: format!("query_{}", worker_id),
938 context,
939 lora,
940 },
941 )
942 }
943
944 #[test]
945 fn test_group_by_lora_single_group_no_lora() {
946 let requests = vec![
947 create_test_request(0, None),
948 create_test_request(1, None),
949 create_test_request(2, None),
950 ];
951
952 let groups = group_by_lora(requests);
953
954 assert_eq!(groups.len(), 1);
955 assert!(groups.contains_key(&None));
956 assert_eq!(groups[&None].len(), 3);
957 }
958
959 #[test]
960 fn test_group_by_lora_single_group_with_lora() {
961 let lora = LoraConfig::with_id(0);
962 let requests = vec![
963 create_test_request(0, Some(lora.clone())),
964 create_test_request(1, Some(lora.clone())),
965 ];
966
967 let groups = group_by_lora(requests);
968
969 assert_eq!(groups.len(), 1);
970 assert!(groups.contains_key(&Some(lora)));
971 }
972
973 #[test]
974 fn test_group_by_lora_multiple_groups() {
975 let lora_a = LoraConfig::with_id(0);
976 let lora_b = LoraConfig::with_id(1);
977
978 let requests = vec![
979 create_test_request(0, Some(lora_a.clone())),
980 create_test_request(1, Some(lora_b.clone())),
981 create_test_request(2, Some(lora_a.clone())),
982 create_test_request(3, None),
983 create_test_request(4, Some(lora_b.clone())),
984 ];
985
986 let groups = group_by_lora(requests);
987
988 assert_eq!(groups.len(), 3);
989 assert_eq!(groups[&Some(lora_a)].len(), 2);
990 assert_eq!(groups[&Some(lora_b)].len(), 2);
991 assert_eq!(groups[&None].len(), 1);
992 }
993
994 #[test]
995 fn test_group_by_lora_preserves_order_within_group() {
996 let lora = LoraConfig::with_id(0);
997 let requests = vec![
998 create_test_request(5, Some(lora.clone())),
999 create_test_request(3, Some(lora.clone())),
1000 create_test_request(7, Some(lora.clone())),
1001 ];
1002
1003 let groups = group_by_lora(requests);
1004 let group = &groups[&Some(lora)];
1005
1006 assert_eq!(group[0].0, WorkerId(5));
1008 assert_eq!(group[1].0, WorkerId(3));
1009 assert_eq!(group[2].0, WorkerId(7));
1010 }
1011
1012 #[test]
1013 fn test_group_by_lora_different_scales() {
1014 let lora_full = LoraConfig::new(0, 1.0);
1016 let lora_half = LoraConfig::new(0, 0.5);
1017
1018 let requests = vec![
1019 create_test_request(0, Some(lora_full.clone())),
1020 create_test_request(1, Some(lora_half.clone())),
1021 create_test_request(2, Some(lora_full.clone())),
1022 ];
1023
1024 let groups = group_by_lora(requests);
1025
1026 assert_eq!(groups.len(), 2);
1027 assert_eq!(groups[&Some(lora_full)].len(), 2);
1028 assert_eq!(groups[&Some(lora_half)].len(), 1);
1029 }
1030
1031 #[test]
1032 fn test_group_by_lora_empty() {
1033 let requests: Vec<(WorkerId, WorkerDecisionRequest)> = vec![];
1034 let groups = group_by_lora(requests);
1035 assert!(groups.is_empty());
1036 }
1037}