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
fix(search): key the refusal to the configuration that failed
obchain4726dd8f7b7c
diff --git a/crates/openhuman-core/src/agent/session_host/recorded_tools.rs b/crates/openhuman-core/src/agent/session_host/recorded_tools.rsindex 5343fef64f..c23aee4106 100644--- a/crates/openhuman-core/src/agent/session_host/recorded_tools.rs+++ b/crates/openhuman-core/src/agent/session_host/recorded_tools.rs@@ -135,24 +135,21 @@ pub(super) fn rehydrate_search_tools(.flat_map(|tools| tools.iter().map(|tool| tool.name())).collect();let mut seen = HashSet::new();- // One batch, so the rebuilt roles share the "no provider answered" latch- // the live surface gives its own search tools (#6991).- let rebuilt: Vec<Box<dyn Tool>> = crate::search::TinySearchTool::recorded_batch(- recorded- .iter()- .filter(|spec| is_search_role_tool(&spec.name))- .filter(|spec| !live_names.contains(spec.name.as_str()))- .filter(|spec| seen.insert(spec.name.clone()))- .map(|spec| tinysearch_bus::ToolSpec {- name: spec.name.clone(),- description: spec.description.clone(),- parameters: spec.parameters.clone(),- })- .collect::<Vec<_>>(),- )- .into_iter()- .map(|tool| Box::new(tool) as Box<dyn Tool>)- .collect();+ let rebuilt: Vec<Box<dyn Tool>> = recorded+ .iter()+ .filter(|spec| is_search_role_tool(&spec.name))+ .filter(|spec| !live_names.contains(spec.name.as_str()))+ .filter(|spec| seen.insert(spec.name.clone()))+ .map(|spec| {+ Box::new(crate::search::TinySearchTool::recorded(+ tinysearch_bus::ToolSpec {+ name: spec.name.clone(),+ description: spec.description.clone(),+ parameters: spec.parameters.clone(),+ },+ )) as Box<dyn Tool>+ })+ .collect();if !rebuilt.is_empty() {log::info!("[session] rebuilt {} recorded search tool(s) the live surface did not supply agent={agent_id}",diff --git a/crates/openhuman-core/src/search/tools.rs b/crates/openhuman-core/src/search/tools.rsindex 82d4e9d0b9..38cd3d940c 100644--- a/crates/openhuman-core/src/search/tools.rs+++ b/crates/openhuman-core/src/search/tools.rs@@ -6,8 +6,9 @@//! through `modules::search::execute_tool`, so a provider or login change is//! honoured on the next call without rebuilding the session.-use std::sync::atomic::{AtomicBool, Ordering};-use std::sync::Arc;+use std::collections::hash_map::DefaultHasher;+use std::hash::{Hash, Hasher};+use std::sync::{Arc, Mutex};use async_trait::async_trait;use serde_json::Value;@@ -22,8 +23,8 @@ use crate::config::Config;/// "unavailable" in this session. It tells the model to stop rather than to/// wait, because nothing about the session will change the answer (#6991).pub const SEARCH_EXHAUSTED_MESSAGE: &str =- "Web search is not available in this session. Do not call it again \u{2014} \- answer from the material you already have.";+ "Web search is not available with this setup: no provider could answer. Do not call \+ it again \u{2014} answer from the material you already have.";pub struct TinySearchTool {spec: ToolSpec,@@ -31,16 +32,22 @@ pub struct TinySearchTool {/// recorded transcript, which resolves the live config per call.config: Option<Arc<Config>>,exposure: ToolExposure,- /// Latched when a call reports that no provider could answer. Shared by- /// the search tools built for one session instance, so the model is told- /// once and the later calls cost no module round-trip.+ /// The provider configuration a call last found nothing usable under.////// A credential alone makes the managed route look reachable/// (`providers::backend_credential_available`), so a deployment that is- /// offline, firewalled, out of balance or holding a dead key offers these- /// tools on every turn and fails every call. The agent then spends turns- /// on a tool that cannot work, at the moment it is least sure what to do.- exhausted: Arc<AtomicBool>,+ /// offline, firewalled, out of balance or holding a dead key offers this+ /// tool on every turn and fails every call. The agent then spends turns on+ /// a tool that cannot work, at the moment it is least sure what to do.+ ///+ /// Keyed by configuration rather than latched outright, because this+ /// module's contract is that every call re-reads the live config so a+ /// provider or login change is honoured without rebuilding the session. A+ /// call whose signature differs from the recorded one tries again; only a+ /// repeat under the same configuration is refused. The signature is this+ /// tool's own view — its role order and the resolved providers — so one+ /// tool's dead providers never answer for another's.+ exhausted_for: Mutex<Option<u64>>,}impl TinySearchTool {@@ -49,7 +56,7 @@ impl TinySearchTool {spec,config: Some(config),exposure: ToolExposure::Direct,- exhausted: Arc::new(AtomicBool::new(false)),+ exhausted_for: Mutex::new(None),}}@@ -60,34 +67,46 @@ impl TinySearchTool {spec,config: None,exposure: ToolExposure::Direct,- exhausted: Arc::new(AtomicBool::new(false)),+ exhausted_for: Mutex::new(None),}}- /// Recorded tools that share one exhaustion latch, the way the tools of a- /// live session do: the roles are three declarations over the same- /// providers, so one of them finding nothing settles it for all three.- pub fn recorded_batch(specs: impl IntoIterator<Item = ToolSpec>) -> Vec<Self> {- let exhausted = Arc::new(AtomicBool::new(false));- specs- .into_iter()- .map(|spec| Self {- spec,- config: None,- exposure: ToolExposure::Direct,- exhausted: exhausted.clone(),- })- .collect()+ /// Whether this tool already found nothing usable under `signature`.+ pub(crate) fn is_exhausted_for(&self, signature: u64) -> bool {+ self.exhausted_for+ .lock()+ .map(|recorded| *recorded == Some(signature))+ .unwrap_or(false)}- /// Whether this session already learned that no provider can answer.- pub(crate) fn is_exhausted(&self) -> bool {- self.exhausted.load(Ordering::Relaxed)+ /// Record that no provider could answer under `signature`. Replaces any+ /// earlier one, so the refusal always describes the current configuration.+ pub(crate) fn mark_exhausted_for(&self, signature: u64) {+ if let Ok(mut recorded) = self.exhausted_for.lock() {+ *recorded = Some(signature);+ }}- /// Record that no provider could answer, for the tools sharing this latch.- pub(crate) fn mark_exhausted(&self) {- self.exhausted.store(true, Ordering::Relaxed);+ /// What this tool's providers look like right now: the order its role+ /// draws from, and every provider's resolved reachability. Two calls agree+ /// only while nothing a user could change — a key, a route, a provider+ /// selection, a login — has moved.+ pub(crate) fn provider_signature(&self, config: &Config) -> u64 {+ let mut hasher = DefaultHasher::new();+ if let Some(role) = tinysearch_bus::role_for_tool(&self.spec.name) {+ for provider in super::providers::role_order(config, role) {+ provider.hash(&mut hasher);+ }+ }+ for provider in super::providers::resolve(config) {+ provider.id.hash(&mut hasher);+ provider.enabled.hash(&mut hasher);+ provider.usable.hash(&mut hasher);+ provider.key_configured.hash(&mut hasher);+ provider.managed_available.hash(&mut hasher);+ matches!(provider.route, crate::config::SearchRoute::Managed).hash(&mut hasher);+ }+ hasher.finish()}pub fn spec(&self) -> &ToolSpec {@@ -185,14 +204,17 @@ impl Tool for TinySearchTool {args: Value,options: ToolCallOptions,) -> anyhow::Result<ToolResult> {- if self.is_exhausted() {+ let config = self.live_config().await?;+ // Re-read per call, so a key added or a provider switched mid-session+ // clears an earlier refusal instead of outliving it.+ let signature = self.provider_signature(&config);+ if self.is_exhausted_for(signature) {tracing::debug!(tool = %self.spec.name,- "[search][tool] refused: no provider answered earlier in this session"+ "[search][tool] refused: no provider answered under this configuration");return Ok(ToolResult::failed(SEARCH_EXHAUSTED_MESSAGE.to_string()));}- let config = self.live_config().await?;let subject = super::render::subject(&args);let max_results = args.get("max_results")@@ -230,12 +252,12 @@ impl Tool for TinySearchTool {}Err(error) => {if exhausts_providers(&error) {- self.mark_exhausted();+ self.mark_exhausted_for(signature);}tracing::warn!(tool = %self.spec.name,code = error_code(&error).unwrap_or("unclassified"),- exhausted = self.is_exhausted(),+ exhausted = self.is_exhausted_for(signature),"[search][tool] failed");Ok(if exhausts_providers(&error) {@@ -267,17 +289,9 @@ pub fn build_search_tools(config: &Config) -> Vec<Box<dyn Tool>> {"[search][tool] registered search tools");let shared = Arc::new(config.clone());- let exhausted = Arc::new(AtomicBool::new(false));specs.into_iter()- .map(|spec| {- Box::new(TinySearchTool {- spec,- config: Some(shared.clone()),- exposure: ToolExposure::Direct,- exhausted: exhausted.clone(),- }) as Box<dyn Tool>- })+ .map(|spec| Box::new(TinySearchTool::new(shared.clone(), spec)) as Box<dyn Tool>).collect()}diff --git a/crates/openhuman-core/src/search/tools_tests.rs b/crates/openhuman-core/src/search/tools_tests.rsindex 417f71f71a..0bd9987051 100644--- a/crates/openhuman-core/src/search/tools_tests.rs+++ b/crates/openhuman-core/src/search/tools_tests.rs@@ -113,15 +113,22 @@ fn standard_privacy_mode_allows_search_tool_dispatch() {// was what the hidden test rejected.// ----------------------------------------------------------------------------fn role_specs() -> Vec<ToolSpec> {- ["web_search_tool", "web_answer_tool", "web_contents_tool"]+fn spec(name: &str) -> ToolSpec {+ ToolSpec {+ name: name.to_string(),+ description: "search".into(),+ parameters: serde_json::json!({"type": "object"}),+ }+}++/// A config whose managed providers are selected but whose direct keys are+/// absent, which is the shape that offers the tools and cannot serve them.+fn nothing_usable_config() -> Config {+ let mut config = Config::default();+ config.search.providers = [("brave".to_string(), SearchProviderSettings::direct())].into_iter()- .map(|name| ToolSpec {- name: name.to_string(),- description: "search".into(),- parameters: serde_json::json!({"type": "object"}),- })- .collect()+ .collect();+ config}#[test]@@ -134,7 +141,7 @@ fn the_unavailable_verdict_tells_the_model_to_stop_rather_than_wait() {);assert!(!message.contains("right now"),- "nothing in the session will change the answer, so it must not read as a retry hint: {message}"+ "nothing the model can do will change the answer, so it must not read as a retry hint: {message}");}@@ -154,37 +161,64 @@ fn only_the_exhaustion_verdict_is_treated_as_final() {}#[test]-fn a_session_starts_unlatched() {- for tool in TinySearchTool::recorded_batch(role_specs()) {- assert!(!tool.is_exhausted(), "{} started latched", tool.name());- }+fn a_tool_starts_with_nothing_recorded() {+ let config = nothing_usable_config();+ let tool = TinySearchTool::recorded(spec("web_search_tool"));++ assert!(!tool.is_exhausted_for(tool.provider_signature(&config)));}#[test]-fn one_role_finding_nothing_settles_it_for_the_others() {- // The three roles are declarations over the same providers, so dropping- // only the role that failed would still leave two tools that cannot work.- let tools = TinySearchTool::recorded_batch(role_specs());-- tools[0].mark_exhausted();-- for tool in &tools {- assert!(- tool.is_exhausted(),- "{} did not share the latch",- tool.name()- );- }+fn the_refusal_answers_only_for_the_configuration_that_failed() {+ // The contract this module documents is that every call re-reads the live+ // config, so a provider or login change is honoured without rebuilding the+ // session. A refusal that outlived a config change would break it: the user+ // adds a key and search stays dead until the thread is abandoned.+ let dead = nothing_usable_config();+ let tool = TinySearchTool::recorded(spec("web_search_tool"));+ let dead_signature = tool.provider_signature(&dead);++ tool.mark_exhausted_for(dead_signature);+ assert!(tool.is_exhausted_for(dead_signature));++ let mut fixed = dead.clone();+ fixed.search.brave.api_key = Some("added-after-the-failure".into());++ assert_ne!(+ tool.provider_signature(&fixed),+ dead_signature,+ "adding a provider key must change what the tool sees"+ );+ assert!(+ !tool.is_exhausted_for(tool.provider_signature(&fixed)),+ "adding a key must clear the refusal"+ );}-#[tokio::test]-async fn a_latched_tool_refuses_before_reaching_the_module() {- // The refusal is returned without loading config or calling the module, so- // a dead search costs the session one round trip rather than one per call.- let tools = TinySearchTool::recorded_batch(role_specs());- tools[1].mark_exhausted();+#[test]+fn one_tools_dead_providers_do_not_answer_for_another() {+ // `build_search_tools` can offer provider-specific tools beside the role+ // tools, and the roles draw on different provider orders, so a shared+ // verdict would let one provider's outage disable unrelated ones.+ let config = nothing_usable_config();+ let failed = TinySearchTool::recorded(spec("web_search_tool"));+ let other = TinySearchTool::recorded(spec("web_answer_tool"));- let result = tools[0]+ failed.mark_exhausted_for(failed.provider_signature(&config));++ assert!(!other.is_exhausted_for(other.provider_signature(&config)));+}++#[tokio::test]+async fn a_refused_call_does_not_reach_the_module() {+ // The second call under the same configuration is answered from the+ // recorded verdict, so a dead search costs one module round trip per+ // configuration rather than one per call.+ let config = nothing_usable_config();+ let tool = TinySearchTool::new(std::sync::Arc::new(config.clone()), spec("web_search_tool"));+ tool.mark_exhausted_for(tool.provider_signature(&config));++ let result = tool.execute(serde_json::json!({"query": "anything"})).await.expect("a refusal is a reported failure, not a tool error");@@ -193,7 +227,7 @@ async fn a_latched_tool_refuses_before_reaching_the_module() {assert_eq!(result.error_kind,Some(tinytools::ToolErrorKind::Failed),- "the failure is permanent, so it must not be tagged retryable"+ "the failure is permanent for this configuration, so it must not be tagged retryable");assert!(result.text().contains("Do not call it again"),