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
//! Event Subscribers - Event を受信して Record に変換し LearningDaemon に送信
//!
//! ## 設計原則: Event ごとに Subscriber を追加
//!
//! **新しい Event Channel を追加したら、対応する Subscriber も追加すること。**
//!
//! 各 Subscriber は単一責務(Single Responsibility)で設計されている:
//! - `ActionEventSubscriber`: `ActionEvent` を処理
//! - `LearningEventSubscriber`: `LearningEvent` を処理
//!
//! これにより:
//! - 各 Subscriber のテストが独立
//! - 新しい Event 追加時の影響範囲が限定的
//! - 必要な Subscriber のみを起動可能
//!
//! ## Event → Record → Episode の流れ
//!
//! **Subscriber は Event と Record の橋渡し役。**
//! Production でオンラインで学習データを収集するための重要なコンポーネント。
//!
//! ```text
//! [Event Layer]              [Subscriber]                    [Learn Layer]
//!
//! ActionEventPublisher ──▶ ActionEventSubscriber ──────┐
//!//!                          Record::from(&event)        ├──▶ LearningDaemon
//!                                                      │         │
//! LearningEventChannel ──▶ LearningEventSubscriber ────┘         ▼
//!                                                           Episode
//!//!//!                                                         LoRA / Learn
//! ```
//!
//! ## 新しい Event Channel を追加する際(Checklist)
//!
//! 1. `events/` に Event 型を定義
//! 2. `learn/record/` に `From<&Event> for *Record` を実装
//! 3. `record/mod.rs` で `From<&Event> for Record` にルーティング追加
//! 4. **このファイルに対応する `*EventSubscriber` を追加**
//! 5. `mod.rs` で re-export
//! 6. 使用箇所で Subscriber を起動
//!
//! **重要**: Builder で直接 Record を作らない。必ず Event 経由で変換する。
//!
//! ## 対応 Event と Subscriber
//!
//! | Event | Subscriber | Record | 変換場所 |
//! |-------|------------|--------|----------|
//! | `ActionEvent` | `ActionEventSubscriber` | `ActionRecord` | `record/action.rs` |
//! | `LearningEvent::StrategyAdvice` | `LearningEventSubscriber` | `ActionRecord` | `record/mod.rs` |
//! | `LearningEvent::DependencyGraphInference` | `LearningEventSubscriber` | `DependencyGraphRecord` | `record/dependency_graph.rs` |
//!
//! ## 使用例
//!
//! ```ignore
//! use swarm_engine_core::events::{ActionEventPublisher, LearningEventChannel};
//! use swarm_engine_core::learn::daemon::{
//!     ActionEventSubscriber, LearningEventSubscriber, EventSubscriberConfig, LearningDaemon
//! };
//!
//! let (publisher, _rx) = ActionEventPublisher::new(1024);
//! let daemon = LearningDaemon::new(config, trigger)?;
//! let record_tx = daemon.record_sender();
//!
//! // ActionEvent Subscriber 起動
//! let action_sub = ActionEventSubscriber::new(publisher.subscribe(), record_tx.clone());
//! tokio::spawn(action_sub.run());
//!
//! // LearningEvent Subscriber 起動
//! let learning_channel = LearningEventChannel::global();
//! learning_channel.enable();
//! let learning_sub = LearningEventSubscriber::new(learning_channel.subscribe(), record_tx);
//! tokio::spawn(learning_sub.run());
//! ```

use tokio::sync::{broadcast, mpsc};

use crate::events::{ActionEvent, LearningEvent};
use crate::learn::record::Record;

// ============================================================================
// EventSubscriberConfig
// ============================================================================

/// Event Subscriber の共通設定
///
/// 全ての Subscriber で共有される設定。
/// バッチ処理とフラッシュ間隔を制御する。
#[derive(Debug, Clone)]
pub struct EventSubscriberConfig {
    /// バッチサイズ(この数に達したら送信)
    pub batch_size: usize,
    /// フラッシュ間隔(None の場合は batch_size のみで判定)
    pub flush_interval_ms: Option<u64>,
}

impl Default for EventSubscriberConfig {
    fn default() -> Self {
        Self {
            batch_size: 100,
            flush_interval_ms: Some(1000), // 1秒
        }
    }
}

impl EventSubscriberConfig {
    /// 新しい設定を作成
    pub fn new() -> Self {
        Self::default()
    }

    /// バッチサイズを設定
    pub fn batch_size(mut self, size: usize) -> Self {
        self.batch_size = size;
        self
    }

