swarm-engine-core 0.1.6

Core types and orchestration 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
//! Learning Daemon - 継続的学習プロセス
//!
//! ## 概要
//!
//! Swarm セッションを跨いで動作する長寿命プロセス。
//! イベントを受信し、データを永続化し、Trigger 発火時に学習を実行する。
//!
//! ## アーキテクチャ
//!
//! ```text
//! EventSource → DataSink → Store
//!//!             TriggerLoop → Processor → Applier
//! ```
//!
//! ## 使用例
//!
//! ```ignore
//! use swarm_engine_core::learn::daemon::{LearningDaemon, DaemonConfig};
//!
//! let config = DaemonConfig::new("my-scenario")
//!     .trigger(TriggerBuilder::every_n_episodes(100))
//!     .auto_apply(true);
//!
//! let daemon = LearningDaemon::new(config)?;
//! daemon.run().await?;
//! ```

mod applier;
mod processor;
mod sink;
mod subscriber;

pub use applier::{Applier, ApplierConfig, ApplierError, ApplyMode, ApplyResult};
pub use processor::{ProcessResult, Processor, ProcessorConfig, ProcessorError, ProcessorMode};
pub use sink::{DataSink, DataSinkError, DataSinkStats};
pub use subscriber::{ActionEventSubscriber, EventSubscriberConfig, LearningEventSubscriber};

use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

use tokio::sync::mpsc;
use tokio::time::interval;

use crate::learn::learn_model::{LearnModel, WorkerDecisionSequenceLearn};
use crate::learn::lora::{
    LoraTrainer, LoraTrainerConfig, ModelApplicator, NoOpApplicator, TrainedModel,
};
use crate::learn::record::{DependencyGraphRecord, LearnStatsRecord, Record};
use crate::learn::snapshot::LearningStore;
use crate::learn::store::{
    EpisodeStore, FileEpisodeStore, FileRecordStore, InMemoryEpisodeStore, InMemoryRecordStore,
    RecordStore, RecordStoreError, StoreError,
};
use crate::learn::trigger::{TrainTrigger, TriggerBuilder, TriggerContext};
use crate::learn::LearnStats;
use crate::util::epoch_millis;

// ============================================================================
// DaemonError
// ============================================================================

/// Daemon のエラー型
#[derive(Debug)]
pub enum DaemonError {
    /// DataSink エラー
    Sink(DataSinkError),
    /// Processor エラー
    Processor(ProcessorError),
    /// Applier エラー
    Applier(ApplierError),
    /// IO エラー
    Io(std::io::Error),
    /// 設定エラー
    Config(String),
    /// Shutdown
    Shutdown,
}

impl std::fmt::Display for DaemonError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Sink(e) => write!(f, "Sink error: {}", e),
            Self::Processor(e) => write!(f, "Processor error: {}", e),
            Self::Applier(e) => write!(f, "Applier error: {}", e),
            Self::Io(e) => write!(f, "IO error: {}", e),
            Self::Config(msg) => write!(f, "Config error: {}", msg),
            Self::Shutdown => write!(f, "Daemon shutdown"),
        }
    }
}

impl std::error::Error for DaemonError {}

impl From<DataSinkError> for DaemonError {
    fn from(e: DataSinkError) -> Self {
        Self::Sink(e)
    }
}

impl From<ProcessorError> for DaemonError {
    fn from(e: ProcessorError) -> Self {
        Self::Processor(e)
    }
}

impl From<ApplierError> for DaemonError {
    fn from(e: ApplierError) -> Self {
        Self::Applier(e)
    }
}

impl From<std::io::Error> for DaemonError {
    fn from(e: std::io::Error) -> Self {
        Self::Io(e)
    }
}

impl From<RecordStoreError> for DaemonError {
    fn from(e: RecordStoreError) -> Self {
        Self::Sink(DataSinkError::RecordStore(e))
    }
}

