swarm-engine-llm 0.1.6

LLM integration backends for SwarmEngine
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
//! Batch Processor - ManagerAgent 向け Batch LLM 処理
//!
//! ManagerAgent の `BatchDecisionRequest` を処理するための抽象化レイヤー。
//!
//! # 設計
//!
//! ```text
//! Core Layer
//! ├── ManagerAgent trait (prepare / finalize)
//! ├── DefaultBatchManagerAgent   ← Core層のデフォルト実装
//! ├── ContextStore / ContextView ← 正規化されたコンテキスト
//! └── ContextResolver            ← スコープ解決
//!
//! LLM Layer
//! ├── PromptBuilder              ← ResolvedContext → プロンプト
//! ├── BatchProcessor trait       ← Batch 処理の抽象
//! │   └── LlmBatchProcessor   ← Ollama 実装(仮想バッチ)
//!//! └── BatchInvoker 実装          ← LLM Batch 呼び出し
//!     └── OllamaBatchInvoker
//! ```
//!
//! # 仮想バッチ vs 真のバッチ
//!
//! Ollama は真の Batch API を持たないため、`LlmBatchProcessor` は
//! 内部で並列/順次処理を行う「仮想バッチ」として実装されます。
//! 将来 vLLM 等の真の Batch API を持つバックエンドに切り替える際は、
//! `BatchProcessor` trait の別実装を提供するだけで対応可能です。
//!
//! # 型の統一
//!
//! LLM層はCore層の型を直接使用するため、変換ロジックは不要です:
//! - `WorkerDecisionRequest` - リクエスト
//! - `DecisionResponse` - レスポンス

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use std::collections::HashMap;

use swarm_engine_core::actions::ActionDef;
use swarm_engine_core::agent::{BatchDecisionRequest, DecisionResponse, WorkerDecisionRequest};
use swarm_engine_core::exploration::{DependencyGraph, SelectResult};
use swarm_engine_core::types::{LoraConfig, WorkerId};

use crate::decider::{LlmDecider, LlmError};

// ============================================================================
// BatchProcessor Trait
// ============================================================================

/// Batch 処理結果
pub type BatchProcessResult = Vec<(WorkerId, Result<DecisionResponse, BatchProcessError>)>;

/// Batch 処理エラー
#[derive(Debug, Clone, thiserror::Error)]
pub enum BatchProcessError {
    /// 一時的エラー(リトライ可能)
    #[error("Batch process error (transient): {0}")]
    Transient(String),

    /// 恒久的エラー(リトライ不可)
    #[error("Batch process error: {0}")]
    Permanent(String),
}

impl BatchProcessError {
    pub fn transient(message: impl Into<String>) -> Self {
        Self::Transient(message.into())
    }

    pub fn permanent(message: impl Into<String>) -> Self {
        Self::Permanent(message.into())
    }

    pub fn is_transient(&self) -> bool {
        matches!(self, Self::Transient(_))
    }

    pub fn message(&self) -> &str {
        match self {
            Self::Transient(msg) => msg,
            Self::Permanent(msg) => msg,
        }
    }
}

impl From<LlmError> for BatchProcessError {
    fn from(e: LlmError) -> Self {
        if e.is_transient() {
            Self::Transient(e.message().to_string())
        } else {
            Self::Permanent(e.message().to_string())
        }
    }
}

impl From<swarm_engine_core::error::SwarmError> for BatchProcessError {
    fn from(err: swarm_engine_core::error::SwarmError) -> Self {
        if err.is_transient() {
            Self::Transient(err.message())
        } else {
            Self::Permanent(err.message())
        }
    }
}

impl From<BatchProcessError> for swarm_engine_core::error::SwarmError {
    fn from(err: BatchProcessError) -> Self {
        match err {
            BatchProcessError::Transient(message) => {
                swarm_engine_core::error::SwarmError::LlmTransient { message }
            }
            BatchProcessError::Permanent(message) => {
                swarm_engine_core::error::SwarmError::LlmPermanent { message }
            }
        }
    }
}

/// Batch Processor trait
///
/// `BatchDecisionRequest` を受け取り、各 Worker への決定を返す。
/// バックエンド(Ollama, vLLM 等)に応じた実装を提供する。
pub trait BatchProcessor: Send + Sync {
    /// Batch リクエストを処理
    ///
    /// # Arguments
    /// * `request` - Core の `BatchDecisionRequest`
    ///
    /// # Returns
    /// 各 Worker への決定結果(WorkerId とペア)
    fn process(
        &self,
        request: BatchDecisionRequest,
    ) -> Pin<Box<dyn Future<Output = BatchProcessResult> + Send + '_>>;

