test: add 26 tests for multi-thread safety, db CRUD, concurrency, errors (#442)

* fix: use std::sync::RwLock in MessageTool to avoid runtime panic

The `requires_approval` method is synchronous but was using
`tokio::sync::RwLock` with `.await` which requires blocking the
runtime. This caused a panic:
"Cannot block the current thread from within a runtime"

Changes:
- Replace `tokio::sync::RwLock` with `std::sync::RwLock` for
  `default_channel` and `default_target` fields
- Use `unwrap_or_else(|e| e.into_inner())` to gracefully handle
  poisoned locks (recovers instead of panicking)
- Update all usages from `.read().await` to `.read().unwrap_or_else()`

The locks are short-held (just cloning strings), making std::sync::RwLock
appropriate for sync methods called from async contexts.

Fixes: "Cannot block the current thread from within a runtime" panic
when the LLM tries to send a message via the message tool.

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

* test: comprehensive testing improvements and fix MessageTool blocking_read panic

Fix tokio::sync::RwLock::blocking_read() panic in MessageTool::requires_approval()
under multi-threaded tokio runtimes by switching to std::sync::RwLock with poison
recovery. Add 26 new tests across 4 tiers:

Tier 1 - Multi-thread runtime safety:
- Fix MessageTool to use std::sync::RwLock instead of tokio::sync::RwLock
- 4 multi-thread tests for MessageTool::requires_approval() scenarios
- 1 multi-thread test for HttpTool credential-dependent approval
- 1 structural test exercising all core tool sync trait methods under multi-thread runtime

Tier 2 - Database CRUD coverage:
- Settings lifecycle (CRUD, bulk ops)
- Tool failure tracking (record, broken list, repair)
- Routine lifecycle (create, get, list, update, delete, runs)
- LLM call recording
- Sandbox job lifecycle (create, get, update, list, mode)
- Job events (save, list, limit)
- Estimation snapshot round-trip

Tier 3 - Concurrency:
- ToolRegistry concurrent register + read under 4-worker runtime

Tier 4 - Error coverage:
- Display tests for all 8 error variants
- From conversion tests for top-level Error enum

Supersedes the fix in PR #411 with the same bug fix plus comprehensive test coverage.

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

* fix: remove trailing whitespace in registry.rs

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

---------

Co-authored-by: Jerome Revillard <[email protected]>
Co-authored-by: Claude Opus 4.6 <[email protected]>
Co-authored-by: Illia Polosukhin <[email protected]>
This commit is contained in:
Zaki Manian
2026-03-06 04:39:04 +00:00
committed by GitHub
co-authored by Jerome Revillard Claude Opus 4.6 Illia Polosukhin
parent 04c5c3fe9f
commit 06c84a5c77
6 changed files with 863 additions and 24 deletions
+579
View File
@@ -652,4 +652,583 @@ mod tests {
assert_eq!(response.content, "hello world");
assert_eq!(response.finish_reason, FinishReason::Stop);
}
// === Database CRUD coverage for untested trait methods ===
#[cfg(feature = "libsql")]
#[tokio::test]
async fn test_settings_crud() {
let harness = TestHarnessBuilder::new().build().await;
let db = &harness.db;
// Initially no setting
let val = db.get_setting("user1", "theme").await.expect("get");
assert!(val.is_none());
// Set a value
db.set_setting("user1", "theme", &serde_json::json!("dark"))
.await
.expect("set");
// Read it back
let val = db
.get_setting("user1", "theme")
.await
.expect("get")
.expect("should exist");
assert_eq!(val, serde_json::json!("dark"));
// Update it
db.set_setting("user1", "theme", &serde_json::json!("light"))
.await
.expect("set update");
let val = db
.get_setting("user1", "theme")
.await
.expect("get")
.expect("should exist");
assert_eq!(val, serde_json::json!("light"));
// List settings
let all = db.list_settings("user1").await.expect("list");
assert_eq!(all.len(), 1);
// Delete
let deleted = db.delete_setting("user1", "theme").await.expect("delete");
assert!(deleted);
let val = db.get_setting("user1", "theme").await.expect("get");
assert!(val.is_none());
// Delete non-existent
let deleted = db.delete_setting("user1", "theme").await.expect("delete");
assert!(!deleted);
}
#[cfg(feature = "libsql")]
#[tokio::test]
async fn test_settings_bulk_operations() {
let harness = TestHarnessBuilder::new().build().await;
let db = &harness.db;
// Initially no settings
let has = db.has_settings("bulk_user").await.expect("has_settings");
assert!(!has);
// Set all settings at once
let mut settings = std::collections::HashMap::new();
settings.insert("key1".to_string(), serde_json::json!("value1"));
settings.insert("key2".to_string(), serde_json::json!(42));
db.set_all_settings("bulk_user", &settings)
.await
.expect("set_all");
// Has settings should now be true
let has = db.has_settings("bulk_user").await.expect("has_settings");
assert!(has);
// Get all settings
let all = db.get_all_settings("bulk_user").await.expect("get_all");
assert_eq!(all.len(), 2);
assert_eq!(all["key1"], serde_json::json!("value1"));
assert_eq!(all["key2"], serde_json::json!(42));
// Get full setting row
let full = db
.get_setting_full("bulk_user", "key1")
.await
.expect("get_full")
.expect("should exist");
assert_eq!(full.key, "key1");
}
#[cfg(feature = "libsql")]
#[tokio::test]
async fn test_tool_failure_tracking() {
let harness = TestHarnessBuilder::new().build().await;
let db = &harness.db;
// Record some failures
db.record_tool_failure("bad_tool", "connection refused")
.await
.expect("record 1");
db.record_tool_failure("bad_tool", "timeout")
.await
.expect("record 2");
db.record_tool_failure("bad_tool", "parse error")
.await
.expect("record 3");
// Get broken tools (threshold = 2, should include bad_tool with 3 failures)
let broken = db.get_broken_tools(2).await.expect("get broken");
assert!(!broken.is_empty());
let found = broken.iter().find(|b| b.name == "bad_tool");
assert!(found.is_some(), "bad_tool should be in broken tools list");
// Mark as repaired
db.mark_tool_repaired("bad_tool")
.await
.expect("mark repaired");
}
#[cfg(feature = "libsql")]
#[tokio::test]
async fn test_routine_crud() {
use crate::agent::routine::{
NotifyConfig, Routine, RoutineAction, RoutineGuardrails, RoutineRun, RunStatus, Trigger,
};
let harness = TestHarnessBuilder::new().build().await;
let db = &harness.db;
let routine_id = uuid::Uuid::new_v4();
let routine = Routine {
id: routine_id,
name: "test-routine".to_string(),
description: "A test routine".to_string(),
user_id: "user1".to_string(),
enabled: true,
trigger: Trigger::Cron {
schedule: "0 * * * *".to_string(),
},
action: RoutineAction::Lightweight {
prompt: "Check status".to_string(),
context_paths: vec![],
max_tokens: 500,
},
guardrails: RoutineGuardrails {
cooldown: std::time::Duration::from_secs(60),
max_concurrent: 1,
dedup_window: None,
},
notify: NotifyConfig {
channel: None,
user: "user1".to_string(),
on_attention: true,
on_failure: true,
on_success: false,
},
last_run_at: None,
next_fire_at: None,
run_count: 0,
consecutive_failures: 0,
state: serde_json::json!({}),
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
};
// Create
db.create_routine(&routine).await.expect("create routine");
// Get by ID
let fetched = db
.get_routine(routine_id)
.await
.expect("get routine")
.expect("should exist");
assert_eq!(fetched.name, "test-routine");
assert!(fetched.enabled);
// Get by name
let by_name = db
.get_routine_by_name("user1", "test-routine")
.await
.expect("get by name")
.expect("should exist");
assert_eq!(by_name.id, routine_id);
// List routines for user
let list = db.list_routines("user1").await.expect("list routines");
assert_eq!(list.len(), 1);
// List all routines
let all = db.list_all_routines().await.expect("list all");
assert!(!all.is_empty());
// Update routine (disable + change description)
let mut updated = fetched;
updated.enabled = false;
updated.description = "Updated description".to_string();
db.update_routine(&updated).await.expect("update routine");
let re_fetched = db
.get_routine(routine_id)
.await
.expect("get")
.expect("exists");
assert!(!re_fetched.enabled);
assert_eq!(re_fetched.description, "Updated description");
// Create a routine run
let run_id = uuid::Uuid::new_v4();
let run = RoutineRun {
id: run_id,
routine_id,
trigger_type: "cron".to_string(),
trigger_detail: Some("0 * * * *".to_string()),
started_at: chrono::Utc::now(),
completed_at: None,
status: RunStatus::Running,
result_summary: None,
tokens_used: None,
job_id: None,
created_at: chrono::Utc::now(),
};
db.create_routine_run(&run).await.expect("create run");
// List runs
let runs = db
.list_routine_runs(routine_id, 10)
.await
.expect("list runs");
assert_eq!(runs.len(), 1);
assert!(matches!(runs[0].status, RunStatus::Running));
// Complete the run
db.complete_routine_run(run_id, RunStatus::Ok, Some("All good"), Some(150))
.await
.expect("complete run");
let runs = db
.list_routine_runs(routine_id, 10)
.await
.expect("list runs after complete");
assert!(matches!(runs[0].status, RunStatus::Ok));
// Delete
let deleted = db.delete_routine(routine_id).await.expect("delete");
assert!(deleted);
// Delete non-existent
let deleted = db.delete_routine(routine_id).await.expect("delete again");
assert!(!deleted);
}
#[cfg(feature = "libsql")]
#[tokio::test]
async fn test_routine_runtime_update() {
use crate::agent::routine::{
NotifyConfig, Routine, RoutineAction, RoutineGuardrails, Trigger,
};
let harness = TestHarnessBuilder::new().build().await;
let db = &harness.db;
let routine_id = uuid::Uuid::new_v4();
let routine = Routine {
id: routine_id,
name: "runtime-test".to_string(),
description: "Test runtime update".to_string(),
user_id: "user1".to_string(),
enabled: true,
trigger: Trigger::Manual,
action: RoutineAction::Lightweight {
prompt: "test".to_string(),
context_paths: vec![],
max_tokens: 100,
},
guardrails: RoutineGuardrails {
cooldown: std::time::Duration::from_secs(0),
max_concurrent: 1,
dedup_window: None,
},
notify: NotifyConfig {
channel: None,
user: "user1".to_string(),
on_attention: false,
on_failure: false,
on_success: false,
},
last_run_at: None,
next_fire_at: None,
run_count: 0,
consecutive_failures: 0,
state: serde_json::json!({}),
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
};
db.create_routine(&routine).await.expect("create");
let now = chrono::Utc::now();
db.update_routine_runtime(
routine_id,
now,
Some(now + chrono::TimeDelta::seconds(3600)),
5,
2,
&serde_json::json!({"last_result": "ok"}),
)
.await
.expect("update runtime");
let fetched = db
.get_routine(routine_id)
.await
.expect("get")
.expect("exists");
assert_eq!(fetched.run_count, 5);
assert_eq!(fetched.consecutive_failures, 2);
assert!(fetched.last_run_at.is_some());
assert!(fetched.next_fire_at.is_some());
// Cleanup
db.delete_routine(routine_id).await.expect("delete");
}
#[cfg(feature = "libsql")]
#[tokio::test]
async fn test_llm_call_recording() {
use crate::history::LlmCallRecord;
let harness = TestHarnessBuilder::new().build().await;
let db = &harness.db;
let record = LlmCallRecord {
job_id: None,
conversation_id: None,
provider: "openai",
model: "gpt-4",
input_tokens: 100,
output_tokens: 50,
cost: Decimal::new(5, 3), // 0.005
purpose: Some("test"),
};
let call_id = db.record_llm_call(&record).await.expect("record llm call");
assert!(!call_id.is_nil());
}
#[cfg(feature = "libsql")]
#[tokio::test]
async fn test_sandbox_job_lifecycle() {
use crate::history::SandboxJobRecord;
let harness = TestHarnessBuilder::new().build().await;
let db = &harness.db;
let job_id = uuid::Uuid::new_v4();
let job = SandboxJobRecord {
id: job_id,
task: "Build a test tool".to_string(),
status: "creating".to_string(),
user_id: "user1".to_string(),
project_dir: "/workspace/test".to_string(),
success: None,
failure_reason: None,
created_at: chrono::Utc::now(),
started_at: None,
completed_at: None,
credential_grants_json: "[]".to_string(),
};
// Create
db.save_sandbox_job(&job).await.expect("save sandbox job");
// Get
let fetched = db
.get_sandbox_job(job_id)
.await
.expect("get")
.expect("should exist");
assert_eq!(fetched.task, "Build a test tool");
assert_eq!(fetched.status, "creating");
// Update status to running
db.update_sandbox_job_status(
job_id,
"running",
None,
None,
Some(chrono::Utc::now()),
None,
)
.await
.expect("update to running");
// Update to completed
db.update_sandbox_job_status(
job_id,
"completed",
Some(true),
Some("Done"),
None,
Some(chrono::Utc::now()),
)
.await
.expect("update to completed");
let fetched = db
.get_sandbox_job(job_id)
.await
.expect("get")
.expect("should exist");
assert_eq!(fetched.status, "completed");
assert_eq!(fetched.success, Some(true));
// List
let all = db.list_sandbox_jobs().await.expect("list");
assert!(!all.is_empty());
// Summary
let summary = db.sandbox_job_summary().await.expect("summary");
assert!(summary.total >= 1);
// Per-user list
let user_jobs = db
.list_sandbox_jobs_for_user("user1")
.await
.expect("user list");
assert!(!user_jobs.is_empty());
// Ownership check
let belongs = db
.sandbox_job_belongs_to_user(job_id, "user1")
.await
.expect("belongs check");
assert!(belongs);
let not_belongs = db
.sandbox_job_belongs_to_user(job_id, "other_user")
.await
.expect("belongs check");
assert!(!not_belongs);
}
#[cfg(feature = "libsql")]
#[tokio::test]
async fn test_sandbox_job_mode() {
use crate::history::SandboxJobRecord;
let harness = TestHarnessBuilder::new().build().await;
let db = &harness.db;
let job_id = uuid::Uuid::new_v4();
let job = SandboxJobRecord {
id: job_id,
task: "Mode test".to_string(),
status: "creating".to_string(),
user_id: "user1".to_string(),
project_dir: "/workspace".to_string(),
success: None,
failure_reason: None,
created_at: chrono::Utc::now(),
started_at: None,
completed_at: None,
credential_grants_json: "[]".to_string(),
};
db.save_sandbox_job(&job).await.expect("save");
// Default mode
let mode = db.get_sandbox_job_mode(job_id).await.expect("get mode");
// Default is "worker" per schema or NULL
assert!(mode.is_none() || mode.as_deref() == Some("worker"));
// Update mode
db.update_sandbox_job_mode(job_id, "claude_code")
.await
.expect("update mode");
let mode = db
.get_sandbox_job_mode(job_id)
.await
.expect("get mode")
.expect("should have mode");
assert_eq!(mode, "claude_code");
}
#[cfg(feature = "libsql")]
#[tokio::test]
async fn test_job_events() {
use crate::history::SandboxJobRecord;
let harness = TestHarnessBuilder::new().build().await;
let db = &harness.db;
// Create a sandbox job first (foreign key)
let job_id = uuid::Uuid::new_v4();
let job = SandboxJobRecord {
id: job_id,
task: "Event test".to_string(),
status: "running".to_string(),
user_id: "user1".to_string(),
project_dir: "/workspace".to_string(),
success: None,
failure_reason: None,
created_at: chrono::Utc::now(),
started_at: Some(chrono::Utc::now()),
completed_at: None,
credential_grants_json: "[]".to_string(),
};
db.save_sandbox_job(&job).await.expect("save job");
// Save events
db.save_job_event(
job_id,
"tool_call",
&serde_json::json!({"tool": "shell", "args": {"command": "ls"}}),
)
.await
.expect("save event 1");
db.save_job_event(
job_id,
"tool_result",
&serde_json::json!({"output": "file1.txt\nfile2.txt"}),
)
.await
.expect("save event 2");
db.save_job_event(
job_id,
"llm_response",
&serde_json::json!({"content": "Found 2 files"}),
)
.await
.expect("save event 3");
// List all events
let events = db.list_job_events(job_id, None).await.expect("list events");
assert_eq!(events.len(), 3);
// List with limit
let events = db
.list_job_events(job_id, Some(2))
.await
.expect("list events limited");
assert_eq!(events.len(), 2);
}
#[cfg(feature = "libsql")]
#[tokio::test]
async fn test_estimation_snapshot_round_trip() {
let harness = TestHarnessBuilder::new().build().await;
let db = &harness.db;
// Create a job first
let job_ctx = crate::context::JobContext::with_user("user1", "Estimate test", "testing");
let job_id = job_ctx.job_id;
db.save_job(&job_ctx).await.expect("save job");
// Save estimation snapshot
let snap_id = db
.save_estimation_snapshot(
job_id,
"code_generation",
&["shell".to_string(), "write_file".to_string()],
Decimal::new(50, 2), // 0.50
120,
Decimal::new(500, 2), // 5.00
)
.await
.expect("save snapshot");
assert!(!snap_id.is_nil());
// Update with actuals
db.update_estimation_actuals(
snap_id,
Decimal::new(45, 2), // 0.45
110,
Some(Decimal::new(600, 2)), // 6.00
)
.await
.expect("update actuals");
}
}