impl From<StoreError> for DaemonError {
    fn from(e: StoreError) -> Self {
        Self::Sink(DataSinkError::EpisodeStore(e))
    }
}

// ============================================================================
// DaemonConfig
// ============================================================================

/// Daemon の設定
#[derive(Debug, Clone)]
pub struct DaemonConfig {
    /// シナリオ名
    pub scenario: String,
    /// データディレクトリ
    pub data_dir: PathBuf,
    /// Trigger チェック間隔
    pub check_interval: Duration,
    /// 処理モード
    pub processor_mode: ProcessorMode,
    /// Offline 分析に使用するセッション数
    pub max_sessions: usize,
    /// 自動適用
    pub auto_apply: bool,
    /// LoRA 設定(None の場合は LoRA 学習しない)
    pub lora_config: Option<LoraTrainerConfig>,
}

impl DaemonConfig {
    /// 新しい設定を作成
    pub fn new(scenario: impl Into<String>) -> Self {
        Self {
            scenario: scenario.into(),
            data_dir: default_data_dir(),
            check_interval: Duration::from_secs(10),
            processor_mode: ProcessorMode::OfflineOnly,
            max_sessions: 20,
            auto_apply: false,
            lora_config: None,
        }
    }

    /// データディレクトリを設定
    pub fn data_dir(mut self, path: impl Into<PathBuf>) -> Self {
        self.data_dir = path.into();
        self
    }

    /// Trigger チェック間隔を設定
    pub fn check_interval(mut self, interval: Duration) -> Self {
        self.check_interval = interval;
        self
    }

    /// 処理モードを設定
    pub fn processor_mode(mut self, mode: ProcessorMode) -> Self {
        self.processor_mode = mode;
        self
    }

    /// 最大セッション数を設定
    pub fn max_sessions(mut self, n: usize) -> Self {
        self.max_sessions = n;
        self
    }

    /// 自動適用を設定
    pub fn auto_apply(mut self, enabled: bool) -> Self {
        self.auto_apply = enabled;
        self
    }

    /// LoRA 設定を追加
    pub fn with_lora(mut self, config: LoraTrainerConfig) -> Self {
        self.lora_config = Some(config);
        self
    }
}

fn default_data_dir() -> PathBuf {
    dirs::data_dir()
        .unwrap_or_else(|| PathBuf::from("."))
        .join("swarm-engine")
        .join("learning")
}

// ============================================================================
// DaemonStats
// ============================================================================

/// Daemon の統計情報
#[derive(Debug, Clone, Default)]
pub struct DaemonStats {
    /// 受信した Record 数
    pub records_received: usize,
    /// 生成した Episode 数
    pub episodes_created: usize,
    /// 実行した学習回数
    pub trainings_completed: usize,
    /// 適用したモデル数
    pub models_applied: usize,
    /// 最終学習時刻(Unix timestamp ms)
    pub last_train_at: Option<u64>,
    /// 起動時刻(Unix timestamp ms)
    pub started_at: u64,
}

// ============================================================================
// LearningDaemon
// ============================================================================

/// 継続的学習プロセス
pub struct LearningDaemon {
    /// 設定
    config: DaemonConfig,
    /// DataSink
    sink: DataSink,
    /// Trigger
    trigger: Arc<dyn TrainTrigger>,
    /// Processor
    processor: Processor,
    /// Applier(オプション)
    applier: Option<Applier>,
    /// LearningStore(session 保存用)
    learning_store: LearningStore,
    /// 統計情報
    stats: DaemonStats,
    /// 最終学習時の Episode 数
    last_train_count: usize,
    /// Record 受信チャンネル
    record_rx: mpsc::Receiver<Vec<Record>>,
    /// Record 送信チャンネル(外部公開用)
    record_tx: mpsc::Sender<Vec<Record>>,
    /// Shutdown チャンネル
    shutdown_rx: mpsc::Receiver<()>,
    /// Shutdown 送信チャンネル(外部公開用)
    shutdown_tx: mpsc::Sender<()>,
}