    /// タスクとアクション一覧からアクション依存グラフを生成
    ///
    /// Swarm の Ticks 開始前に呼び出され、アクション間の依存関係を計画する。
    /// LLM を使用して動的に依存グラフを生成する。
    ///
    /// # Arguments
    ///
    /// * `task` - タスク説明
    /// * `actions` - 利用可能なアクション一覧
    /// * `hint` - `LearnedDependencyProvider.select()` の結果(LoRA、vote_count 等を含む)
    ///
    /// # Default
    /// デフォルトでは None を返す(依存グラフ生成をスキップ)。
    fn plan_dependencies(
        &self,
        _task: &str,
        _actions: &[ActionDef],
        _hint: Option<&SelectResult>,
    ) -> Pin<Box<dyn Future<Output = Option<DependencyGraph>> + Send + '_>> {
        Box::pin(async { None })
    }

    /// ヘルスチェック
    fn is_healthy(&self) -> Pin<Box<dyn Future<Output = bool> + Send + '_>>;

    /// プロセッサ名
    fn name(&self) -> &str;
}

// ============================================================================
// LlmBatchProcessor
// ============================================================================

/// Ollama Batch Processor 設定
#[derive(Debug, Clone)]
pub struct LlmBatchProcessorConfig {
    /// 並列実行するか(false の場合は順次処理)
    pub parallel: bool,
    /// 並列実行時の最大同時実行数
    pub max_concurrency: usize,
    /// DependencyGraph 生成時の最大リトライ回数
    pub max_retries: Option<usize>,
}

impl Default for LlmBatchProcessorConfig {
    fn default() -> Self {
        Self {
            parallel: true,
            max_concurrency: 4,
            max_retries: Some(5),
        }
    }
}

/// Ollama Batch Processor
///
/// Ollama は真の Batch API を持たないため、仮想バッチとして実装。
/// 内部で `LlmDecider` を使用して並列/順次処理を行う。
pub struct LlmBatchProcessor<D: LlmDecider> {
    decider: Arc<D>,
    config: LlmBatchProcessorConfig,
}

impl<D: LlmDecider> LlmBatchProcessor<D> {
    /// 新しい LlmBatchProcessor を作成
    pub fn new(decider: D) -> Self {
        Self {
            decider: Arc::new(decider),
            config: LlmBatchProcessorConfig::default(),
        }
    }

    /// Arc でラップされた Decider から作成
    pub fn from_arc(decider: Arc<D>) -> Self {
        Self {
            decider,
            config: LlmBatchProcessorConfig::default(),
        }
    }

    /// 設定を指定して作成
    pub fn with_config(mut self, config: LlmBatchProcessorConfig) -> Self {
        self.config = config;
        self
    }
}

