phago-runtime 1.0.0

Colony management, scheduling, and runtime for Phago biological computing
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
//! Streaming document ingestion for Phago Colony.
//!
//! This module provides real-time document processing as they arrive,
//! with support for file watchers, async streams, and backpressure handling.
//!
//! # Feature Flag
//!
//! This module requires the `streaming` feature:
//! ```toml
//! phago-runtime = { version = "0.5", features = ["streaming"] }
//! ```
//!
//! # Architecture
//!
//! ```text
//! ┌──────────────┐    ┌─────────────────┐    ┌─────────────────┐
//! │ File Watcher │───>│ Bounded Channel │───>│ StreamingColony │
//! └──────────────┘    └─────────────────┘    └─────────────────┘
//!        │                    ▲                       │
//!        │            Backpressure                    ▼
//!        │                    │              ┌─────────────────┐
//!        └────────────────────┘              │  Colony.tick()  │
//!                                            └─────────────────┘
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! use phago_runtime::streaming::{StreamingColony, FileWatcher};
//! use phago_runtime::prelude::*;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let colony = Colony::new();
//!     let streaming = StreamingColony::new(colony, StreamingConfig::default());
//!
//!     // Watch a directory for new documents
//!     let watcher = FileWatcher::new("./documents")?;
//!     streaming.start_watching(watcher).await?;
//!
//!     // Process documents as they arrive
//!     streaming.run_until_idle().await;
//!     Ok(())
//! }
//! ```

#![cfg(feature = "streaming")]

use crate::colony::{Colony, ColonyEvent};
use phago_agents::digester::Digester;
use phago_core::types::{DocumentId, Position};
use std::cell::RefCell;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::mpsc;
use std::time::Duration;
use tokio::sync::broadcast;

/// A document to be ingested into the colony.
#[derive(Debug, Clone)]
pub struct IngestDocument {
    /// Document title or filename.
    pub title: String,
    /// Document content.
    pub content: String,
    /// Position in 2D space (optional, defaults to auto-layout).
    pub position: Option<Position>,
    /// Source path if from file system.
    pub source_path: Option<PathBuf>,
}

impl IngestDocument {
    /// Create a new document for ingestion.
    pub fn new(title: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            content: content.into(),
            position: None,
            source_path: None,
        }
    }

    /// Set the position for this document.
    pub fn with_position(mut self, x: f64, y: f64) -> Self {
        self.position = Some(Position::new(x, y));
        self
    }

    /// Set the source path for this document.
    pub fn with_source(mut self, path: impl Into<PathBuf>) -> Self {
        self.source_path = Some(path.into());
        self
    }
}

/// Configuration for streaming ingestion.
#[derive(Debug, Clone)]
pub struct StreamingConfig {
    /// Maximum documents in the queue before backpressure kicks in.
    pub queue_capacity: usize,
    /// Ticks to run after each document ingestion.
    pub ticks_per_document: u64,
    /// Whether to run continuous background ticks.
    pub background_ticks: bool,
    /// Interval between background ticks (ms).
    pub tick_interval_ms: u64,
    /// Auto-layout spacing for documents without explicit position.
    pub auto_layout_spacing: f64,
}

impl Default for StreamingConfig {
    fn default() -> Self {
        Self {
            queue_capacity: 100,
            ticks_per_document: 10,
            background_ticks: true,
            tick_interval_ms: 100,
            auto_layout_spacing: 5.0,
        }
    }
}

/// Metrics for the streaming ingestion system.
#[derive(Debug, Clone, Default)]
pub struct StreamingMetrics {
    /// Total documents received.
    pub documents_received: u64,
    /// Documents successfully ingested.
    pub documents_ingested: u64,
    /// Documents dropped due to backpressure.
    pub documents_dropped: u64,
    /// Current queue depth.
    pub queue_depth: usize,
    /// Total ticks processed.
    pub ticks_processed: u64,
}

/// A streaming colony that can consume document streams.
///
/// This wraps a Colony and adds streaming ingestion capabilities
/// with backpressure handling and metrics.
pub struct StreamingColony {
    colony: Rc<RefCell<Colony>>,
    config: StreamingConfig,
    metrics: Rc<RefCell<StreamingMetrics>>,
    document_count: Rc<RefCell<u64>>,
    event_tx: broadcast::Sender<ColonyEvent>,
}