impl LearningDaemon {
    /// 新しい Daemon を作成(In-Memory Store)
    pub fn new(config: DaemonConfig, trigger: Arc<dyn TrainTrigger>) -> Result<Self, DaemonError> {
        let record_store: Arc<dyn RecordStore> = Arc::new(InMemoryRecordStore::new());
        let episode_store: Arc<dyn EpisodeStore> = Arc::new(InMemoryEpisodeStore::new());
        let learn_model: Arc<dyn LearnModel> = Arc::new(WorkerDecisionSequenceLearn::new());

        Self::with_stores(config, trigger, record_store, episode_store, learn_model)
    }

    /// 新しい Daemon を作成(File Store)
    pub fn with_file_stores(
        config: DaemonConfig,
        trigger: Arc<dyn TrainTrigger>,
    ) -> Result<Self, DaemonError> {
        // ディレクトリ作成
        std::fs::create_dir_all(&config.data_dir)?;

        let record_store: Arc<dyn RecordStore> =
            Arc::new(FileRecordStore::new(config.data_dir.join("records"))?);
        let episode_store: Arc<dyn EpisodeStore> =
            Arc::new(FileEpisodeStore::new(config.data_dir.join("episodes"))?);
        let learn_model: Arc<dyn LearnModel> = Arc::new(WorkerDecisionSequenceLearn::new());

        Self::with_stores(config, trigger, record_store, episode_store, learn_model)
    }

    /// カスタム Store で Daemon を作成
    pub fn with_stores(
        config: DaemonConfig,
        trigger: Arc<dyn TrainTrigger>,
        record_store: Arc<dyn RecordStore>,
        episode_store: Arc<dyn EpisodeStore>,
        learn_model: Arc<dyn LearnModel>,
    ) -> Result<Self, DaemonError> {
        // DataSink
        let sink = DataSink::new(
            record_store,
            Arc::clone(&episode_store),
            Arc::clone(&learn_model),
        );

        // Processor
        let processor_config = ProcessorConfig::new(&config.scenario)
            .mode(config.processor_mode)
            .max_sessions(config.max_sessions);

        let mut processor = Processor::new(processor_config);

        // LearningStore for offline analysis and session persistence
        let learning_store = LearningStore::new(&config.data_dir)?;
        // Clone for processor (processor needs its own reference)
        let learning_store_for_processor = LearningStore::new(&config.data_dir)?;
        processor = processor.with_learning_store(learning_store_for_processor);

        // LoRA Trainer (if configured)
        if let Some(lora_config) = &config.lora_config {
            let trainer = LoraTrainer::new(lora_config.clone(), episode_store);
            processor = processor
                .with_lora_trainer(trainer)
                .with_learn_model(learn_model);
        }

        // Applier
        let applier = if config.auto_apply {
            let applier_config = ApplierConfig::default().auto_apply();
            // TODO: Real applicator based on config
            let applicator: Arc<dyn ModelApplicator> = Arc::new(NoOpApplicator::new());
            Some(Applier::new(applier_config, applicator))
        } else {
            None
        };

        // Channels
        let (record_tx, record_rx) = mpsc::channel(1000);
        let (shutdown_tx, shutdown_rx) = mpsc::channel(1);

        Ok(Self {
            config,
            sink,
            trigger,
            processor,
            applier,
            learning_store,
            stats: DaemonStats {
                started_at: epoch_millis(),
                ..Default::default()
            },
            last_train_count: 0,
            record_rx,
            record_tx,
            shutdown_rx,
            shutdown_tx,
        })
    }

    /// Record 送信チャンネルを取得
    pub fn record_sender(&self) -> mpsc::Sender<Vec<Record>> {
        self.record_tx.clone()
    }

