Files
optimclaw/tests/e2e_routine_heartbeat.rs
T
df3635d6be feat(timezone): add timezone-aware session context (#671)
* feat(timezone): add timezone-aware session context (#661)

All timestamps were UTC-only, causing daily logs to split at UTC midnight,
cron schedules to fire in UTC, and no quiet hours for heartbeat. This adds
timezone as a per-session property flowing from the client.

Key changes:
- New `src/timezone.rs` module with resolution chain, parsing, and detection
- `IncomingMessage` carries optional timezone from client
- `JobContext.user_timezone` flows timezone to tools
- `next_cron_fire()` accepts timezone for schedule evaluation
- `Trigger::Cron` stores optional timezone (backward-compatible)
- Workspace gains `_tz` variants for daily logs and system prompt
- Heartbeat supports quiet hours (`HEARTBEAT_QUIET_START/END`)
- Web frontend sends `Intl.DateTimeFormat().resolvedOptions().timeZone`
- REPL auto-detects system timezone
- `DEFAULT_TIMEZONE` env var / settings for server-wide default

Storage stays UTC. Conversion happens at display boundaries.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix(timezone): address review feedback on timezone-aware sessions

- Validate quiet hours values (0-23) in HeartbeatConfig::resolve()
- Fall back to settings values when env vars are unset for quiet hours
- Validate IANA timezone strings in routine_create/update with parse_timezone
- Add timezone field to routine_create tool schema
- Allow standalone timezone update on cron routines without changing schedule
- Return path from append_daily_log_tz to avoid TOCTOU race at midnight
- Delegate append_daily_log to append_daily_log_tz(entry, UTC) to avoid drift
- Preserve timezone through approval flow via PendingApproval.user_timezone
- Improve test_today_in_tz to not depend on hardcoded year
- Add 3 regression tests for quiet hours config validation

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* style: fix formatting in routine.rs

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix(timezone): address second round of review feedback

- Remove .claude/scheduled_tasks.lock from repo and add to .gitignore
- Store resolved timezone (not raw message.timezone) in PendingApproval
- Carry forward user_timezone through chained approvals in thread_ops
- Wire quiet_hours_start/end from config to HeartbeatRunner
- Support X-Timezone header as fallback in chat_send_handler

[skip-regression-check]

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix(timezone): include user's local time in time tool response

The time tool's "now" operation now returns local_iso and timezone
fields based on ctx.user_timezone, so the LLM can report time in
the user's timezone instead of always UTC.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* style: fix formatting in time.rs

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix(timezone): address Copilot review round 3 — validation, deterministic tests, schema fixes

- Validate DEFAULT_TIMEZONE and HEARTBEAT_TIMEZONE at config load time
- Add timezone field to HeartbeatSettings and config::HeartbeatConfig
- Wire heartbeat timezone from config through agent_loop to HeartbeatRunner
- Add timezone to routine_update tool schema (was accepted but not advertised)
- Error on schedule/timezone update for non-cron routines
- Validate timezone in Trigger::from_db (coerce invalid to None with warning)
- Validate timezone in approval path (thread_ops.rs) before overwriting
- Time tool always includes timezone/local_iso fields (fallback to UTC)
- Make quiet hours tests deterministic using current UTC hour
- Add regression tests for config validation

[skip-regression-check]

Co-Authored-By: Claude Opus 4.6 <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
2026-03-08 08:01:56 +00:00

426 lines
15 KiB
Rust

//! E2E tests: routine engine and heartbeat (#575).
//!
//! These tests construct RoutineEngine and HeartbeatRunner directly
//! with a TraceLlm and libSQL database, bypassing the full TestRig.
#[cfg(feature = "libsql")]
mod support;
#[cfg(feature = "libsql")]
mod tests {
use std::sync::Arc;
use std::time::Duration;
use chrono::Utc;
use uuid::Uuid;
use ironclaw::agent::routine::{
NotifyConfig, Routine, RoutineAction, RoutineGuardrails, Trigger,
};
use ironclaw::agent::routine_engine::RoutineEngine;
use ironclaw::agent::{HeartbeatConfig, HeartbeatRunner};
use ironclaw::channels::IncomingMessage;
use ironclaw::config::{RoutineConfig, SafetyConfig};
use ironclaw::db::Database;
use ironclaw::safety::SafetyLayer;
use ironclaw::workspace::Workspace;
use ironclaw::workspace::hygiene::HygieneConfig;
use crate::support::trace_llm::{LlmTrace, TraceLlm, TraceResponse, TraceStep};
/// Create a temp libSQL database with migrations applied.
async fn create_test_db() -> (Arc<dyn Database>, tempfile::TempDir) {
use ironclaw::db::libsql::LibSqlBackend;
let temp_dir = tempfile::tempdir().expect("tempdir");
let db_path = temp_dir.path().join("test.db");
let backend = LibSqlBackend::new_local(&db_path)
.await
.expect("LibSqlBackend");
backend.run_migrations().await.expect("migrations");
let db: Arc<dyn Database> = Arc::new(backend);
(db, temp_dir)
}
/// Create a workspace backed by the test database.
fn create_workspace(db: &Arc<dyn Database>) -> Arc<Workspace> {
Arc::new(Workspace::new_with_db("default", db.clone()))
}
/// Helper to insert a routine directly into the database.
fn make_routine(name: &str, trigger: Trigger, prompt: &str) -> Routine {
Routine {
id: Uuid::new_v4(),
name: name.to_string(),
description: format!("Test routine: {name}"),
user_id: "default".to_string(),
enabled: true,
trigger,
action: RoutineAction::Lightweight {
prompt: prompt.to_string(),
context_paths: vec![],
max_tokens: 1000,
},
guardrails: RoutineGuardrails {
cooldown: Duration::from_secs(0),
max_concurrent: 5,
dedup_window: None,
},
notify: NotifyConfig::default(),
last_run_at: None,
next_fire_at: None,
run_count: 0,
consecutive_failures: 0,
state: serde_json::json!({}),
created_at: Utc::now(),
updated_at: Utc::now(),
}
}
// -----------------------------------------------------------------------
// Test 1: cron_routine_fires
// -----------------------------------------------------------------------
#[tokio::test]
async fn cron_routine_fires() {
let (db, _tmp) = create_test_db().await;
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 llm = Arc::new(TraceLlm::from_trace(trace));
let (notify_tx, mut notify_rx) = tokio::sync::mpsc::channel(16);
let engine = Arc::new(RoutineEngine::new(
RoutineConfig::default(),
db.clone(),
llm,
ws,
notify_tx,
None,
));
// 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;
// Give the spawned task time to execute.
tokio::time::sleep(Duration::from_millis(500)).await;
// Verify a run was recorded.
let runs = db
.list_routine_runs(routine.id, 10)
.await
.expect("list_routine_runs");
assert!(
!runs.is_empty(),
"Expected at least one routine run after cron trigger"
);
// Notification may or may not be sent depending on config;
// just verify no panic occurred. Drain the channel.
let _ = notify_rx.try_recv();
}
// -----------------------------------------------------------------------
// Test 2: event_trigger_matches
// -----------------------------------------------------------------------
#[tokio::test]
async fn event_trigger_matches() {
let (db, _tmp) = create_test_db().await;
let ws = create_workspace(&db);
let trace = LlmTrace::single_turn(
"test-event-match",
"deploy",
vec![TraceStep {
request_hint: None,
response: TraceResponse::Text {
content: "Deployment detected".to_string(),
input_tokens: 50,
output_tokens: 10,
},
expected_tool_results: vec![],
}],
);
let llm = Arc::new(TraceLlm::from_trace(trace));
let (notify_tx, _notify_rx) = tokio::sync::mpsc::channel(16);
let engine = Arc::new(RoutineEngine::new(
RoutineConfig::default(),
db.clone(),
llm,
ws,
notify_tx,
None,
));
// Insert an event routine matching "deploy.*production".
let routine = make_routine(
"deploy-watcher",
Trigger::Event {
channel: None,
pattern: "deploy.*production".to_string(),
},
"Report on deployment.",
);
db.create_routine(&routine).await.expect("create_routine");
// Refresh the event cache so the engine knows about the routine.
engine.refresh_event_cache().await;
// Positive match: message containing "deploy to production".
let matching_msg = IncomingMessage {
id: Uuid::new_v4(),
channel: "test".to_string(),
user_id: "default".to_string(),
user_name: None,
content: "deploy to production now".to_string(),
thread_id: None,
received_at: Utc::now(),
metadata: serde_json::json!({}),
timezone: None,
attachments: Vec::new(),
};
let fired = engine.check_event_triggers(&matching_msg).await;
assert!(
fired >= 1,
"Expected >= 1 routine fired on match, got {fired}"
);
// Give spawn time.
tokio::time::sleep(Duration::from_millis(500)).await;
// Negative match: message that doesn't match.
let non_matching_msg = IncomingMessage {
id: Uuid::new_v4(),
channel: "test".to_string(),
user_id: "default".to_string(),
user_name: None,
content: "check the staging environment".to_string(),
thread_id: None,
received_at: Utc::now(),
metadata: serde_json::json!({}),
timezone: None,
attachments: Vec::new(),
};
let fired_neg = engine.check_event_triggers(&non_matching_msg).await;
assert_eq!(fired_neg, 0, "Expected 0 routines fired on non-match");
}
// -----------------------------------------------------------------------
// Test 3: routine_cooldown
// -----------------------------------------------------------------------
#[tokio::test]
async fn routine_cooldown() {
let (db, _tmp) = create_test_db().await;
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 llm = Arc::new(TraceLlm::from_trace(trace));
let (notify_tx, _notify_rx) = tokio::sync::mpsc::channel(16);
let engine = Arc::new(RoutineEngine::new(
RoutineConfig::default(),
db.clone(),
llm,
ws,
notify_tx,
None,
));
// 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 = IncomingMessage {
id: Uuid::new_v4(),
channel: "test".to_string(),
user_id: "default".to_string(),
user_name: None,
content: "test-cooldown trigger".to_string(),
thread_id: None,
received_at: Utc::now(),
metadata: serde_json::json!({}),
timezone: None,
attachments: Vec::new(),
};
let fired1 = engine.check_event_triggers(&msg).await;
assert!(fired1 >= 1, "First fire should work");
// Give spawn time, then update last_run_at to simulate recent execution.
tokio::time::sleep(Duration::from_millis(300)).await;
// Update the routine's last_run_at to now (simulating it just ran).
db.update_routine_runtime(routine.id, Utc::now(), None, 1, 0, &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");
}
// -----------------------------------------------------------------------
// Test 4: heartbeat_findings
// -----------------------------------------------------------------------
#[tokio::test]
async fn heartbeat_findings() {
let (db, _tmp) = create_test_db().await;
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 safety = Arc::new(SafetyLayer::new(&SafetyConfig {
max_output_length: 100_000,
injection_check_enabled: false,
}));
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, safety)
.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();
}
// -----------------------------------------------------------------------
// Test 5: heartbeat_empty_skip
// -----------------------------------------------------------------------
#[tokio::test]
async fn heartbeat_empty_skip() {
let (db, _tmp) = create_test_db().await;
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 safety = Arc::new(SafetyLayer::new(&SafetyConfig {
max_output_length: 100_000,
injection_check_enabled: false,
}));
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, safety);
let result = runner.check_heartbeat().await;
assert!(
matches!(result, ironclaw::agent::HeartbeatResult::Skipped),
"Expected Skipped for empty checklist, got: {result:?}"
);
}
}