mirror of
https://github.com/outbackdingo/optimclaw.git
synced 2026-08-25 14:53:34 +00:00
* feat(db): add list_dispatched_routine_runs to RoutineStore trait Add method to query routine runs with status='running' AND job_id IS NOT NULL, enabling the routine engine to sync completion status from background jobs. Implements for both PostgreSQL and libSQL backends. [skip-regression-check] Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix(routines): sync dispatched full-job runs with background job status (#697) Full-job routines were immediately marked Ok on dispatch, so failures/completions were never reflected in the routine run record. Now dispatch returns Running status, and a periodic sync checks linked jobs to update the run when the job completes, fails, or is cancelled. Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix(routines): fail fast when sandbox unavailable at dispatch time (#697) Thread sandbox_available bool from Docker detection through AgentDeps to RoutineEngine. Full-job routines now fail immediately with a clear error message when sandbox is enabled but Docker is not available, instead of dispatching a job that silently fails. Co-Authored-By: Claude Opus 4.6 <[email protected]> * feat(startup): notify user when sandbox unavailable (#697) When sandbox is enabled but Docker is not installed or not running, send a user-visible warning through all channels at startup (with a 2s delay to let channels connect). Previously this was only logged via tracing::warn, invisible to TUI/web users. Co-Authored-By: Claude Opus 4.6 <[email protected]> * style: fix formatting in routine_engine.rs Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix(tests): set sandbox_available=true in test rig for full_job traces Test rig doesn't use real Docker — full_job routines execute via trace replay. Setting sandbox_available=true allows the routine_news_digest trace test to dispatch full_job routines as before. Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix(routines): address review feedback on sync_dispatched_runs (#697) - Sanitize last_reason from job transitions before using in notifications (truncate to 500 chars, strip control characters) - Treat Submitted as in-progress (can still transition to Failed), only Completed and Accepted are terminal success states - Add test for sanitize_summary Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix(tests): add missing sandbox_available field to test constructors Staging added sandbox_available to AgentDeps and RoutineEngine::new. Add the missing field/argument in test files to fix CI compilation. Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix: sanitize job reason in notifications, fix state handling for Submitted/Accepted - Enhance sanitize_summary to strip HTML tags and collapse whitespace, preventing injection via untrusted container job reasons - Use char-boundary-safe truncation to avoid panics on multi-byte strings - Treat Submitted and Accepted as in-progress states (continue polling) rather than terminal success, since they can still transition to Failed - Increase channel-connect delay from 2s to 5s and add debug log for sandbox-unavailable warning delivery Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> * Replace sandbox_available bool with SandboxReadiness enum Distinguishes DisabledByConfig from DockerUnavailable so full-job routine errors give actionable guidance instead of a generic message. Co-Authored-By: Claude Opus 4.6 <[email protected]> * ci: re-trigger CI with latest changes Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix: add missing owner_id arg to send_notification call Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix: update e2e tests to use SandboxReadiness enum Co-Authored-By: Claude Opus 4.6 <[email protected]> --------- Co-authored-by: Claude Opus 4.6 <[email protected]> Co-authored-by: [email protected] <[email protected]>
356 lines
12 KiB
Rust
356 lines
12 KiB
Rust
//! E2E tests for Telegram message routing through the real agent + message tool.
|
|
|
|
#[cfg(feature = "libsql")]
|
|
mod support;
|
|
|
|
#[cfg(feature = "libsql")]
|
|
mod tests {
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use async_trait::async_trait;
|
|
use futures::StreamExt;
|
|
use ironclaw::agent::{Agent, AgentDeps};
|
|
use ironclaw::app::{AppBuilder, AppBuilderFlags};
|
|
use ironclaw::channels::web::log_layer::LogBroadcaster;
|
|
use ironclaw::channels::{
|
|
Channel, ChannelManager, IncomingMessage, MessageStream, OutgoingResponse, StatusUpdate,
|
|
};
|
|
use ironclaw::config::Config;
|
|
use ironclaw::db::{Database, libsql::LibSqlBackend};
|
|
use ironclaw::error::ChannelError;
|
|
use ironclaw::llm::{LlmProvider, SessionConfig, SessionManager};
|
|
use tokio::sync::{Mutex, mpsc};
|
|
use tokio_stream::wrappers::ReceiverStream;
|
|
|
|
use crate::support::test_channel::{TestChannel, TestChannelHandle};
|
|
use crate::support::trace_llm::{LlmTrace, TraceLlm, TraceResponse, TraceStep, TraceToolCall};
|
|
|
|
type TelegramCaptures = Arc<Mutex<Vec<(String, OutgoingResponse)>>>;
|
|
|
|
struct RecordingTelegramChannel {
|
|
captures: TelegramCaptures,
|
|
}
|
|
|
|
impl RecordingTelegramChannel {
|
|
fn new() -> (Self, TelegramCaptures) {
|
|
let captures = Arc::new(Mutex::new(Vec::new()));
|
|
(
|
|
Self {
|
|
captures: Arc::clone(&captures),
|
|
},
|
|
captures,
|
|
)
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl Channel for RecordingTelegramChannel {
|
|
fn name(&self) -> &str {
|
|
"telegram"
|
|
}
|
|
|
|
async fn start(&self) -> Result<MessageStream, ChannelError> {
|
|
let (_tx, rx) = mpsc::channel::<IncomingMessage>(1);
|
|
Ok(ReceiverStream::new(rx).boxed())
|
|
}
|
|
|
|
async fn respond(
|
|
&self,
|
|
_msg: &IncomingMessage,
|
|
response: OutgoingResponse,
|
|
) -> Result<(), ChannelError> {
|
|
self.captures
|
|
.lock()
|
|
.await
|
|
.push(("respond".to_string(), response));
|
|
Ok(())
|
|
}
|
|
|
|
async fn send_status(
|
|
&self,
|
|
_status: StatusUpdate,
|
|
_metadata: &serde_json::Value,
|
|
) -> Result<(), ChannelError> {
|
|
Ok(())
|
|
}
|
|
|
|
async fn broadcast(
|
|
&self,
|
|
user_id: &str,
|
|
response: OutgoingResponse,
|
|
) -> Result<(), ChannelError> {
|
|
self.captures
|
|
.lock()
|
|
.await
|
|
.push((user_id.to_string(), response));
|
|
Ok(())
|
|
}
|
|
|
|
async fn health_check(&self) -> Result<(), ChannelError> {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
struct Harness {
|
|
gateway: Arc<TestChannel>,
|
|
telegram_captures: Arc<Mutex<Vec<(String, OutgoingResponse)>>>,
|
|
db: Arc<dyn Database>,
|
|
owner_id: String,
|
|
_temp_dir: tempfile::TempDir,
|
|
agent_handle: Option<tokio::task::JoinHandle<()>>,
|
|
}
|
|
|
|
impl Harness {
|
|
async fn store_telegram_owner_binding(&self, owner_id: i64) {
|
|
for scope in [&self.owner_id, "test-user"] {
|
|
self.db
|
|
.set_setting(
|
|
scope,
|
|
"channels.wasm_channel_owner_ids.telegram",
|
|
&serde_json::json!(owner_id),
|
|
)
|
|
.await
|
|
.expect("failed to store telegram owner binding");
|
|
}
|
|
}
|
|
|
|
async fn wait_for_telegram_broadcasts(
|
|
&self,
|
|
expected: usize,
|
|
timeout: Duration,
|
|
) -> Vec<(String, OutgoingResponse)> {
|
|
let deadline = tokio::time::Instant::now() + timeout;
|
|
loop {
|
|
let snapshot = self.telegram_captures.lock().await.clone();
|
|
if snapshot.len() >= expected || tokio::time::Instant::now() >= deadline {
|
|
return snapshot;
|
|
}
|
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Drop for Harness {
|
|
fn drop(&mut self) {
|
|
self.gateway.signal_shutdown();
|
|
if let Some(handle) = self.agent_handle.take() {
|
|
handle.abort();
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn build_harness(trace: LlmTrace) -> Harness {
|
|
let temp_dir = tempfile::tempdir().expect("failed to create temp dir");
|
|
let db_path = temp_dir.path().join("telegram_message_routing.db");
|
|
let backend = LibSqlBackend::new_local(&db_path)
|
|
.await
|
|
.expect("failed to create test LibSqlBackend");
|
|
backend
|
|
.run_migrations()
|
|
.await
|
|
.expect("failed to run migrations");
|
|
let db: Arc<dyn Database> = Arc::new(backend);
|
|
|
|
let skills_dir = temp_dir.path().join("skills");
|
|
let installed_skills_dir = temp_dir.path().join("installed_skills");
|
|
let _ = std::fs::create_dir_all(&skills_dir);
|
|
let _ = std::fs::create_dir_all(&installed_skills_dir);
|
|
let mut config = Config::for_testing(db_path, skills_dir, installed_skills_dir);
|
|
config.agent.auto_approve_tools = true;
|
|
|
|
let session = Arc::new(SessionManager::new(SessionConfig::default()));
|
|
let log_broadcaster = Arc::new(LogBroadcaster::new());
|
|
let llm: Arc<dyn LlmProvider> = Arc::new(TraceLlm::from_trace(trace));
|
|
|
|
let mut builder = AppBuilder::new(
|
|
config,
|
|
AppBuilderFlags::default(),
|
|
None,
|
|
session,
|
|
log_broadcaster,
|
|
);
|
|
builder.with_database(Arc::clone(&db));
|
|
builder.with_llm(llm);
|
|
|
|
let mut components = builder
|
|
.build_all()
|
|
.await
|
|
.expect("AppBuilder::build_all() failed");
|
|
components.config.agent.auto_approve_tools = true;
|
|
components.config.agent.allow_local_tools = true;
|
|
|
|
let deps = AgentDeps {
|
|
owner_id: components.config.owner_id.clone(),
|
|
store: components.db.clone(),
|
|
llm: components.llm.clone(),
|
|
cheap_llm: components.cheap_llm.clone(),
|
|
safety: components.safety.clone(),
|
|
tools: components.tools.clone(),
|
|
workspace: components.workspace.clone(),
|
|
extension_manager: components.extension_manager.clone(),
|
|
skill_registry: components.skill_registry.clone(),
|
|
skill_catalog: components.skill_catalog.clone(),
|
|
skills_config: components.config.skills.clone(),
|
|
hooks: components.hooks.clone(),
|
|
cost_guard: components.cost_guard.clone(),
|
|
sse_tx: None,
|
|
http_interceptor: None,
|
|
transcription: None,
|
|
document_extraction: None,
|
|
sandbox_readiness: ironclaw::agent::SandboxReadiness::DisabledByConfig,
|
|
builder: None,
|
|
};
|
|
|
|
let gateway = Arc::new(TestChannel::new());
|
|
let gateway_handle = TestChannelHandle::new(Arc::clone(&gateway));
|
|
let (telegram_channel, telegram_captures) = RecordingTelegramChannel::new();
|
|
|
|
let channel_manager = ChannelManager::new();
|
|
channel_manager.add(Box::new(gateway_handle)).await;
|
|
channel_manager.add(Box::new(telegram_channel)).await;
|
|
let channels = Arc::new(channel_manager);
|
|
|
|
deps.tools
|
|
.register_message_tools(Arc::clone(&channels), deps.extension_manager.clone())
|
|
.await;
|
|
|
|
let agent = Agent::new(
|
|
components.config.agent.clone(),
|
|
deps,
|
|
channels,
|
|
None,
|
|
None,
|
|
None,
|
|
Some(Arc::clone(&components.context_manager)),
|
|
None,
|
|
);
|
|
|
|
let agent_handle = tokio::spawn(async move {
|
|
if let Err(err) = agent.run().await {
|
|
eprintln!("[telegram routing e2e] Agent exited with error: {err}");
|
|
}
|
|
});
|
|
|
|
if let Some(rx) = gateway.take_ready_rx().await {
|
|
let _ = tokio::time::timeout(Duration::from_secs(5), rx).await;
|
|
}
|
|
|
|
Harness {
|
|
gateway,
|
|
telegram_captures,
|
|
db,
|
|
owner_id: components.config.owner_id.clone(),
|
|
_temp_dir: temp_dir,
|
|
agent_handle: Some(agent_handle),
|
|
}
|
|
}
|
|
|
|
fn single_message_trace(arguments: serde_json::Value, final_text: &str) -> LlmTrace {
|
|
LlmTrace::single_turn(
|
|
"telegram-message-routing",
|
|
"send a reminder",
|
|
vec![
|
|
TraceStep {
|
|
request_hint: None,
|
|
response: TraceResponse::ToolCalls {
|
|
tool_calls: vec![TraceToolCall {
|
|
id: "call_message_1".to_string(),
|
|
name: "message".to_string(),
|
|
arguments,
|
|
}],
|
|
input_tokens: 32,
|
|
output_tokens: 12,
|
|
},
|
|
expected_tool_results: Vec::new(),
|
|
},
|
|
TraceStep {
|
|
request_hint: None,
|
|
response: TraceResponse::Text {
|
|
content: final_text.to_string(),
|
|
input_tokens: 24,
|
|
output_tokens: 8,
|
|
},
|
|
expected_tool_results: Vec::new(),
|
|
},
|
|
],
|
|
)
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn telegram_message_tool_uses_bound_owner_target_when_target_omitted() {
|
|
let harness = build_harness(single_message_trace(
|
|
serde_json::json!({
|
|
"content": "Walk Conan",
|
|
"channel": "telegram",
|
|
}),
|
|
"Sent on Telegram.",
|
|
))
|
|
.await;
|
|
|
|
harness.store_telegram_owner_binding(424242).await;
|
|
|
|
harness
|
|
.gateway
|
|
.send_message("remind me to walk conan")
|
|
.await;
|
|
let responses = harness
|
|
.gateway
|
|
.wait_for_responses(1, Duration::from_secs(10))
|
|
.await;
|
|
assert!(
|
|
responses
|
|
.iter()
|
|
.any(|response| response.content.contains("Sent on Telegram")),
|
|
"expected assistant confirmation, got: {:?}",
|
|
responses
|
|
.iter()
|
|
.map(|response| &response.content)
|
|
.collect::<Vec<_>>()
|
|
);
|
|
|
|
let broadcasts = harness
|
|
.wait_for_telegram_broadcasts(1, Duration::from_secs(10))
|
|
.await;
|
|
assert_eq!(
|
|
broadcasts.len(),
|
|
1,
|
|
"expected exactly one telegram broadcast"
|
|
);
|
|
assert_eq!(broadcasts[0].0, "424242");
|
|
assert_eq!(broadcasts[0].1.content, "Walk Conan");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn telegram_message_tool_prefers_explicit_target_over_bound_owner_target() {
|
|
let harness = build_harness(single_message_trace(
|
|
serde_json::json!({
|
|
"content": "Walk Conan",
|
|
"channel": "telegram",
|
|
"target": "999999",
|
|
}),
|
|
"Sent on Telegram.",
|
|
))
|
|
.await;
|
|
|
|
harness.store_telegram_owner_binding(424242).await;
|
|
|
|
harness.gateway.send_message("send the reminder").await;
|
|
let _ = harness
|
|
.gateway
|
|
.wait_for_responses(1, Duration::from_secs(10))
|
|
.await;
|
|
|
|
let broadcasts = harness
|
|
.wait_for_telegram_broadcasts(1, Duration::from_secs(10))
|
|
.await;
|
|
assert_eq!(
|
|
broadcasts.len(),
|
|
1,
|
|
"expected exactly one telegram broadcast"
|
|
);
|
|
assert_eq!(broadcasts[0].0, "999999");
|
|
assert_eq!(broadcasts[0].1.content, "Walk Conan");
|
|
}
|
|
}
|