impl<D: LlmDecider + 'static> BatchProcessor for LlmBatchProcessor<D> {
    fn process(
        &self,
        request: BatchDecisionRequest,
    ) -> Pin<Box<dyn Future<Output = BatchProcessResult> + Send + '_>> {
        Box::pin(async move {
            if request.requests.is_empty() {
                return vec![];
            }

            // Core の WorkerDecisionRequest をそのまま使用(変換不要)
            let requests: Vec<(WorkerId, WorkerDecisionRequest)> = request
                .requests
                .into_iter()
                .map(|r| (r.worker_id, r))
                .collect();

            if self.config.parallel {
                self.process_parallel(requests).await
            } else {
                self.process_sequential(requests).await
            }
        })
    }

    fn plan_dependencies(
        &self,
        task: &str,
        actions: &[ActionDef],
        hint: Option<&SelectResult>,
    ) -> Pin<Box<dyn Future<Output = Option<DependencyGraph>> + Send + '_>> {
        let task = task.to_string();
        let actions: Vec<ActionDef> = actions.to_vec();
        let decider = Arc::clone(&self.decider);

        // SelectResult から lora と vote_count を抽出
        let (lora, vote_count) = match hint {
            Some(SelectResult::UseLlm {
                lora,
                vote_count,
                match_rate,
                ..
            }) => {
                tracing::debug!(
                    match_rate = match_rate,
                    vote_count = vote_count,
                    has_lora = lora.is_some(),
                    "Using SelectResult hint for plan_dependencies"
                );
                (lora.clone(), *vote_count)
            }
            _ => {
                // ヒントなし or UseLearnedGraph(呼ばれるべきでない)
                tracing::debug!("No SelectResult hint, using defaults (lora=None, vote_count=3)");
                (None, 3)
            }
        };

        Box::pin(async move {
            use std::time::Instant;
            use swarm_engine_core::actions::ActionCategory;
            use swarm_engine_core::exploration::DependencyGraphBuilder;

            let start_time = Instant::now();
            let action_names: Vec<String> = actions.iter().map(|a| a.name.clone()).collect();

            // 1. Discover (NodeExpand) と NotDiscover (NodeStateChange) に分離
            let discover: Vec<&ActionDef> = actions
                .iter()
                .filter(|a| a.category == ActionCategory::NodeExpand)
                .collect();
            let not_discover: Vec<&ActionDef> = actions
                .iter()
                .filter(|a| a.category == ActionCategory::NodeStateChange)
                .collect();

            tracing::debug!(
                discover = ?discover.iter().map(|a| &a.name).collect::<Vec<_>>(),
                not_discover = ?not_discover.iter().map(|a| &a.name).collect::<Vec<_>>(),
                "Separated actions by category"
            );

            // 2. Discover を Binary + Vote でソート(SelectResult の vote_count と lora を使用)
            let discover_sort_start = Instant::now();
            let sorted_discover = if discover.len() <= 1 {
                discover.iter().map(|a| a.name.clone()).collect()
            } else {
                binary_sort_actions(&task, &discover, decider.as_ref(), lora.as_ref(), vote_count).await
            };
            let discover_sort_ms = discover_sort_start.elapsed().as_millis();

            tracing::debug!(
                sorted = ?sorted_discover,
                elapsed_ms = discover_sort_ms,
                vote_count = vote_count,
                has_lora = lora.is_some(),
                "Sorted Discover actions via binary comparison"
            );

            // 3. NotDiscover を Binary + Vote でソート(同じく SelectResult のパラメータを使用)
            let not_discover_sort_start = Instant::now();
            let sorted_not_discover = if not_discover.len() <= 1 {
                not_discover.iter().map(|a| a.name.clone()).collect()
            } else {
                binary_sort_actions(&task, &not_discover, decider.as_ref(), lora.as_ref(), vote_count).await
            };
            let not_discover_sort_ms = not_discover_sort_start.elapsed().as_millis();

            tracing::debug!(
                sorted = ?sorted_not_discover,
                elapsed_ms = not_discover_sort_ms,
                "Sorted NotDiscover actions via binary comparison"
            );

            // 4. グラフ構築: Discover(線形)→ NotDiscover(線形)
            let mut builder = DependencyGraphBuilder::new()
                .task(&task)
                .available_actions(action_names.clone());

            // 最初の Discover を Start node として設定
            if !sorted_discover.is_empty() {
                builder = builder.start_node(&sorted_discover[0]);
            } else if !sorted_not_discover.is_empty() {
                // Discover がなければ最初の NotDiscover を Start に
                builder = builder.start_node(&sorted_not_discover[0]);
            }

            // NotDiscover の最後を Terminal に
            if let Some(last) = sorted_not_discover.last() {
                builder = builder.terminal_node(last);
            } else if !sorted_discover.is_empty() {
                // NotDiscover がなければ最後の Discover を Terminal に
                builder = builder.terminal_node(sorted_discover.last().unwrap());
            }

            // Discover 間のエッジ(線形)
            for window in sorted_discover.windows(2) {
                builder = builder.edge(&window[0], &window[1], 0.9);
            }

            // 最後の Discover → 最初の NotDiscover へのエッジ
            if !sorted_discover.is_empty() && !sorted_not_discover.is_empty() {
                builder = builder.edge(
                    sorted_discover.last().unwrap(),
                    &sorted_not_discover[0],
                    0.9,
                );
            }

            // NotDiscover 間のエッジ(線形)
            for window in sorted_not_discover.windows(2) {
                builder = builder.edge(&window[0], &window[1], 0.9);
            }

            let mut graph = builder.build();
            let total_ms = start_time.elapsed().as_millis();

            // Store action order for caching
            graph.set_action_order(sorted_discover.clone(), sorted_not_discover.clone());

            // Create learning record for DependencyGraph inference
            {
                use swarm_engine_core::events::{LearningEvent, LearningEventChannel};
                use swarm_engine_core::learn::DependencyGraphRecord;

                // Build a summary prompt representing the inference input
                let prompt = format!(
                    "Task: {}\n\nAvailable Actions:\n{}",
                    task,
                    action_names
                        .iter()
                        .map(|n| format!("- {}", n))
                        .collect::<Vec<_>>()
                        .join("\n")
                );

                // Build a summary response representing the inference output
                let response = format!(
                    "discover_order: {:?}\nnot_discover_order: {:?}",
                    sorted_discover, sorted_not_discover
                );

                // Create Event and emit to LearningEventChannel
                let event = LearningEvent::dependency_graph_inference(decider.model_name())
                    .prompt(&prompt)
                    .response(&response)
                    .available_actions(action_names)
                    .discover_order(sorted_discover.clone())
                    .not_discover_order(sorted_not_discover.clone())
                    .endpoint(decider.endpoint())
                    .latency_ms(total_ms as u64)
                    .success()
                    .build();

                // Emit event for learning pipeline
                LearningEventChannel::global().emit(event.clone());

                // Convert Event to Record for graph storage
                let record = DependencyGraphRecord::from(&event);
                graph.set_learn_record(record);
            }

            tracing::info!(
                discover_order = ?sorted_discover,
                not_discover_order = ?sorted_not_discover,
                edges = graph.edges().len(),
                discover_sort_ms = discover_sort_ms,
                not_discover_sort_ms = not_discover_sort_ms,
                total_ms = total_ms,
                "DependencyGraph generated via LLM binary sort"
            );

            Some(graph)
        })
    }

    fn is_healthy(&self) -> Pin<Box<dyn Future<Output = bool> + Send + '_>> {
        let decider = Arc::clone(&self.decider);
        Box::pin(async move { decider.is_healthy().await })
    }

    fn name(&self) -> &str {
        self.decider.model_name()
    }
}

