Skip to main content

rbp_autotrain/
trainer.rs

1//! Session trait - unified training abstraction
2use std::sync::Arc;
3use tokio_postgres::Client;
4
5/// Unified training session interface.
6/// Both fast and slow modes implement this for polymorphic training loops.
7#[async_trait::async_trait]
8pub trait Trainer: Send + Sync + Sized {
9    /// Database client for persistence operations.
10    fn client(&self) -> &Arc<Client>;
11    /// Sync in-memory state to database on graceful exit.
12    async fn sync(self);
13    /// Run one training iteration.
14    async fn step(&mut self);
15    /// Get current epoch count.
16    async fn epoch(&self) -> usize;
17    /// Get final summary on completion.
18    async fn summary(&self) -> String;
19    /// Get training statistics if checkpoint interval has elapsed.
20    async fn checkpoint(&self) -> Option<String>;
21
22    async fn train(mut self) {
23        log::info!("training blueprint");
24        log::info!("press 'Q + ↵' to stop gracefully");
25        loop {
26            self.step().await;
27            self.checkpoint().await.map(|s| log::info!("{}", s));
28            if rbp_core::interrupted() {
29                log::info!("{}", self.summary().await);
30                break;
31            }
32        }
33        self.sync().await;
34    }
35}