    /// Shutdown 送信チャンネルを取得
    pub fn shutdown_sender(&self) -> mpsc::Sender<()> {
        self.shutdown_tx.clone()
    }

    /// 設定を取得
    pub fn config(&self) -> &DaemonConfig {
        &self.config
    }

    /// 統計情報を取得
    pub fn stats(&self) -> &DaemonStats {
        &self.stats
    }

    /// メインループを実行
    pub async fn run(&mut self) -> Result<(), DaemonError> {
        tracing::info!(
            scenario = %self.config.scenario,
            data_dir = %self.config.data_dir.display(),
            trigger = self.trigger.name(),
            "Learning daemon started"
        );

        let mut check_interval = interval(self.config.check_interval);

        loop {
            tokio::select! {
                // Shutdown signal
                _ = self.shutdown_rx.recv() => {
                    tracing::info!("Shutdown signal received, draining remaining records...");

                    // Drain remaining records before shutdown
                    // Give subscribers a moment to flush their batches
                    tokio::time::sleep(std::time::Duration::from_millis(100)).await;

                    // Process any remaining records in the channel
                    while let Ok(records) = self.record_rx.try_recv() {
                        if let Err(e) = self.handle_records(records).await {
                            tracing::warn!("Error processing records during shutdown: {}", e);
                        }
                    }

                    tracing::info!(
                        records_received = self.stats.records_received,
                        episodes_created = self.stats.episodes_created,
                        "Shutdown complete"
                    );
                    return Ok(());
                }

                // Record batch received
                Some(records) = self.record_rx.recv() => {
                    self.handle_records(records).await?;
                }

                // Periodic trigger check
                _ = check_interval.tick() => {
                    self.check_and_train().await?;
                }
            }
        }
    }

    /// Record を処理
    async fn handle_records(&mut self, records: Vec<Record>) -> Result<(), DaemonError> {
        if records.is_empty() {
            return Ok(());
        }

        let count = records.len();

        // LearnStatsRecord と DependencyGraphRecord を検出して LearningStore に保存
        for record in &records {
            match record {
                Record::LearnStats(stats_record) => {
                    self.save_stats_to_learning_store(stats_record);
                }
                Record::DependencyGraph(dep_graph_record) => {
                    self.save_dependency_graph_to_learning_store(dep_graph_record);
                }
                _ => {}
            }
        }

        let episode_ids = self.sink.ingest(records)?;

        self.stats.records_received += count;
        self.stats.episodes_created += episode_ids.len();

        tracing::debug!(
            records = count,
            episodes = episode_ids.len(),
            "Processed records"
        );

        Ok(())
    }