impl<D: LlmDecider + 'static> LlmBatchProcessor<D> {
    /// 並列実行(LoRA グルーピング + Semaphore で同時実行数を制限)
    ///
    /// # LoRA グルーピング
    ///
    /// llama.cpp の continuous batching では、同じ LoRA 設定のリクエストは
    /// 効率的にバッチ処理される。異なる LoRA を混ぜると効率が落ちるため、
    /// リクエストを LoRA 設定でグルーピングして処理する。
    ///
    /// ```text
    /// リクエスト群
    /// ├── LoRA A のリクエスト群 → 並列実行(グループ内)
    /// ├── LoRA B のリクエスト群 → 並列実行(グループ内)
    /// └── LoRA なしのリクエスト群 → 並列実行(グループ内)
    /// ```
    ///
    /// グループ間は順次処理(同じ LoRA を連続して処理することで効率化)
    async fn process_parallel(
        &self,
        requests: Vec<(WorkerId, WorkerDecisionRequest)>,
    ) -> BatchProcessResult {
        // リクエストを LoRA 設定でグルーピング
        let grouped = group_by_lora(requests);

        let group_count = grouped.len();
        if group_count > 1 {
            tracing::debug!(
                groups = group_count,
                "Processing requests in {} LoRA groups",
                group_count
            );
        }

        // 各グループを順次処理(グループ内は並列)
        let mut all_results = Vec::new();
        for (lora_config, group_requests) in grouped {
            if group_count > 1 {
                tracing::trace!(
                    lora = ?lora_config,
                    count = group_requests.len(),
                    "Processing LoRA group"
                );
            }
            let results = self.process_group(group_requests).await;
            all_results.extend(results);
        }

        all_results
    }

    /// 単一グループの並列処理(Semaphore で同時実行数を制限)
    async fn process_group(
        &self,
        requests: Vec<(WorkerId, WorkerDecisionRequest)>,
    ) -> BatchProcessResult {
        use futures::future::join_all;
        use tokio::sync::Semaphore;

        // サーバーからスロット数を取得、取得できなければconfig値を使用
        let max_concurrency = self
            .decider
            .max_concurrency()
            .await
            .unwrap_or(self.config.max_concurrency);

        let semaphore = Arc::new(Semaphore::new(max_concurrency));

        let futures: Vec<_> = requests
            .into_iter()
            .map(|(worker_id, req)| {
                let decider = Arc::clone(&self.decider);
                let sem = Arc::clone(&semaphore);
                async move {
                    // スロットを取得してから実行
                    let _permit = sem.acquire().await.expect("Semaphore closed");
                    let result = decider.decide(req).await;
                    (worker_id, result)
                }
            })
            .collect();

        let results = join_all(futures).await;

        results
            .into_iter()
            .map(|(worker_id, result)| {
                let mapped = result.map_err(BatchProcessError::from);
                (worker_id, mapped)
            })
            .collect()
    }

    /// 順次実行
    async fn process_sequential(
        &self,
        requests: Vec<(WorkerId, WorkerDecisionRequest)>,
    ) -> BatchProcessResult {
        let mut results = Vec::with_capacity(requests.len());

        for (worker_id, req) in requests {
            let result = self.decider.decide(req).await;
            let mapped = result.map_err(BatchProcessError::from);
            results.push((worker_id, mapped));
        }

        results
    }
}

