The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
test(relay): exercise relayed turns through the real session host
Steven Enamakeld944050274d3
diff --git a/crates/openhuman-core/src/channels/providers/relay/ops_tests.rs b/crates/openhuman-core/src/channels/providers/relay/ops_tests.rsindex 7b05b6df8c..dc6851ce2f 100644--- a/crates/openhuman-core/src/channels/providers/relay/ops_tests.rs+++ b/crates/openhuman-core/src/channels/providers/relay/ops_tests.rs@@ -1,9 +1,14 @@use super::super::channel::CHANNEL_OUTBOUND_EVENT;use super::*;-use crate::agent::bus::{mock_agent_run_turn, AgentTurnRequest, AgentTurnResponse};+use crate::agent::host_agents::{self, HostAgent, HostAgentResolver};use crate::agent::turn_origin::AgentTurnOrigin;use crate::core::runtime::{ContextOverlay, CoreContext, DomainSet};-use std::sync::Mutex;+use serde_json::{json, Value};+use std::sync::atomic::{AtomicUsize, Ordering};+use std::sync::{Arc, Mutex};+use tinytools::{PermissionLevel, Tool, ToolResult};+use wiremock::matchers::{method, path};+use wiremock::{Mock, MockServer, ResponseTemplate};#[tokio::test]async fn relayed_thread_and_external_authority_survive_scoped_spawn() {@@ -88,49 +93,143 @@ fn params(message_id: &str, text: &str) -> RelayInboundParams {.expect("params")}-/// What the stubbed `agent.run_turn` saw.+/// What the actual SessionHost tool execution saw.#[derive(Default)]struct Seen {- histories: Vec<Vec<(String, String)>>,- origins: Vec<AgentTurnOrigin>,- /// Whether the relayed thread was busy (for background delivery) while- /// the turn ran.+ turns: AtomicUsize,+ origins: Mutex<Vec<Option<AgentTurnOrigin>>>,+ threads: Mutex<Vec<Option<String>>>,busy: Vec<bool>,}+struct RelayProbe {+ dispatch_ctx: crate::core::runtime::DispatchCtx,+ thread_id: String,+ seen: Arc<Seen>,+}++#[async_trait::async_trait]+impl Tool for RelayProbe {+ fn name(&self) -> &str { "relay_probe" }+ fn description(&self) -> &str { "Record trusted relay turn context." }+ fn parameters_schema(&self) -> Value { json!({"type":"object","properties":{}}) }+ fn permission_level(&self) -> PermissionLevel { PermissionLevel::ReadOnly }+ async fn execute(&self, _args: Value) -> anyhow::Result<ToolResult> {+ self.seen.turns.fetch_add(1, Ordering::SeqCst);+ self.seen.origins.lock().unwrap().push(CoreContext::current_turn_origin());+ self.seen.threads.lock().unwrap().push(CoreContext::current_channel_thread());+ self.seen.busy.lock().unwrap().push(+ crate::agent::orchestration::busy_guard::is_busy(&self.dispatch_ctx, &self.thread_id),+ );+ Ok(ToolResult::success("relay context recorded"))+ }+}++struct RelayAgentResolver {+ config: Config,+ context: Arc<CoreContext>,+ dispatch_ctx: crate::core::runtime::DispatchCtx,+ seen: Arc<Seen>,+ thread_id: String,+}++impl HostAgentResolver for RelayAgentResolver {+ fn resolve(&self, agent_id: &str) -> Option<HostAgent> {+ (agent_id == "relay-agent").then(|| {+ let mut definition =+ crate::agent::harness::host_definition::AgentDefinitionRegistry::builtins_only()+ .get("orchestrator")+ .cloned()+ .expect("the orchestrator is built in");+ definition.id = agent_id.to_owned();+ definition.subagents.clear();+ definition.system_prompt = tinyagents_definition::PromptSource::Inline(+ "Relay integration test".into(),+ );+ definition.tools = tinyagents_definition::ToolScope::Named(vec!["relay_probe".into()]);+ let dispatch_ctx = self.dispatch_ctx.clone();+ let thread_id = self.thread_id.clone();+ let seen = Arc::clone(&self.seen);+ HostAgent {+ definition,+ config: self.config.clone(),+ host_tools: Some(Arc::new(move |_turn| {+ crate::agent::HostTurnTools::advertised(vec![Box::new(RelayProbe {+ dispatch_ctx: dispatch_ctx.clone(),+ thread_id: thread_id.clone(),+ seen: Arc::clone(&seen),+ })])+ })),+ hooks: Default::default(),+ context: Arc::clone(&self.context),+ }+ })+ }+}++fn tool_call() -> Value {+ json!({+ "id":"relay-tool-call", "object":"chat.completion", "created":1,"model":"fixture",+ "choices":[{"index":0,"finish_reason":"tool_calls","message":{"role":"assistant","content":null,+ "tool_calls":[{"id":"call_relay_probe","type":"function","function":{"name":"relay_probe","arguments":"{}"}}]}}],+ "usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}+ })+}++fn answer(text: &str) -> Value {+ json!({+ "id":"relay-answer", "object":"chat.completion", "created":1,"model":"fixture",+ "choices":[{"index":0,"finish_reason":"stop","message":{"role":"assistant","content":text}}],+ "usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}+ })+}++async fn relay_provider() -> MockServer {+ let server = MockServer::start().await;+ Mock::given(method("POST"))+ .and(path("/v1/chat/completions"))+ .respond_with(ResponseTemplate::new(200).set_body_json(tool_call()))+ .up_to_n_times(2)+ .mount(&server)+ .await;+ for text in ["answer 1", "answer 2"] {+ Mock::given(method("POST"))+ .and(path("/v1/chat/completions"))+ .respond_with(ResponseTemplate::new(200).set_body_json(answer(text)))+ .up_to_n_times(1)+ .mount(&server)+ .await;+ }+ server+}+#[tokio::test]async fn a_relayed_turn_runs_as_an_external_channel_and_keeps_its_thread() {+ let _slot = host_agents::tests::lock();let tmp = tempfile::tempdir().unwrap();- crate::agent::harness::host_definition::AgentDefinitionRegistry::init_global_builtins()- .unwrap();- let config = config(&tmp);+ let server = relay_provider().await;+ let mut config = config(&tmp);+ config.agent.channel_agents.insert("telegram".into(), "relay-agent".into());+ config.default_model = Some("fixture".into());+ let route = crate::config::schema::EphemeralRoute::from_params(+ Some(format!("{}/v1", server.uri())), Some("sk-fixture".into()),+ ).unwrap();+ crate::config::schema::ephemeral_route::apply(&mut config, route);let core_context = CoreContext::for_test(DomainSet::full(), None).derive_with(ContextOverlay::new(config.clone(), DomainSet::full(), Default::default()).session_agent("relay-agent"),);let dispatch_ctx = crate::core::runtime::DispatchCtx::new(core_context);- let stub_dispatch = dispatch_ctx.clone();let seen = Arc::new(Mutex::new(Seen::default()));- let stub_seen = Arc::clone(&seen);let relayed_thread = params("m1", "x").thread_id();- let _bus = mock_agent_run_turn(move |req: AgentTurnRequest| {- let mut seen = stub_seen.lock().unwrap();- seen.busy- .push(crate::agent::orchestration::busy_guard::is_busy(- &stub_dispatch,- &relayed_thread,- ));- seen.histories.push(- req.history- .iter()- .map(|m| (m.role.clone(), m.content.clone()))- .collect(),- );- seen.origins.push(req.origin.clone());- let n = seen.histories.len();- async move { Ok(AgentTurnResponse::new(format!("answer {n}"))) }- })- .await;+ let resolver: Arc<dyn HostAgentResolver> = Arc::new(RelayAgentResolver {+ config: config.clone(),+ context: dispatch_ctx.core().unwrap().clone(),+ dispatch_ctx: dispatch_ctx.clone(),+ seen: Arc::clone(&seen),+ thread_id: relayed_thread.clone(),+ });+ host_agents::install(Arc::clone(&resolver));let workspace = config.workspace_dir.clone();let mut events = crate::web_chat::subscribe_web_channel_events();@@ -149,13 +248,13 @@ async fn a_relayed_turn_runs_as_an_external_channel_and_keeps_its_thread() {format!("req-{id}"),).await;- assert_eq!(replies.len(), 1, "{replies:?}");+ assert_eq!(replies, vec![format!("answer {}", if id == "m1" { 1 } else { 2 })]);}- let seen = seen.lock().unwrap();- assert_eq!(seen.histories.len(), 2, "one agent turn per message");+ assert!(host_agents::clear_if(&resolver));+ assert_eq!(seen.turns.load(Ordering::SeqCst), 2, "one tool execution per agent turn");assert_eq!(- seen.busy,+ *seen.busy.lock().unwrap(),vec![true, true],"a relayed turn marks its thread busy, so a background result waits");@@ -166,32 +265,29 @@ async fn a_relayed_turn_runs_as_an_external_channel_and_keeps_its_thread() {),"the thread is idle once the turn ends");- for origin in &seen.origins {+ for origin in seen.origins.lock().unwrap().iter() {match origin {- AgentTurnOrigin::ExternalChannel {+ Some(AgentTurnOrigin::ExternalChannel {channel, sender, ..- } => {+ }) => {assert_eq!(channel, "telegram");assert_eq!(sender.as_deref(), Some("42"));}other => panic!("relayed turns run as an external channel, got {other:?}"),}}- // The second turn is seeded from the thread: the first exchange, then- // the new message.- let second = &seen.histories[1];- let contents: Vec<&str> = second.iter().map(|(_, c)| c.as_str()).collect();- assert!(- contents.iter().any(|c| c.contains("first question")),- "{contents:?}"- );- assert!(contents.contains(&"answer 1"), "{contents:?}");- assert!(- contents- .last()- .is_some_and(|c| c.contains("second question")),- "{contents:?}"- );+ assert_eq!(*seen.threads.lock().unwrap(), vec![Some(relayed_thread.clone()); 2]);++ let requests = server.received_requests().await.unwrap();+ let chats: Vec<Value> = requests.iter()+ .filter(|request| request.url.path().ends_with("/chat/completions"))+ .map(|request| serde_json::from_slice(&request.body).unwrap())+ .collect();+ assert!(chats.len() >= 4, "{chats:#?}");+ let serialized = serde_json::to_string(&chats[2..]).unwrap();+ assert!(serialized.contains("first question"), "{serialized}");+ assert!(serialized.contains("answer 1"), "{serialized}");+ assert!(serialized.contains("second question"), "{serialized}");let thread = params("m1", "x").thread_id();let messages =