    /// LearnStatsRecord を LearningSnapshot に変換して保存
    fn save_stats_to_learning_store(&self, stats_record: &LearnStatsRecord) {
        use crate::learn::snapshot::{LearningSnapshot, SnapshotMetadata, SNAPSHOT_VERSION};
        use crate::learn::{EpisodeTransitions, NgramStats, SelectionPerformance};
        use crate::online_stats::ActionStats;
        use std::collections::HashMap;

        // LearnStats JSON をデシリアライズ
        let learn_stats: Option<LearnStats> = serde_json::from_str(&stats_record.stats_json).ok();

        // SnapshotMetadata を作成(実際のフィールドのみ使用)
        let metadata = SnapshotMetadata {
            scenario_name: Some(stats_record.scenario.clone()),
            task_description: None,
            created_at: stats_record.timestamp_ms / 1000, // ms → s
            session_count: 1,
            total_episodes: 1,
            total_actions: stats_record.total_actions as u32,
            phase: None,
            group_id: None,
        };

        // LearnStats からデータを抽出(あれば)
        let (
            episode_transitions,
            action_stats,
            ngram_stats,
            selection_performance,
            contextual_stats,
        ) = if let Some(ref stats) = learn_stats {
            // EpisodeTransitions を直接コピー
            let transitions = stats.episode_transitions.clone();

            // NgramStats を直接コピー
            let ngram = stats.ngram_stats.clone();

            // SelectionPerformance を直接コピー
            let selection = stats.selection_performance.clone();

            // contextual_stats を ActionStats に変換
            let mut ctx_stats: HashMap<(String, String), ActionStats> = HashMap::new();
            for ((prev, action), ctx) in &stats.contextual_stats {
                ctx_stats.insert(
                    (prev.clone(), action.clone()),
                    ActionStats {
                        visits: ctx.visits,
                        successes: ctx.successes,
                        failures: ctx.failures,
                        ..Default::default()
                    },
                );
            }

            // action_stats は空(LearnStats には直接のアクション統計はない)
            let action_stats: HashMap<String, ActionStats> = HashMap::new();

            (transitions, action_stats, ngram, selection, ctx_stats)
        } else {
            (
                EpisodeTransitions::default(),
                HashMap::new(),
                NgramStats::default(),
                SelectionPerformance::default(),
                HashMap::new(),
            )
        };

        // LearningSnapshot を作成
        let snapshot = LearningSnapshot {
            version: SNAPSHOT_VERSION,
            metadata,
            episode_transitions,
            ngram_stats,
            selection_performance,
            contextual_stats,
            action_stats,
        };

        // LearningStore に保存
        match self
            .learning_store
            .save_session(&stats_record.scenario, &snapshot)
        {
            Ok(session_id) => {
                tracing::info!(
                    scenario = %stats_record.scenario,
                    session_id = %session_id.0,
                    success = stats_record.is_success(),
                    "Saved session to LearningStore"
                );
            }
            Err(e) => {
                tracing::warn!(
                    scenario = %stats_record.scenario,
                    error = %e,
                    "Failed to save session to LearningStore"
                );
            }
        }
    }

    /// DependencyGraphRecord を LearningStore に保存
    ///
    /// DependencyGraph 推論結果を action_order として OfflineModel に追加保存する。
    /// これにより `learn once` で action_order を抽出できるようになる。
    fn save_dependency_graph_to_learning_store(&self, record: &DependencyGraphRecord) {
        use crate::learn::{ActionOrderSource, LearnedActionOrder};

        // action_set_hash を計算(LearnedActionOrder::compute_hash を使用)
        let all_actions: Vec<String> = record
            .discover_order
            .iter()
            .chain(record.not_discover_order.iter())
            .cloned()
            .collect();
        let action_set_hash = LearnedActionOrder::compute_hash(&all_actions);

        let action_order = LearnedActionOrder {
            discover: record.discover_order.clone(),
            not_discover: record.not_discover_order.clone(),
            action_set_hash,
            source: ActionOrderSource::Llm,
            lora: None,
            validated_accuracy: None,
        };

        // OfflineModel をロードして action_order を更新
        let scenario = &self.config.scenario;
        let model_result = self.learning_store.load_offline_model(scenario);

        let updated_model = match model_result {
            Ok(mut model) => {
                model.action_order = Some(action_order.clone());
                model
            }
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                // OfflineModel がなければ新規作成
                crate::learn::OfflineModel {
                    action_order: Some(action_order.clone()),
                    ..Default::default()
                }
            }
            Err(e) => {
                tracing::warn!(
                    scenario = %scenario,
                    error = %e,
                    "Failed to load OfflineModel for action_order update"
                );
                return;
            }
        };