/// リクエストを LoRA 設定でグルーピング
///
/// 同じ LoRA 設定(または LoRA なし)のリクエストをまとめる。
/// HashMap の順序は不定だが、グループ内の順序は保持される。
fn group_by_lora(
    requests: Vec<(WorkerId, WorkerDecisionRequest)>,
) -> HashMap<Option<LoraConfig>, Vec<(WorkerId, WorkerDecisionRequest)>> {
    let mut groups: HashMap<Option<LoraConfig>, Vec<(WorkerId, WorkerDecisionRequest)>> =
        HashMap::new();

    for (worker_id, req) in requests {
        let lora_key = req.lora.clone();
        groups.entry(lora_key).or_default().push((worker_id, req));
    }

    groups
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Binary + Vote でアクションをソート(バッチ版)
///
/// 全ペア × N回分のプロンプトを一括でバッチ送信し、結果を集計。
/// 勝ち数でソート(勝ち数が少ない = 先に来る)。
///
/// # Arguments
///
/// * `task` - タスクの説明
/// * `actions` - ソート対象のアクション
/// * `decider` - LLM 決定器
/// * `lora` - LoRA 設定(None = Base Model)
/// * `vote_count` - 投票回数(1 or 3、0 は呼び出し元で処理)
async fn binary_sort_actions<D: LlmDecider>(
    task: &str,
    actions: &[&ActionDef],
    decider: &D,
    lora: Option<&LoraConfig>,
    vote_count: u8,
) -> Vec<String> {
    use futures::future::join_all;
    use std::collections::HashMap;

    if actions.len() <= 1 {
        return actions.iter().map(|a| a.name.clone()).collect();
    }

    // 全ペア × 3回分のリクエストを作成
    // (pair_index, vote_index, prompt, a_name, b_name)
    let mut requests: Vec<(usize, usize, String, String, String)> = Vec::new();
    let mut pair_index = 0;

    for i in 0..actions.len() {
        for j in (i + 1)..actions.len() {
            let a = actions[i];
            let b = actions[j];
            let prompt = format!(
                "Goal: {}\n- {}: {}\n- {}: {}\nWhich comes first: {} or {}?\nAnswer (one word):",
                task, a.name, a.description, b.name, b.description, a.name, b.name
            );

            // 同じペアを vote_count 回投げる
            for vote_idx in 0..vote_count as usize {
                requests.push((
                    pair_index,
                    vote_idx,
                    prompt.clone(),
                    a.name.clone(),
                    b.name.clone(),
                ));
            }
            pair_index += 1;
        }
    }

    let total_requests = requests.len();
    tracing::debug!(
        pairs = pair_index,
        total_requests = total_requests,
        "Binary sort: sending batch requests"
    );

    // 全リクエストを並列で送信
    // LoRA が指定されていれば適用
    let futures: Vec<_> = requests
        .into_iter()
        .map(|(pair_idx, vote_idx, prompt, a_name, b_name)| {
            let decider_ref = decider;
            async move {
                let result = decider_ref.call_raw(&prompt, lora).await;
                (pair_idx, vote_idx, result, a_name, b_name)
            }
        })
        .collect();

    let results = join_all(futures).await;

    // ペアごとに投票結果を集計
    // pair_index -> (a_count, b_count, a_name, b_name)
    let mut pair_votes: HashMap<usize, (usize, usize, String, String)> = HashMap::new();

    for (pair_idx, _vote_idx, result, a_name, b_name) in results {
        let entry = pair_votes
            .entry(pair_idx)
            .or_insert((0, 0, a_name.clone(), b_name.clone()));

        if let Ok(response) = result {
            let response_upper = response.to_uppercase();
            let a_upper = a_name.to_uppercase();
            let b_upper = b_name.to_uppercase();

            if response_upper.contains(&a_upper) {
                entry.0 += 1;
            } else if response_upper.contains(&b_upper) {
                entry.1 += 1;
            }
        }
    }

    // 各アクションの「勝ち数」をカウント
    let mut wins: HashMap<String, usize> = HashMap::new();
    for a in actions {
        wins.insert(a.name.clone(), 0);
    }

    for (_pair_idx, (a_count, b_count, a_name, b_name)) in pair_votes {
        // winner = 「先に来る方」なので、もう一方が「後」= 勝ち
        if a_count >= b_count {
            // a が先 → b に勝ち+1
            *wins.get_mut(&b_name).unwrap() += 1;
        } else {
            // b が先 → a に勝ち+1
            *wins.get_mut(&a_name).unwrap() += 1;
        }
    }

    // 勝ち数が少ない順にソート(先に来るものが少ない)
    let mut sorted: Vec<_> = wins.into_iter().collect();
    sorted.sort_by_key(|(_, count)| *count);

    tracing::debug!(
        sorted = ?sorted.iter().map(|(n, c)| format!("{}:{}", n, c)).collect::<Vec<_>>(),
        "Binary sort completed"
    );

    sorted.into_iter().map(|(name, _)| name).collect()
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_batch_process_error_transient() {
        let err = BatchProcessError::transient("connection timeout");
        assert!(err.is_transient());
        assert_eq!(err.message(), "connection timeout");
    }

    #[test]
    fn test_batch_process_error_permanent() {
        let err = BatchProcessError::permanent("invalid model");
        assert!(!err.is_transient());
        assert_eq!(err.message(), "invalid model");
    }

    #[test]
    fn test_batch_process_error_from_llm_error() {
        let llm_err = LlmError::transient("timeout");
        let batch_err: BatchProcessError = llm_err.into();
        assert!(batch_err.is_transient());
        assert_eq!(batch_err.message(), "timeout");
    }

    #[test]
    fn test_ollama_batch_processor_config_default() {
        let config = LlmBatchProcessorConfig::default();
        assert!(config.parallel);
        assert_eq!(config.max_concurrency, 4);
    }

    // =========================================================================
    // Binary Sort Tests
    // =========================================================================

    use std::collections::HashMap;

    /// 同期版の binary_sort (テスト用)
    /// wins の計算ロジックをテスト
    fn binary_sort_sync(
        actions: &[&str],
        // (a, b) -> winner (先に来る方)
        comparator: impl Fn(&str, &str) -> String,
    ) -> Vec<String> {
        if actions.len() <= 1 {
            return actions.iter().map(|s| s.to_string()).collect();
        }

        let mut wins: HashMap<String, usize> = HashMap::new();
        for &a in actions {
            wins.insert(a.to_string(), 0);
        }

        for i in 0..actions.len() {
            for j in (i + 1)..actions.len() {
                let a = actions[i];
                let b = actions[j];
                let winner = comparator(a, b);

                // winner = 先に来る方 → もう一方が後 = 勝ち
                if winner == a {
                    *wins.get_mut(b).unwrap() += 1;
                } else {
                    *wins.get_mut(a).unwrap() += 1;
                }
            }
        }

        let mut sorted: Vec<_> = wins.into_iter().collect();
        sorted.sort_by_key(|(_, count)| *count);
        sorted.into_iter().map(|(name, _)| name).collect()
    }

    #[test]
    fn test_binary_sort_two_actions() {
        // Fetch が先、Summarize が後
        let result = binary_sort_sync(
            &["Fetch", "Summarize"],
            |a, _b| a.to_string(), // 常に a が先
        );
        assert_eq!(result, vec!["Fetch", "Summarize"]);

        // Summarize が先、Fetch が後
        let result = binary_sort_sync(
            &["Fetch", "Summarize"],
            |_a, b| b.to_string(), // 常に b が先
        );
        assert_eq!(result, vec!["Summarize", "Fetch"]);
    }

    #[test]
    fn test_binary_sort_three_actions() {
        // Test -> Deploy の順
        // comparator: 常に正しい順序を返す
        let result = binary_sort_sync(&["Test", "Deploy", "Build"], |a, b| {
            let order = ["Build", "Test", "Deploy"];
            let a_idx = order.iter().position(|&x| x == a).unwrap();
            let b_idx = order.iter().position(|&x| x == b).unwrap();
            if a_idx < b_idx {
                a.to_string()
            } else {
                b.to_string()
            }
        });
        assert_eq!(result, vec!["Build", "Test", "Deploy"]);
    }

    #[test]
    fn test_binary_sort_wins_calculation() {
        // 3つのアクション: A, B, C
        // 正しい順序: A -> B -> C
        // 比較結果:
        //   A vs B -> A が先 -> B に+1
        //   A vs C -> A が先 -> C に+1
        //   B vs C -> B が先 -> C に+1
        // wins = {A: 0, B: 1, C: 2}
        // ソート後: A(0), B(1), C(2)

        let mut wins: HashMap<String, usize> = HashMap::new();
        wins.insert("A".to_string(), 0);
        wins.insert("B".to_string(), 0);
        wins.insert("C".to_string(), 0);

        // A vs B: A が先 → B に+1
        *wins.get_mut("B").unwrap() += 1;
        // A vs C: A が先 → C に+1
        *wins.get_mut("C").unwrap() += 1;
        // B vs C: B が先 → C に+1
        *wins.get_mut("C").unwrap() += 1;

        assert_eq!(wins["A"], 0);
        assert_eq!(wins["B"], 1);
        assert_eq!(wins["C"], 2);

        let mut sorted: Vec<_> = wins.into_iter().collect();
        sorted.sort_by_key(|(_, count)| *count);
        let result: Vec<_> = sorted.into_iter().map(|(name, _)| name).collect();

        assert_eq!(result, vec!["A", "B", "C"]);
    }

    /// response から winner を抽出するロジックのテスト
    fn extract_winner(response: &str, a: &str, b: &str) -> Option<String> {
        let response_upper = response.to_uppercase();
        let a_upper = a.to_uppercase();
        let b_upper = b.to_uppercase();

        if response_upper.contains(&a_upper) {
            Some(a.to_string())
        } else if response_upper.contains(&b_upper) {
            Some(b.to_string())
        } else {
            None
        }
    }

    #[test]
    fn test_extract_winner() {
        // 正常ケース
        assert_eq!(
            extract_winner("Fetch", "Fetch", "Summarize"),
            Some("Fetch".to_string())
        );
        assert_eq!(
            extract_winner("Summarize", "Fetch", "Summarize"),
            Some("Summarize".to_string())
        );

        // 先頭スペース
        assert_eq!(
            extract_winner(" Fetch", "Fetch", "Summarize"),
            Some("Fetch".to_string())
        );

        // 大文字小文字
        assert_eq!(
            extract_winner("fetch", "Fetch", "Summarize"),
            Some("Fetch".to_string())
        );
        assert_eq!(
            extract_winner("FETCH", "Fetch", "Summarize"),
            Some("Fetch".to_string())
        );

        // 文中に含まれる
        assert_eq!(
            extract_winner("The answer is Fetch.", "Fetch", "Summarize"),
            Some("Fetch".to_string())
        );

        // どちらも含まれない
        assert_eq!(extract_winner("Unknown", "Fetch", "Summarize"), None);

        // 両方含まれる場合は先にマッチした方
        assert_eq!(
            extract_winner("Fetch then Summarize", "Fetch", "Summarize"),
            Some("Fetch".to_string())
        );
    }

    #[test]
    fn test_vote_majority() {
        // 3回の投票で多数決
        fn vote_majority(responses: &[&str], a: &str, b: &str) -> String {
            let mut a_count = 0;
            let mut b_count = 0;

            for response in responses {
                if let Some(winner) = extract_winner(response, a, b) {
                    if winner == a {
                        a_count += 1;
                    } else {
                        b_count += 1;
                    }
                }
            }

            if a_count >= b_count {
                a.to_string()
            } else {
                b.to_string()
            }
        }

        // 3回とも Fetch
        assert_eq!(
            vote_majority(&["Fetch", "Fetch", "Fetch"], "Fetch", "Summarize"),
            "Fetch"
        );

        // 2回 Fetch, 1回 Summarize
        assert_eq!(
            vote_majority(&["Fetch", "Summarize", "Fetch"], "Fetch", "Summarize"),
            "Fetch"
        );

        // 2回 Summarize, 1回 Fetch
        assert_eq!(
            vote_majority(&["Summarize", "Summarize", "Fetch"], "Fetch", "Summarize"),
            "Summarize"
        );

        // 同数の場合は a (Fetch) を返す
        assert_eq!(
            vote_majority(&["Fetch", "Summarize", "Unknown"], "Fetch", "Summarize"),
            "Fetch"
        );
    }

    // =========================================================================
    // LoRA Grouping Tests
    // =========================================================================

    use swarm_engine_core::context::{ContextTarget, GlobalContext, ResolvedContext};

    fn create_test_request(
        worker_id: usize,
        lora: Option<LoraConfig>,
    ) -> (WorkerId, WorkerDecisionRequest) {
        let global = GlobalContext {
            tick: 0,
            max_ticks: 100,
            progress: 0.0,
            success_rate: 0.0,
            task_description: Some("test".to_string()),
            hint: None,
        };
        let context = ResolvedContext::new(global, ContextTarget::Worker(WorkerId(worker_id)));

        (
            WorkerId(worker_id),
            WorkerDecisionRequest {
                worker_id: WorkerId(worker_id),
                query: format!("query_{}", worker_id),
                context,
                lora,
            },
        )
    }

    #[test]
    fn test_group_by_lora_single_group_no_lora() {
        let requests = vec![
            create_test_request(0, None),
            create_test_request(1, None),
            create_test_request(2, None),
        ];

        let groups = group_by_lora(requests);

        assert_eq!(groups.len(), 1);
        assert!(groups.contains_key(&None));
        assert_eq!(groups[&None].len(), 3);
    }

    #[test]
    fn test_group_by_lora_single_group_with_lora() {
        let lora = LoraConfig::with_id(0);
        let requests = vec![
            create_test_request(0, Some(lora.clone())),
            create_test_request(1, Some(lora.clone())),
        ];

        let groups = group_by_lora(requests);

        assert_eq!(groups.len(), 1);
        assert!(groups.contains_key(&Some(lora)));
    }

    #[test]
    fn test_group_by_lora_multiple_groups() {
        let lora_a = LoraConfig::with_id(0);
        let lora_b = LoraConfig::with_id(1);

        let requests = vec![
            create_test_request(0, Some(lora_a.clone())),
            create_test_request(1, Some(lora_b.clone())),
            create_test_request(2, Some(lora_a.clone())),
            create_test_request(3, None),
            create_test_request(4, Some(lora_b.clone())),
        ];

        let groups = group_by_lora(requests);

        assert_eq!(groups.len(), 3);
        assert_eq!(groups[&Some(lora_a)].len(), 2);
        assert_eq!(groups[&Some(lora_b)].len(), 2);
        assert_eq!(groups[&None].len(), 1);
    }

    #[test]
    fn test_group_by_lora_preserves_order_within_group() {
        let lora = LoraConfig::with_id(0);
        let requests = vec![
            create_test_request(5, Some(lora.clone())),
            create_test_request(3, Some(lora.clone())),
            create_test_request(7, Some(lora.clone())),
        ];

        let groups = group_by_lora(requests);
        let group = &groups[&Some(lora)];

        // グループ内の順序は保持される
        assert_eq!(group[0].0, WorkerId(5));
        assert_eq!(group[1].0, WorkerId(3));
        assert_eq!(group[2].0, WorkerId(7));
    }

    #[test]
    fn test_group_by_lora_different_scales() {
        // 同じ ID でも scale が違えば別グループ
        let lora_full = LoraConfig::new(0, 1.0);
        let lora_half = LoraConfig::new(0, 0.5);

        let requests = vec![
            create_test_request(0, Some(lora_full.clone())),
            create_test_request(1, Some(lora_half.clone())),
            create_test_request(2, Some(lora_full.clone())),
        ];

        let groups = group_by_lora(requests);

        assert_eq!(groups.len(), 2);
        assert_eq!(groups[&Some(lora_full)].len(), 2);
        assert_eq!(groups[&Some(lora_half)].len(), 1);
    }

    #[test]
    fn test_group_by_lora_empty() {
        let requests: Vec<(WorkerId, WorkerDecisionRequest)> = vec![];
        let groups = group_by_lora(requests);
        assert!(groups.is_empty());
    }
}