impl StreamingColony {
    /// Create a new StreamingColony.
    pub fn new(colony: Colony, config: StreamingConfig) -> Self {
        let (event_tx, _) = broadcast::channel(256);
        Self {
            colony: Rc::new(RefCell::new(colony)),
            config,
            metrics: Rc::new(RefCell::new(StreamingMetrics::default())),
            document_count: Rc::new(RefCell::new(0)),
            event_tx,
        }
    }

    /// Get a receiver for colony events.
    pub fn subscribe_events(&self) -> broadcast::Receiver<ColonyEvent> {
        self.event_tx.subscribe()
    }

    /// Get current metrics.
    pub fn metrics(&self) -> StreamingMetrics {
        self.metrics.borrow().clone()
    }

    /// Ingest a single document immediately.
    pub fn ingest(&self, doc: IngestDocument) -> DocumentId {
        let position = doc.position.unwrap_or_else(|| {
            let count = *self.document_count.borrow();
            *self.document_count.borrow_mut() += 1;
            Position::new(count as f64 * self.config.auto_layout_spacing, 0.0)
        });

        let doc_id = self.colony.borrow_mut().ingest_document(
            &doc.title,
            &doc.content,
            position,
        );

        // Spawn a digester for the document
        self.colony.borrow_mut().spawn(Box::new(
            Digester::new(position).with_max_idle(30),
        ));

        // Update metrics
        {
            let mut metrics = self.metrics.borrow_mut();
            metrics.documents_received += 1;
            metrics.documents_ingested += 1;
        }

        // Run post-ingestion ticks
        self.run_ticks(self.config.ticks_per_document);

        doc_id
    }

    /// Ingest a document asynchronously, respecting backpressure.
    pub async fn ingest_async(&self, doc: IngestDocument) -> DocumentId {
        // Yield to allow other tasks to run
        tokio::task::yield_now().await;
        self.ingest(doc)
    }

    /// Run N ticks and broadcast events.
    pub fn run_ticks(&self, ticks: u64) {
        for _ in 0..ticks {
            let events = self.colony.borrow_mut().tick();
            self.metrics.borrow_mut().ticks_processed += 1;
            for event in events {
                let _ = self.event_tx.send(event);
            }
        }
    }

    /// Run ticks asynchronously with controlled rate.
    pub async fn run_ticks_async(&self, ticks: u64) {
        let interval = Duration::from_millis(self.config.tick_interval_ms);
        for _ in 0..ticks {
            let events = self.colony.borrow_mut().tick();
            self.metrics.borrow_mut().ticks_processed += 1;
            for event in events {
                let _ = self.event_tx.send(event);
            }
            tokio::time::sleep(interval).await;
        }
    }

    /// Process documents from a channel until it's empty.
    pub async fn process_channel(
        &self,
        mut rx: tokio::sync::mpsc::Receiver<IngestDocument>,
    ) {
        while let Some(doc) = rx.recv().await {
            self.ingest_async(doc).await;
        }
    }

    /// Get the inner colony for direct access.
    pub fn colony(&self) -> &Rc<RefCell<Colony>> {
        &self.colony
    }

    /// Take ownership of the inner colony.
    pub fn into_colony(self) -> Colony {
        match Rc::try_unwrap(self.colony) {
            Ok(cell) => cell.into_inner(),
            Err(_) => panic!("Cannot unwrap StreamingColony: other references exist"),
        }
    }
}

/// Result of a file watch operation.
#[derive(Debug)]
pub enum WatchEvent {
    /// A new file was created or modified.
    FileChanged(PathBuf),
    /// A file was removed.
    FileRemoved(PathBuf),
    /// An error occurred.
    Error(String),
}

/// File watcher for monitoring directories for new documents.
pub struct FileWatcher {
    path: PathBuf,
    extensions: Vec<String>,
    rx: mpsc::Receiver<WatchEvent>,
    _watcher: notify::RecommendedWatcher,
}

