|
1 | | -//! Timer loop: read workspace/HEARTBEAT.md, push content as one-shot user message to agent. |
| 1 | +//! Timer loop: read workspace/HEARTBEAT.md, push one InboundMsg per task to agent. |
| 2 | +//! |
| 3 | +//! Each markdown bullet (`- `) in HEARTBEAT.md becomes its own agent run (one-shot, no session). |
| 4 | +//! Heartbeat pushes onto the same `inbound_tx` as Telegram and cron; the main loop branches on |
| 5 | +//! `channel == "heartbeat"` to call `process_heartbeat_message` instead of `process_message`. |
| 6 | +
|
| 7 | +use std::path::{Path, PathBuf}; |
| 8 | +use std::sync::atomic::{AtomicI64, Ordering}; |
| 9 | +use std::sync::Arc; |
| 10 | +use std::time::Duration; |
| 11 | + |
| 12 | +use tokio::sync::mpsc; |
| 13 | + |
| 14 | +use crate::telegram::InboundMsg; |
| 15 | + |
| 16 | +/// Parse markdown bullet tasks from HEARTBEAT.md content. |
| 17 | +/// |
| 18 | +/// Lines whose trimmed form starts with `"- "` are tasks; everything else is ignored. |
| 19 | +/// Inner whitespace around the task text is trimmed; blank tasks are dropped. |
| 20 | +pub fn parse_tasks(content: &str) -> Vec<String> { |
| 21 | + content |
| 22 | + .lines() |
| 23 | + .filter_map(|line| { |
| 24 | + line.trim() |
| 25 | + .strip_prefix("- ") |
| 26 | + .map(|task| task.trim().to_string()) |
| 27 | + }) |
| 28 | + .filter(|task| !task.is_empty()) |
| 29 | + .collect() |
| 30 | +} |
| 31 | + |
| 32 | +/// Read and parse tasks from `workspace/HEARTBEAT.md`. |
| 33 | +/// |
| 34 | +/// Returns an empty vec if the file does not exist or cannot be read. |
| 35 | +/// Sync I/O is fine: this is called at most once per N-minute tick. |
| 36 | +fn read_tasks(workspace: &Path) -> Vec<String> { |
| 37 | + let path = workspace.join("HEARTBEAT.md"); |
| 38 | + if !path.exists() { |
| 39 | + return vec![]; |
| 40 | + } |
| 41 | + let content = std::fs::read_to_string(&path).unwrap_or_default(); |
| 42 | + parse_tasks(&content) |
| 43 | +} |
| 44 | + |
| 45 | +/// Spawn the heartbeat runner. |
| 46 | +/// |
| 47 | +/// Every `interval_minutes` minutes: read `HEARTBEAT.md`, and for each task push one |
| 48 | +/// `InboundMsg { channel: "heartbeat" }` onto `inbound_tx`. The main loop will call |
| 49 | +/// `process_heartbeat_message` once per message — N agent calls per tick (N = tasks). |
| 50 | +/// |
| 51 | +/// `last_chat_id` is loaded on each tick to find the current active Telegram chat. |
| 52 | +/// If it is `0` (no user has messaged yet) the messages are still pushed; main.rs |
| 53 | +/// drops the reply in that case. |
| 54 | +/// |
| 55 | +/// # Panics |
| 56 | +/// Panics if `interval_minutes == 0` (caller must check before calling). |
| 57 | +pub fn spawn_heartbeat_runner( |
| 58 | + workspace: PathBuf, |
| 59 | + interval_minutes: u64, |
| 60 | + inbound_tx: mpsc::Sender<InboundMsg>, |
| 61 | + last_chat_id: Arc<AtomicI64>, |
| 62 | +) -> tokio::task::JoinHandle<()> { |
| 63 | + assert!(interval_minutes >= 1, "heartbeat interval_minutes must be >= 1"); |
| 64 | + tokio::spawn(async move { |
| 65 | + let mut interval = tokio::time::interval(Duration::from_secs(interval_minutes * 60)); |
| 66 | + // Skip the immediately-firing first tick so the first real tick is one full interval out. |
| 67 | + interval.tick().await; |
| 68 | + loop { |
| 69 | + interval.tick().await; |
| 70 | + let tasks = read_tasks(&workspace); |
| 71 | + if tasks.is_empty() { |
| 72 | + continue; |
| 73 | + } |
| 74 | + let chat_id = last_chat_id.load(Ordering::Relaxed); |
| 75 | + for task in tasks { |
| 76 | + let msg = InboundMsg { |
| 77 | + chat_id, |
| 78 | + user_id: 0, |
| 79 | + text: format!("[Heartbeat Task] {task}"), |
| 80 | + channel: "heartbeat".to_string(), |
| 81 | + }; |
| 82 | + if inbound_tx.send(msg).await.is_err() { |
| 83 | + // Receiver closed (main loop exited); nothing more to do. |
| 84 | + return; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +#[cfg(test)] |
| 92 | +mod tests { |
| 93 | + use super::*; |
| 94 | + |
| 95 | + // --- parse_tasks --- |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn parse_empty() { |
| 99 | + assert!(parse_tasks("").is_empty()); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn parse_single_bullet() { |
| 104 | + assert_eq!(parse_tasks("- Check weather"), ["Check weather"]); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn parse_multiple_bullets() { |
| 109 | + let tasks = parse_tasks("- First\n- Second\n- Third"); |
| 110 | + assert_eq!(tasks, ["First", "Second", "Third"]); |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn parse_skips_non_bullet_lines() { |
| 115 | + let content = "# Heartbeat\n\nProse.\n\n- Do thing\n<!-- comment -->\n- Another"; |
| 116 | + assert_eq!(parse_tasks(content), ["Do thing", "Another"]); |
| 117 | + } |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn parse_unicode_task() { |
| 121 | + assert_eq!(parse_tasks("- こんにちは 🦀"), ["こんにちは 🦀"]); |
| 122 | + } |
| 123 | + |
| 124 | + #[test] |
| 125 | + fn parse_strips_inner_whitespace() { |
| 126 | + assert_eq!(parse_tasks("- \t trim me \t"), ["trim me"]); |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn parse_skips_empty_bullets() { |
| 131 | + // A bare "- " with nothing after it is dropped. |
| 132 | + assert_eq!(parse_tasks("- \n- real task"), ["real task"]); |
| 133 | + } |
| 134 | + |
| 135 | + #[test] |
| 136 | + fn parse_mixed_indentation_ignored() { |
| 137 | + // Lines that do NOT start with "- " after trimming are skipped. |
| 138 | + let tasks = parse_tasks(" - indented\n- normal"); |
| 139 | + assert_eq!(tasks, ["indented", "normal"]); |
| 140 | + } |
| 141 | + |
| 142 | + // --- read_tasks --- |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn read_tasks_returns_empty_when_file_missing() { |
| 146 | + let dir = std::env::temp_dir().join("icrab_hb_no_file_test"); |
| 147 | + // Ensure no HEARTBEAT.md exists in this temp dir. |
| 148 | + let _ = std::fs::remove_file(dir.join("HEARTBEAT.md")); |
| 149 | + assert!(read_tasks(&dir).is_empty()); |
| 150 | + } |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn read_tasks_parses_file() { |
| 154 | + let dir = std::env::temp_dir().join("icrab_hb_read_test"); |
| 155 | + std::fs::create_dir_all(&dir).unwrap(); |
| 156 | + std::fs::write(dir.join("HEARTBEAT.md"), "- Alpha\n- Beta\n").unwrap(); |
| 157 | + let tasks = read_tasks(&dir); |
| 158 | + assert_eq!(tasks, ["Alpha", "Beta"]); |
| 159 | + let _ = std::fs::remove_dir_all(&dir); |
| 160 | + } |
| 161 | + |
| 162 | + // --- message format --- |
| 163 | + |
| 164 | + #[tokio::test] |
| 165 | + async fn messages_have_correct_format_and_channel() { |
| 166 | + use std::sync::atomic::AtomicI64; |
| 167 | + use tokio::sync::mpsc; |
| 168 | + |
| 169 | + let dir = std::env::temp_dir().join("icrab_hb_msg_fmt_test"); |
| 170 | + std::fs::create_dir_all(&dir).unwrap(); |
| 171 | + std::fs::write(dir.join("HEARTBEAT.md"), "- Task A\n- Task B\n").unwrap(); |
| 172 | + |
| 173 | + let last_chat_id = Arc::new(AtomicI64::new(42)); |
| 174 | + let tasks = read_tasks(&dir); |
| 175 | + assert_eq!(tasks.len(), 2); |
| 176 | + |
| 177 | + let (tx, mut rx) = mpsc::channel(8); |
| 178 | + let chat_id = last_chat_id.load(Ordering::Relaxed); |
| 179 | + for task in &tasks { |
| 180 | + tx.send(InboundMsg { |
| 181 | + chat_id, |
| 182 | + user_id: 0, |
| 183 | + text: format!("[Heartbeat Task] {task}"), |
| 184 | + channel: "heartbeat".to_string(), |
| 185 | + }) |
| 186 | + .await |
| 187 | + .unwrap(); |
| 188 | + } |
| 189 | + drop(tx); |
| 190 | + |
| 191 | + let a = rx.recv().await.unwrap(); |
| 192 | + assert_eq!(a.chat_id, 42); |
| 193 | + assert_eq!(a.text, "[Heartbeat Task] Task A"); |
| 194 | + assert_eq!(a.channel, "heartbeat"); |
| 195 | + assert_eq!(a.user_id, 0); |
| 196 | + |
| 197 | + let b = rx.recv().await.unwrap(); |
| 198 | + assert_eq!(b.text, "[Heartbeat Task] Task B"); |
| 199 | + |
| 200 | + let _ = std::fs::remove_dir_all(&dir); |
| 201 | + } |
| 202 | +} |
0 commit comments