    /// フラッシュ間隔を設定(ミリ秒)
    pub fn flush_interval_ms(mut self, ms: u64) -> Self {
        self.flush_interval_ms = Some(ms);
        self
    }

    /// フラッシュ間隔を無効化
    pub fn no_flush_interval(mut self) -> Self {
        self.flush_interval_ms = None;
        self
    }
}

// ============================================================================
// ActionEventSubscriber
// ============================================================================

/// ActionEvent を受信して LearningDaemon に Record を送信
///
/// `ActionEventPublisher` から `ActionEvent` を受信し、
/// `Record` に変換してバッチで `LearningDaemon` に送信する。
///
/// ## 対応 Event
///
/// - `ActionEvent` → `ActionRecord`
pub struct ActionEventSubscriber {
    /// ActionEvent 受信チャンネル
    rx: broadcast::Receiver<ActionEvent>,
    /// Record 送信チャンネル(LearningDaemon.record_sender())
    record_tx: mpsc::Sender<Vec<Record>>,
    /// 設定
    config: EventSubscriberConfig,
    /// バッファ
    buffer: Vec<Record>,
}

impl ActionEventSubscriber {
    /// 新しい ActionEventSubscriber を作成
    pub fn new(rx: broadcast::Receiver<ActionEvent>, record_tx: mpsc::Sender<Vec<Record>>) -> Self {
        Self::with_config(rx, record_tx, EventSubscriberConfig::default())
    }

    /// 設定を指定して ActionEventSubscriber を作成
    pub fn with_config(
        rx: broadcast::Receiver<ActionEvent>,
        record_tx: mpsc::Sender<Vec<Record>>,
        config: EventSubscriberConfig,
    ) -> Self {
        let batch_size = config.batch_size;
        Self {
            rx,
            record_tx,
            config,
            buffer: Vec::with_capacity(batch_size),
        }
    }

    /// 受信ループを開始
    ///
    /// `ActionEvent` を受信し、`Record` に変換してバッチ送信する。
    /// チャンネルが閉じられるか、送信先が閉じられたら終了。
    pub async fn run(mut self) {
        tracing::info!(
            batch_size = self.config.batch_size,
            flush_interval_ms = ?self.config.flush_interval_ms,
            "ActionEventSubscriber started"
        );

        if let Some(interval_ms) = self.config.flush_interval_ms {
            self.run_with_flush_interval(interval_ms).await;
        } else {
            self.run_batch_only().await;
        }

        // Flush remaining records
        self.flush().await;

        tracing::info!("ActionEventSubscriber stopped");
    }

    /// フラッシュ間隔ありの受信ループ
    async fn run_with_flush_interval(&mut self, interval_ms: u64) {
        use std::time::Duration;
        use tokio::time::{interval, Instant};

        let mut flush_interval = interval(Duration::from_millis(interval_ms));
        let mut last_flush = Instant::now();

        loop {
            tokio::select! {
                // ActionEvent 受信
                result = self.rx.recv() => {
                    match result {
                        Ok(event) => {
                            self.buffer.push(Record::from(&event));

                            if self.buffer.len() >= self.config.batch_size {
                                if !self.flush().await {
                                    return;
                                }
                                last_flush = Instant::now();
                            }
                        }
                        Err(broadcast::error::RecvError::Closed) => {
                            tracing::debug!("ActionEvent channel closed");
                            return;
                        }
                        Err(broadcast::error::RecvError::Lagged(n)) => {
                            tracing::warn!(lagged = n, "ActionEventSubscriber lagged behind");
                        }
                    }
                }

                // 定期フラッシュ
                _ = flush_interval.tick() => {
                    if !self.buffer.is_empty() && last_flush.elapsed().as_millis() as u64 >= interval_ms {
                        if !self.flush().await {
                            return;
                        }
                        last_flush = Instant::now();
                    }
                }
            }
        }
    }

    /// バッチサイズのみの受信ループ(フラッシュ間隔なし)
    async fn run_batch_only(&mut self) {
        loop {
            match self.rx.recv().await {
                Ok(event) => {
                    self.buffer.push(Record::from(&event));

                    if self.buffer.len() >= self.config.batch_size && !self.flush().await {
                        return;
                    }
                }
                Err(broadcast::error::RecvError::Closed) => {
                    tracing::debug!("ActionEvent channel closed");
                    return;
                }
                Err(broadcast::error::RecvError::Lagged(n)) => {
                    tracing::warn!(lagged = n, "ActionEventSubscriber lagged behind");
                }
            }
        }
    }