impl FileWatcher {
    /// Create a new file watcher for the given path.
    ///
    /// By default, watches for .txt, .md, and .json files.
    pub fn new<P: AsRef<Path>>(path: P) -> Result<Self, String> {
        Self::with_extensions(path, vec!["txt", "md", "json"])
    }

    /// Create a file watcher with custom extensions.
    pub fn with_extensions<P: AsRef<Path>>(
        path: P,
        extensions: Vec<&str>,
    ) -> Result<Self, String> {
        use notify::{RecursiveMode, Watcher};

        let path = path.as_ref().to_path_buf();
        let extensions: Vec<String> = extensions.into_iter().map(|s| s.to_string()).collect();

        let (tx, rx) = mpsc::channel();
        let ext_clone = extensions.clone();

        let mut watcher = notify::recommended_watcher(move |res: Result<notify::Event, notify::Error>| {
            match res {
                Ok(event) => {
                    for path in event.paths {
                        // Filter by extension
                        if let Some(ext) = path.extension() {
                            let ext_str = ext.to_string_lossy().to_string();
                            if ext_clone.contains(&ext_str) {
                                match event.kind {
                                    notify::EventKind::Create(_) |
                                    notify::EventKind::Modify(_) => {
                                        let _ = tx.send(WatchEvent::FileChanged(path.clone()));
                                    }
                                    notify::EventKind::Remove(_) => {
                                        let _ = tx.send(WatchEvent::FileRemoved(path.clone()));
                                    }
                                    _ => {}
                                }
                            }
                        }
                    }
                }
                Err(e) => {
                    let _ = tx.send(WatchEvent::Error(e.to_string()));
                }
            }
        }).map_err(|e| format!("Failed to create watcher: {}", e))?;

        watcher.watch(&path, RecursiveMode::Recursive)
            .map_err(|e| format!("Failed to watch path: {}", e))?;

        Ok(Self {
            path,
            extensions,
            rx,
            _watcher: watcher,
        })
    }

    /// Get the path being watched.
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Get the extensions being watched.
    pub fn extensions(&self) -> &[String] {
        &self.extensions
    }

    /// Try to receive a watch event without blocking.
    pub fn try_recv(&self) -> Option<WatchEvent> {
        self.rx.try_recv().ok()
    }

    /// Receive a watch event, blocking until one is available.
    pub fn recv(&self) -> Option<WatchEvent> {
        self.rx.recv().ok()
    }

    /// Receive a watch event with a timeout.
    pub fn recv_timeout(&self, timeout: Duration) -> Option<WatchEvent> {
        self.rx.recv_timeout(timeout).ok()
    }
}

/// A document stream that can be consumed by StreamingColony.
pub struct DocumentChannel {
    tx: tokio::sync::mpsc::Sender<IngestDocument>,
    rx: Option<tokio::sync::mpsc::Receiver<IngestDocument>>,
    capacity: usize,
}

impl DocumentChannel {
    /// Create a new bounded document channel.
    pub fn new(capacity: usize) -> Self {
        let (tx, rx) = tokio::sync::mpsc::channel(capacity);
        Self {
            tx,
            rx: Some(rx),
            capacity,
        }
    }

    /// Get a sender for this channel.
    pub fn sender(&self) -> tokio::sync::mpsc::Sender<IngestDocument> {
        self.tx.clone()
    }

    /// Take the receiver (can only be done once).
    pub fn take_receiver(&mut self) -> Option<tokio::sync::mpsc::Receiver<IngestDocument>> {
        self.rx.take()
    }

    /// Get the capacity of this channel.
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Send a document to the channel.
    ///
    /// Returns false if the channel is full (backpressure).
    pub fn try_send(&self, doc: IngestDocument) -> bool {
        self.tx.try_send(doc).is_ok()
    }

    /// Send a document, waiting if the channel is full.
    pub async fn send(&self, doc: IngestDocument) -> Result<(), String> {
        self.tx.send(doc).await
            .map_err(|e| format!("Channel closed: {}", e))
    }
}