        // 保存
        match self
            .learning_store
            .save_offline_model(scenario, &updated_model)
        {
            Ok(()) => {
                tracing::info!(
                    scenario = %scenario,
                    discover = ?action_order.discover,
                    not_discover = ?action_order.not_discover,
                    action_set_hash = action_order.action_set_hash,
                    "Saved action_order to OfflineModel"
                );
            }
            Err(e) => {
                tracing::warn!(
                    scenario = %scenario,
                    error = %e,
                    "Failed to save action_order to OfflineModel"
                );
            }
        }
    }

    /// Trigger をチェックし、必要なら学習を実行
    async fn check_and_train(&mut self) -> Result<(), DaemonError> {
        // Trigger 判定用コンテキスト
        let current_count = self.sink.episode_count();
        let ctx = TriggerContext::with_count(current_count)
            .last_train_at(self.stats.last_train_at.unwrap_or(0))
            .last_train_count(self.last_train_count);

        if !self.trigger.should_train(&ctx).unwrap_or(false) {
            return Ok(());
        }

        tracing::info!(
            episode_count = current_count,
            trigger = self.trigger.name(),
            "Trigger fired, starting learning"
        );

        // 学習実行
        let result = self
            .processor
            .run(self.sink.episode_store().as_ref())
            .await?;

        // 統計更新
        self.stats.trainings_completed += 1;
        self.stats.last_train_at = Some(epoch_millis());
        self.last_train_count = current_count;

        // 自動適用
        if let Some(applier) = &mut self.applier {
            if let Some(model) = result.lora_model() {
                let apply_result = applier.apply(model).await?;
                if apply_result.is_applied() {
                    self.stats.models_applied += 1;
                }
            }
        }

        tracing::info!(
            trainings = self.stats.trainings_completed,
            models_applied = self.stats.models_applied,
            "Learning cycle completed"
        );

        Ok(())
    }

    /// 手動で学習を実行(Trigger 無視)
    pub async fn train_now(&mut self) -> Result<ProcessResult, DaemonError> {
        tracing::info!("Manual training triggered");

        let result = self
            .processor
            .run(self.sink.episode_store().as_ref())
            .await?;

        self.stats.trainings_completed += 1;
        self.stats.last_train_at = Some(epoch_millis());
        self.last_train_count = self.sink.episode_count();

        Ok(result)
    }

    /// 手動でモデルを適用(非同期)
    pub async fn apply_model(&mut self, model: &TrainedModel) -> Result<ApplyResult, DaemonError> {
        let applier = self
            .applier
            .as_mut()
            .ok_or_else(|| DaemonError::Config("Applier not configured".into()))?;

        let result = applier.apply_now(model).await?;
        if result.is_applied() {
            self.stats.models_applied += 1;
        }

        Ok(result)
    }
}

// ============================================================================
// Builder Pattern
// ============================================================================

/// LearningDaemon のビルダー
pub struct DaemonBuilder {
    config: DaemonConfig,
    trigger: Option<Arc<dyn TrainTrigger>>,
    record_store: Option<Arc<dyn RecordStore>>,
    episode_store: Option<Arc<dyn EpisodeStore>>,
    learn_model: Option<Arc<dyn LearnModel>>,
    applicator: Option<Arc<dyn ModelApplicator>>,
}

impl DaemonBuilder {
    /// 新しいビルダーを作成
    pub fn new(scenario: impl Into<String>) -> Self {
        Self {
            config: DaemonConfig::new(scenario),
            trigger: None,
            record_store: None,
            episode_store: None,
            learn_model: None,
            applicator: None,
        }
    }

    /// データディレクトリを設定
    pub fn data_dir(mut self, path: impl Into<PathBuf>) -> Self {
        self.config.data_dir = path.into();
        self
    }

    /// Trigger を設定
    pub fn trigger(mut self, trigger: Arc<dyn TrainTrigger>) -> Self {
        self.trigger = Some(trigger);
        self
    }

    /// 処理モードを設定
    pub fn processor_mode(mut self, mode: ProcessorMode) -> Self {
        self.config.processor_mode = mode;
        self
    }

    /// 自動適用を有効化
    pub fn auto_apply(mut self) -> Self {
        self.config.auto_apply = true;
        self
    }

    /// RecordStore を設定
    pub fn record_store(mut self, store: Arc<dyn RecordStore>) -> Self {
        self.record_store = Some(store);
        self
    }

