-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor: split routine_heartbeat.rs into focused submodules (#25) #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
leynos
merged 4 commits into
main
from
issue-25-split-routine-heartbeat-rs-into-multiple-modules
Apr 7, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2f7cc8a
Refactor: split routine_heartbeat.rs into focused submodules (#25)
c60ccd3
Refactor: address review feedback for routine_heartbeat split (#25)
fe542de
Refactor: address review feedback - polling loops and error propagati…
0406d7a
Docs: add routine test helpers documentation to testing-strategy.md (…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| //! E2E tests: heartbeat runner. | ||
| //! | ||
| //! Tests that the HeartbeatRunner correctly processes heartbeat checklists | ||
| //! and handles findings or skips appropriately. | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use ironclaw::agent::{HeartbeatConfig, HeartbeatRunner}; | ||
| use ironclaw::workspace::hygiene::HygieneConfig; | ||
|
|
||
| use crate::support::routines::{create_test_db, create_workspace}; | ||
| use crate::support::trace_llm::{LlmTrace, TraceLlm, TraceResponse, TraceStep}; | ||
|
|
||
| #[tokio::test] | ||
| async fn heartbeat_findings() { | ||
| let (db, _tmp) = create_test_db().await.expect("create_test_db"); | ||
| let ws = create_workspace(&db); | ||
|
|
||
| // Write a real heartbeat checklist. | ||
| ws.write( | ||
| "HEARTBEAT.md", | ||
| "# Heartbeat Checklist\n\n- [ ] Check if the server is running\n- [ ] Review error logs", | ||
| ) | ||
| .await | ||
| .expect("write heartbeat"); | ||
|
|
||
| // LLM responds with findings (not HEARTBEAT_OK). | ||
| let trace = LlmTrace::single_turn( | ||
| "test-heartbeat-findings", | ||
| "heartbeat", | ||
| vec![TraceStep { | ||
| request_hint: None, | ||
| response: TraceResponse::Text { | ||
| content: "The server has elevated error rates. Review the logs immediately." | ||
| .to_string(), | ||
| input_tokens: 100, | ||
| output_tokens: 20, | ||
| }, | ||
| expected_tool_results: vec![], | ||
| }], | ||
| ); | ||
| let llm = Arc::new(TraceLlm::from_trace(trace)); | ||
|
|
||
| let (tx, mut rx) = tokio::sync::mpsc::channel(16); | ||
|
|
||
| let hygiene_config = HygieneConfig { | ||
| enabled: false, | ||
| daily_retention_days: 30, | ||
| conversation_retention_days: 7, | ||
| cadence_hours: 24, | ||
| state_dir: _tmp.path().to_path_buf(), | ||
| }; | ||
|
|
||
| let runner = HeartbeatRunner::new(HeartbeatConfig::default(), hygiene_config, ws, llm) | ||
| .with_response_channel(tx); | ||
|
|
||
| let result = runner.check_heartbeat().await; | ||
| match result { | ||
| ironclaw::agent::HeartbeatResult::NeedsAttention(msg) => { | ||
| assert!( | ||
| msg.contains("error"), | ||
| "Expected 'error' in attention message: {msg}" | ||
| ); | ||
| } | ||
| other => panic!("Expected NeedsAttention, got: {other:?}"), | ||
| } | ||
|
|
||
| // No notification since we called check_heartbeat directly (not run). | ||
| let _ = rx.try_recv(); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn heartbeat_empty_skip() { | ||
| let (db, _tmp) = create_test_db().await.expect("create_test_db"); | ||
| let ws = create_workspace(&db); | ||
|
|
||
| // Write an effectively empty heartbeat (just headers and comments). | ||
| ws.write( | ||
| "HEARTBEAT.md", | ||
| "# Heartbeat Checklist\n\n<!-- No tasks yet -->\n", | ||
| ) | ||
| .await | ||
| .expect("write heartbeat"); | ||
|
|
||
| // LLM should NOT be called, so provide a trace that would panic if called. | ||
| let trace = LlmTrace::single_turn("test-heartbeat-skip", "skip", vec![]); | ||
| let llm = Arc::new(TraceLlm::from_trace(trace)); | ||
|
|
||
| let hygiene_config = HygieneConfig { | ||
| enabled: false, | ||
| daily_retention_days: 30, | ||
| conversation_retention_days: 7, | ||
| cadence_hours: 24, | ||
| state_dir: _tmp.path().to_path_buf(), | ||
| }; | ||
|
|
||
| let runner = HeartbeatRunner::new(HeartbeatConfig::default(), hygiene_config, ws, llm); | ||
|
|
||
| let result = runner.check_heartbeat().await; | ||
| assert!( | ||
| matches!(result, ironclaw::agent::HeartbeatResult::Skipped), | ||
| "Expected Skipped for empty checklist, got: {result:?}" | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| //! E2E tests: routine cooldown behaviour. | ||
| //! | ||
| //! Tests that routines respect their configured cooldown period and | ||
| //! prevent re-triggering within the cooldown window. | ||
|
|
||
| use std::time::Duration; | ||
|
|
||
| use chrono::Utc; | ||
|
|
||
| use ironclaw::agent::routine::Trigger; | ||
| use ironclaw::db::RoutineRuntimeUpdate; | ||
|
|
||
| use crate::support::routines::{ | ||
| create_test_db, create_workspace, make_minimal_engine, make_routine, make_test_incoming_message, | ||
| }; | ||
| use crate::support::trace_llm::{LlmTrace, TraceResponse, TraceStep}; | ||
|
|
||
| #[tokio::test] | ||
| async fn routine_cooldown() { | ||
| let (db, _tmp) = create_test_db().await.expect("create_test_db"); | ||
| let ws = create_workspace(&db); | ||
|
|
||
| // Need two LLM responses (one for the first fire). | ||
| let trace = LlmTrace::single_turn( | ||
| "test-cooldown", | ||
| "check", | ||
| vec![TraceStep { | ||
| request_hint: None, | ||
| response: TraceResponse::Text { | ||
| content: "ROUTINE_OK".to_string(), | ||
| input_tokens: 50, | ||
| output_tokens: 5, | ||
| }, | ||
| expected_tool_results: vec![], | ||
| }], | ||
| ); | ||
| let (engine, _notify_rx) = make_minimal_engine(trace, db.clone(), ws); | ||
|
|
||
| // Insert an event routine with 1-hour cooldown. | ||
| let mut routine = make_routine( | ||
| "cooldown-test", | ||
| Trigger::Event { | ||
| channel: None, | ||
| pattern: "test-cooldown".to_string(), | ||
| }, | ||
| "Check status.", | ||
| ); | ||
| routine.guardrails.cooldown = Duration::from_secs(3600); | ||
| db.create_routine(&routine).await.expect("create_routine"); | ||
| engine.refresh_event_cache().await; | ||
|
|
||
| // First fire should work. | ||
| let msg = make_test_incoming_message("test-cooldown trigger"); | ||
| let fired1 = engine.check_event_triggers(&msg).await; | ||
| assert!(fired1 >= 1, "First fire should work"); | ||
|
|
||
| // Poll for routine completion with timeout before updating last_run_at. | ||
| let mut attempts = 0; | ||
| let max_attempts = 50; | ||
| loop { | ||
| let runs = db | ||
| .list_routine_runs(routine.id, 10) | ||
| .await | ||
| .expect("list_routine_runs"); | ||
| if !runs.is_empty() { | ||
| break; | ||
| } | ||
| attempts += 1; | ||
| assert!( | ||
| attempts < max_attempts, | ||
| "Routine did not complete within timeout" | ||
| ); | ||
| tokio::time::sleep(Duration::from_millis(10)).await; | ||
| } | ||
|
|
||
| // Update the routine's last_run_at to now (simulating it just ran). | ||
| db.update_routine_runtime(RoutineRuntimeUpdate { | ||
| id: routine.id, | ||
| last_run_at: Utc::now(), | ||
| next_fire_at: None, | ||
| run_count: 1, | ||
| consecutive_failures: 0, | ||
| state: &serde_json::json!({}), | ||
| }) | ||
| .await | ||
| .expect("update_routine_runtime"); | ||
|
|
||
| // Refresh cache to pick up updated last_run_at. | ||
| engine.refresh_event_cache().await; | ||
|
|
||
| // Second fire should be blocked by cooldown. | ||
| let fired2 = engine.check_event_triggers(&msg).await; | ||
| assert_eq!(fired2, 0, "Second fire should be blocked by cooldown"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| //! E2E tests: cron-triggered routines. | ||
| //! | ||
| //! Tests that routines with cron schedules fire correctly when their | ||
| //! next_fire_at time is in the past. | ||
|
|
||
| use std::time::Duration; | ||
|
|
||
| use chrono::Utc; | ||
|
|
||
| use ironclaw::agent::routine::Trigger; | ||
|
|
||
| use crate::support::routines::{ | ||
| create_test_db, create_workspace, make_minimal_engine, make_routine, | ||
| }; | ||
| use crate::support::trace_llm::{LlmTrace, TraceResponse, TraceStep}; | ||
|
|
||
| #[tokio::test] | ||
| async fn cron_routine_fires() { | ||
| let (db, _tmp) = create_test_db().await.expect("create_test_db"); | ||
| let ws = create_workspace(&db); | ||
|
|
||
| // Create a TraceLlm that responds with ROUTINE_OK. | ||
| let trace = LlmTrace::single_turn( | ||
| "test-cron-fire", | ||
| "check", | ||
| vec![TraceStep { | ||
| request_hint: None, | ||
| response: TraceResponse::Text { | ||
| content: "ROUTINE_OK".to_string(), | ||
| input_tokens: 50, | ||
| output_tokens: 5, | ||
| }, | ||
| expected_tool_results: vec![], | ||
| }], | ||
| ); | ||
| let (engine, mut notify_rx) = make_minimal_engine(trace, db.clone(), ws); | ||
|
|
||
| // Insert a cron routine with next_fire_at in the past. | ||
| let mut routine = make_routine( | ||
| "cron-test", | ||
| Trigger::Cron { | ||
| schedule: "* * * * *".to_string(), | ||
| timezone: None, | ||
| }, | ||
| "Check system status.", | ||
| ); | ||
| routine.next_fire_at = Some(Utc::now() - chrono::Duration::minutes(5)); | ||
| db.create_routine(&routine).await.expect("create_routine"); | ||
|
|
||
| // Fire cron triggers. | ||
| engine.check_cron_triggers().await; | ||
|
|
||
| // Poll for routine completion with timeout. | ||
| let mut attempts = 0; | ||
| let max_attempts = 50; | ||
| loop { | ||
| let runs = db | ||
| .list_routine_runs(routine.id, 10) | ||
| .await | ||
| .expect("list_routine_runs"); | ||
| if !runs.is_empty() { | ||
| break; | ||
| } | ||
| attempts += 1; | ||
| assert!( | ||
| attempts < max_attempts, | ||
| "Routine did not complete within timeout" | ||
| ); | ||
| tokio::time::sleep(Duration::from_millis(10)).await; | ||
| } | ||
|
|
||
| // Notification may or may not be sent depending on config; | ||
| // just verify no panic occurred. Drain the channel. | ||
| let _ = notify_rx.try_recv(); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.