    /// バッファを送信
    ///
    /// Returns: 送信成功なら true、チャンネル閉鎖なら false
    async fn flush(&mut self) -> bool {
        if self.buffer.is_empty() {
            return true;
        }

        let records = std::mem::take(&mut self.buffer);
        let count = records.len();

        match self.record_tx.send(records).await {
            Ok(()) => {
                tracing::debug!(count, "Flushed ActionEvent records to LearningDaemon");
                true
            }
            Err(_) => {
                tracing::warn!("LearningDaemon channel closed");
                false
            }
        }
    }
}

// ============================================================================
// LearningEventSubscriber
// ============================================================================

/// LearningEvent を受信して LearningDaemon に Record を送信
///
/// `LearningEventChannel` から `LearningEvent` を受信し、
/// `Record` に変換してバッチで `LearningDaemon` に送信する。
///
/// ## 対応 Event
///
/// - `LearningEvent::StrategyAdvice` → `StrategyAdviceRecord`
/// - `LearningEvent::DependencyGraphInference` → `DependencyGraphRecord`
/// - `LearningEvent::LearnStatsSnapshot` → `LearnStatsRecord`
///
/// ## 使用例
///
/// ```ignore
/// use swarm_engine_core::events::LearningEventChannel;
/// use swarm_engine_core::learn::daemon::{LearningEventSubscriber, EventSubscriberConfig};
///
/// let channel = LearningEventChannel::global();
/// channel.enable();
///
/// let subscriber = LearningEventSubscriber::new(channel.subscribe(), record_tx);
/// tokio::spawn(subscriber.run());
/// ```
pub struct LearningEventSubscriber {
    /// LearningEvent 受信チャンネル
    rx: broadcast::Receiver<LearningEvent>,
    /// Record 送信チャンネル(LearningDaemon.record_sender())
    record_tx: mpsc::Sender<Vec<Record>>,
    /// 設定
    config: EventSubscriberConfig,
    /// バッファ
    buffer: Vec<Record>,
}

impl LearningEventSubscriber {
    /// 新しい LearningEventSubscriber を作成
    pub fn new(
        rx: broadcast::Receiver<LearningEvent>,
        record_tx: mpsc::Sender<Vec<Record>>,
    ) -> Self {
        Self::with_config(rx, record_tx, EventSubscriberConfig::default())
    }

    /// 設定を指定して LearningEventSubscriber を作成
    pub fn with_config(
        rx: broadcast::Receiver<LearningEvent>,
        record_tx: mpsc::Sender<Vec<Record>>,
        config: EventSubscriberConfig,
    ) -> Self {
        let batch_size = config.batch_size;
        Self {
            rx,
            record_tx,
            config,
            buffer: Vec::with_capacity(batch_size),
        }
    }

    /// 受信ループを開始
    ///
    /// `LearningEvent` を受信し、`Record` に変換してバッチ送信する。
    /// チャンネルが閉じられるか、送信先が閉じられたら終了。
    pub async fn run(mut self) {
        tracing::info!(
            batch_size = self.config.batch_size,
            flush_interval_ms = ?self.config.flush_interval_ms,
            "LearningEventSubscriber started"
        );

        if let Some(interval_ms) = self.config.flush_interval_ms {
            self.run_with_flush_interval(interval_ms).await;
        } else {
            self.run_batch_only().await;
        }

        // Flush remaining records
        self.flush().await;

        tracing::info!("LearningEventSubscriber stopped");
    }

    /// フラッシュ間隔ありの受信ループ
    async fn run_with_flush_interval(&mut self, interval_ms: u64) {
        use std::time::Duration;
        use tokio::time::{interval, Instant};

        let mut flush_interval = interval(Duration::from_millis(interval_ms));
        let mut last_flush = Instant::now();

        loop {
            tokio::select! {
                // LearningEvent 受信
                result = self.rx.recv() => {
                    match result {
                        Ok(event) => {
                            self.buffer.push(Record::from(&event));

                            if self.buffer.len() >= self.config.batch_size {
                                if !self.flush().await {
                                    return;
                                }
                                last_flush = Instant::now();
                            }
                        }
                        Err(broadcast::error::RecvError::Closed) => {
                            tracing::debug!("LearningEvent channel closed");
                            return;
                        }
                        Err(broadcast::error::RecvError::Lagged(n)) => {
                            tracing::warn!(lagged = n, "LearningEventSubscriber lagged behind");
                        }
                    }
                }

                // 定期フラッシュ
                _ = flush_interval.tick() => {
                    if !self.buffer.is_empty() && last_flush.elapsed().as_millis() as u64 >= interval_ms {
                        if !self.flush().await {
                            return;
                        }
                        last_flush = Instant::now();
                    }
                }
            }
        }
    }