    /// EpisodeStore を設定
    pub fn episode_store(mut self, store: Arc<dyn EpisodeStore>) -> Self {
        self.episode_store = Some(store);
        self
    }

    /// LearnModel を設定
    pub fn learn_model(mut self, model: Arc<dyn LearnModel>) -> Self {
        self.learn_model = Some(model);
        self
    }

    /// ModelApplicator を設定
    pub fn applicator(mut self, applicator: Arc<dyn ModelApplicator>) -> Self {
        self.applicator = Some(applicator);
        self
    }

    /// LoRA 設定を追加
    pub fn with_lora(mut self, config: LoraTrainerConfig) -> Self {
        self.config.lora_config = Some(config);
        self
    }

    /// Daemon をビルド
    pub fn build(self) -> Result<LearningDaemon, DaemonError> {
        let trigger = self
            .trigger
            .unwrap_or_else(|| TriggerBuilder::default_watch());

        let record_store = self
            .record_store
            .unwrap_or_else(|| Arc::new(InMemoryRecordStore::new()));

        let episode_store = self
            .episode_store
            .unwrap_or_else(|| Arc::new(InMemoryEpisodeStore::new()));

        let learn_model = self
            .learn_model
            .unwrap_or_else(|| Arc::new(WorkerDecisionSequenceLearn::new()));

        LearningDaemon::with_stores(
            self.config,
            trigger,
            record_store,
            episode_store,
            learn_model,
        )
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::events::{ActionContext, ActionEventBuilder, ActionEventResult};
    use crate::learn::trigger::AlwaysTrigger;
    use crate::types::WorkerId;

    fn make_test_records(count: usize) -> Vec<Record> {
        (0..count)
            .map(|i| {
                let event = ActionEventBuilder::new(i as u64, WorkerId(0), format!("Action{}", i))
                    .result(ActionEventResult::success())
                    .duration(std::time::Duration::from_millis(10))
                    .context(ActionContext::new())
                    .build();
                Record::from(&event)
            })
            .collect()
    }

    #[test]
    fn test_daemon_config_builder() {
        let config = DaemonConfig::new("test")
            .data_dir("/tmp/test")
            .check_interval(Duration::from_secs(30))
            .processor_mode(ProcessorMode::Full)
            .auto_apply(true);

        assert_eq!(config.scenario, "test");
        assert_eq!(config.data_dir, PathBuf::from("/tmp/test"));
        assert_eq!(config.check_interval, Duration::from_secs(30));
        assert_eq!(config.processor_mode, ProcessorMode::Full);
        assert!(config.auto_apply);
    }

    #[tokio::test]
    async fn test_daemon_creation() {
        let config = DaemonConfig::new("test");
        let trigger = TriggerBuilder::never();

        let daemon = LearningDaemon::new(config, trigger).unwrap();
        assert_eq!(daemon.config().scenario, "test");
        assert_eq!(daemon.stats().records_received, 0);
    }

    #[tokio::test]
    async fn test_daemon_record_ingestion() {
        let config = DaemonConfig::new("test");
        let trigger = TriggerBuilder::never(); // Never auto-train

        let mut daemon = LearningDaemon::new(config, trigger).unwrap();
        let sender = daemon.record_sender();

        // Send records
        let records = make_test_records(5);
        sender.send(records).await.unwrap();

        // Process one batch
        daemon.handle_records(make_test_records(3)).await.unwrap();

        assert_eq!(daemon.stats().records_received, 3);
    }

    #[tokio::test]
    async fn test_daemon_builder() {
        let daemon = DaemonBuilder::new("test-scenario")
            .data_dir("/tmp/test")
            .trigger(Arc::new(AlwaysTrigger))
            .processor_mode(ProcessorMode::OfflineOnly)
            .build()
            .unwrap();

        assert_eq!(daemon.config().scenario, "test-scenario");
    }
}