/// Watch a directory and stream documents to a channel.
///
/// This function runs in a background thread and reads files as they appear,
/// sending them to the provided channel.
pub fn watch_directory_to_channel(
    watcher: FileWatcher,
    channel: DocumentChannel,
) -> std::thread::JoinHandle<()> {
    let tx = channel.sender();

    std::thread::spawn(move || {
        loop {
            match watcher.recv() {
                Some(WatchEvent::FileChanged(path)) => {
                    // Read the file content
                    if let Ok(content) = std::fs::read_to_string(&path) {
                        let title = path.file_name()
                            .map(|s| s.to_string_lossy().to_string())
                            .unwrap_or_else(|| "Untitled".to_string());

                        let doc = IngestDocument::new(title, content)
                            .with_source(path);

                        // Try to send, drop if channel is full (backpressure)
                        if tx.blocking_send(doc).is_err() {
                            // Channel closed, exit
                            break;
                        }
                    }
                }
                Some(WatchEvent::FileRemoved(_)) => {
                    // Could track removed files if needed
                }
                Some(WatchEvent::Error(e)) => {
                    eprintln!("File watcher error: {}", e);
                }
                None => {
                    // Channel closed
                    break;
                }
            }
        }
    })
}

/// Convenience function to create a file-watching streaming colony.
///
/// Returns the StreamingColony and a handle to the watcher thread.
pub fn streaming_from_directory<P: AsRef<Path>>(
    colony: Colony,
    path: P,
    config: StreamingConfig,
) -> Result<(StreamingColony, std::thread::JoinHandle<()>), String> {
    let watcher = FileWatcher::new(path)?;
    let mut channel = DocumentChannel::new(config.queue_capacity);
    let _rx = channel.take_receiver().ok_or("Channel receiver already taken")?;

    let streaming = StreamingColony::new(colony, config);

    // Start the watcher thread
    let handle = watch_directory_to_channel(watcher, channel);

    // Note: The caller needs to call streaming.process_channel(rx) to consume documents

    Ok((streaming, handle))
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    use std::fs;

    #[test]
    fn ingest_document_basic() {
        let colony = Colony::new();
        let streaming = StreamingColony::new(colony, StreamingConfig::default());

        let doc = IngestDocument::new("Test", "Content about cells");
        let _id = streaming.ingest(doc);

        let metrics = streaming.metrics();
        assert_eq!(metrics.documents_ingested, 1);
        assert!(metrics.ticks_processed > 0);
    }

    #[test]
    fn auto_layout_positions() {
        let mut config = StreamingConfig::default();
        config.auto_layout_spacing = 10.0;

        let colony = Colony::new();
        let streaming = StreamingColony::new(colony, config);

        // Ingest multiple documents
        for i in 0..5 {
            let doc = IngestDocument::new(format!("Doc {}", i), "Content");
            streaming.ingest(doc);
        }

        let metrics = streaming.metrics();
        assert_eq!(metrics.documents_ingested, 5);
    }

    #[test]
    fn document_channel_backpressure() {
        let channel = DocumentChannel::new(2);

        // Should succeed
        assert!(channel.try_send(IngestDocument::new("1", "c")));
        assert!(channel.try_send(IngestDocument::new("2", "c")));

        // Should fail (channel full)
        assert!(!channel.try_send(IngestDocument::new("3", "c")));
    }

    #[tokio::test]
    async fn async_channel_send() {
        let channel = DocumentChannel::new(10);
        let tx = channel.sender();

        tx.send(IngestDocument::new("Test", "Content")).await.unwrap();
    }

    #[test]
    fn file_watcher_creation() {
        let temp_dir = TempDir::new().unwrap();
        let watcher = FileWatcher::new(temp_dir.path());
        assert!(watcher.is_ok());
    }

    #[test]
    fn file_watcher_detects_new_file() {
        let temp_dir = TempDir::new().unwrap();
        let watcher = FileWatcher::new(temp_dir.path()).unwrap();

        // Create a file
        let file_path = temp_dir.path().join("test.txt");
        fs::write(&file_path, "Hello, world!").unwrap();

        // Wait a bit and check for event
        std::thread::sleep(Duration::from_millis(100));

        // Try to receive (may or may not have event depending on OS timing)
        let _event = watcher.try_recv();
        // Note: Event detection timing varies by OS, so we just verify no panic
    }
}