    /// バッチサイズのみの受信ループ(フラッシュ間隔なし)
    async fn run_batch_only(&mut self) {
        loop {
            match self.rx.recv().await {
                Ok(event) => {
                    self.buffer.push(Record::from(&event));

                    if self.buffer.len() >= self.config.batch_size && !self.flush().await {
                        return;
                    }
                }
                Err(broadcast::error::RecvError::Closed) => {
                    tracing::debug!("LearningEvent channel closed");
                    return;
                }
                Err(broadcast::error::RecvError::Lagged(n)) => {
                    tracing::warn!(lagged = n, "LearningEventSubscriber lagged behind");
                }
            }
        }
    }

    /// バッファを送信
    ///
    /// Returns: 送信成功なら true、チャンネル閉鎖なら false
    async fn flush(&mut self) -> bool {
        if self.buffer.is_empty() {
            return true;
        }

        let records = std::mem::take(&mut self.buffer);
        let count = records.len();

        match self.record_tx.send(records).await {
            Ok(()) => {
                tracing::debug!(count, "Flushed LearningEvent records to LearningDaemon");
                true
            }
            Err(_) => {
                tracing::warn!("LearningDaemon channel closed");
                false
            }
        }
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::events::{ActionEventBuilder, ActionEventResult, LearningEvent};
    use crate::types::WorkerId;
    use std::time::Duration;

    fn make_action_event(tick: u64, action: &str) -> ActionEvent {
        ActionEventBuilder::new(tick, WorkerId(0), action)
            .result(ActionEventResult::success())
            .duration(Duration::from_millis(10))
            .build()
    }

    fn make_learning_event(model: &str) -> LearningEvent {
        LearningEvent::dependency_graph_inference(model)
            .prompt("test prompt")
            .response("test response")
            .discover_order(vec!["A".into(), "B".into()])
            .success()
            .build()
    }

    // ========================================================================
    // ActionEventSubscriber Tests
    // ========================================================================

    #[tokio::test]
    async fn test_action_subscriber_batch() {
        let (tx, rx) = broadcast::channel::<ActionEvent>(16);
        let (record_tx, mut record_rx) = mpsc::channel::<Vec<Record>>(16);

        let config = EventSubscriberConfig::new()
            .batch_size(3)
            .no_flush_interval();

        let subscriber = ActionEventSubscriber::with_config(rx, record_tx, config);

        let handle = tokio::spawn(async move {
            subscriber.run().await;
        });

        // Send 5 events (should trigger 1 batch of 3)
        for i in 0..5 {
            tx.send(make_action_event(i, &format!("Action{}", i)))
                .unwrap();
        }

        // Wait for batch
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Should receive batch of 3
        let batch = record_rx.try_recv().unwrap();
        assert_eq!(batch.len(), 3);

        // Drop sender to close channel
        drop(tx);

        // Wait for subscriber to finish
        let _ = handle.await;

        // Should receive remaining 2
        let batch = record_rx.try_recv().unwrap();
        assert_eq!(batch.len(), 2);
    }

    #[tokio::test]
    async fn test_action_subscriber_flush_interval() {
        let (tx, rx) = broadcast::channel::<ActionEvent>(16);
        let (record_tx, mut record_rx) = mpsc::channel::<Vec<Record>>(16);

        let config = EventSubscriberConfig::new()
            .batch_size(100) // Large batch size
            .flush_interval_ms(50); // Short flush interval

        let subscriber = ActionEventSubscriber::with_config(rx, record_tx, config);

        let handle = tokio::spawn(async move {
            subscriber.run().await;
        });

        // Send 2 events (won't reach batch size)
        tx.send(make_action_event(0, "Action0")).unwrap();
        tx.send(make_action_event(1, "Action1")).unwrap();

        // Wait for flush interval
        tokio::time::sleep(Duration::from_millis(100)).await;

        // Should receive batch due to flush interval
        let batch = record_rx.try_recv().unwrap();
        assert_eq!(batch.len(), 2);

        drop(tx);
        let _ = handle.await;
    }

    #[tokio::test]
    async fn test_action_subscriber_channel_closed() {
        let (tx, rx) = broadcast::channel::<ActionEvent>(16);
        let (record_tx, record_rx) = mpsc::channel::<Vec<Record>>(16);

        let config = EventSubscriberConfig::new()
            .batch_size(100)
            .no_flush_interval();

        let subscriber = ActionEventSubscriber::with_config(rx, record_tx, config);

        let handle = tokio::spawn(async move {
            subscriber.run().await;
        });

        // Send event
        tx.send(make_action_event(0, "Action0")).unwrap();

        // Drop receiver to close channel
        drop(record_rx);

        // Send more events
        tx.send(make_action_event(1, "Action1")).unwrap();

        // Subscriber should detect closed channel and stop
        tokio::time::sleep(Duration::from_millis(50)).await;

        drop(tx);
        let _ = handle.await;
    }

    // ========================================================================
    // LearningEventSubscriber Tests
    // ========================================================================

    #[tokio::test]
    async fn test_learning_subscriber_batch() {
        let (tx, rx) = broadcast::channel::<LearningEvent>(16);
        let (record_tx, mut record_rx) = mpsc::channel::<Vec<Record>>(16);

        let config = EventSubscriberConfig::new()
            .batch_size(2)
            .no_flush_interval();

        let subscriber = LearningEventSubscriber::with_config(rx, record_tx, config);

        let handle = tokio::spawn(async move {
            subscriber.run().await;
        });

        // Send 3 events (should trigger 1 batch of 2)
        for i in 0..3 {
            tx.send(make_learning_event(&format!("model{}", i)))
                .unwrap();
        }

        // Wait for batch
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Should receive batch of 2
        let batch = record_rx.try_recv().unwrap();
        assert_eq!(batch.len(), 2);

        // All records should be DependencyGraph
        for record in &batch {
            assert!(record.is_dependency_graph());
        }

        // Drop sender to close channel
        drop(tx);

        // Wait for subscriber to finish
        let _ = handle.await;

        // Should receive remaining 1
        let batch = record_rx.try_recv().unwrap();
        assert_eq!(batch.len(), 1);
    }

    #[tokio::test]
    async fn test_learning_subscriber_flush_interval() {
        let (tx, rx) = broadcast::channel::<LearningEvent>(16);
        let (record_tx, mut record_rx) = mpsc::channel::<Vec<Record>>(16);

        let config = EventSubscriberConfig::new()
            .batch_size(100) // Large batch size
            .flush_interval_ms(50); // Short flush interval

        let subscriber = LearningEventSubscriber::with_config(rx, record_tx, config);

        let handle = tokio::spawn(async move {
            subscriber.run().await;
        });

        // Send 1 event (won't reach batch size)
        tx.send(make_learning_event("model")).unwrap();

        // Wait for flush interval
        tokio::time::sleep(Duration::from_millis(100)).await;

        // Should receive batch due to flush interval
        let batch = record_rx.try_recv().unwrap();
        assert_eq!(batch.len(), 1);
        assert!(batch[0].is_dependency_graph());

        drop(tx);
        let _ = handle.await;
    }

    #[tokio::test]
    async fn test_learning_subscriber_converts_to_dependency_graph_record() {
        let (tx, rx) = broadcast::channel::<LearningEvent>(16);
        let (record_tx, mut record_rx) = mpsc::channel::<Vec<Record>>(16);

        let config = EventSubscriberConfig::new()
            .batch_size(1)
            .no_flush_interval();

        let subscriber = LearningEventSubscriber::with_config(rx, record_tx, config);

        let handle = tokio::spawn(async move {
            subscriber.run().await;
        });

        // Send DependencyGraphInference event
        tx.send(make_learning_event("test-model")).unwrap();

        // Wait for batch
        tokio::time::sleep(Duration::from_millis(50)).await;

        let batch = record_rx.try_recv().unwrap();
        assert_eq!(batch.len(), 1);

        // Verify it's a DependencyGraphRecord
        let record = batch[0].as_dependency_graph().unwrap();
        assert_eq!(record.model, "test-model");
        assert_eq!(record.prompt, "test prompt");
        assert_eq!(record.discover_order, vec!["A", "B"]);

        drop(tx);
        let _ = handle.await;
    }

    // ========================================================================
    // Config Tests
    // ========================================================================

    #[tokio::test]
    async fn test_subscriber_config() {
        let config = EventSubscriberConfig::new()
            .batch_size(50)
            .flush_interval_ms(500);

        assert_eq!(config.batch_size, 50);
        assert_eq!(config.flush_interval_ms, Some(500));

        let config2 = EventSubscriberConfig::new().no_flush_interval();
        assert_eq!(config2.flush_interval_ms, None);
    }
}