Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(agent): handle missing reasoning agent gracefully

Steven Enamakel27665ba03177
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session.rs b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
index 910fd40b45..a90d31d25c 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
@@ -744,12 +744,12 @@ impl OpenHumanTurnPrelude {
self.tool_dispatcher.tool_call_format(),
)
.harness_dispatcher();
- // The thread's reasoning choice rides the effective config this session
- // was built from (web_chat writes a per-turn override there).
- run_context.reasoning = self
- .runtime_config
- .as_deref()
- .and_then(crate::agent::tinyagents::reasoning_for_config);
+ // The thread's reasoning choice is read per turn, so changing it keeps
+ // this warm session; the effective config supplies the default.
+ run_context.reasoning = crate::agent::tinyagents::turn_reasoning(
+ self.thread_id.as_deref(),
+ self.runtime_config.as_deref(),
+ );
if let Some(reasoning) = run_context.reasoning.as_ref() {
log::debug!(
"[agent][reasoning] turn reasoning effort={:?} budget_tokens={:?} agent={}",
diff --git a/crates/openhuman-core/src/agent/tinyagents/mod.rs b/crates/openhuman-core/src/agent/tinyagents/mod.rs
index eea335aba9..4c3d25b38f 100644
--- a/crates/openhuman-core/src/agent/tinyagents/mod.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/mod.rs
@@ -80,7 +80,9 @@ pub(crate) use turn_outcome::{
HaltSummarySlot, TinyagentsTurnOutcome, ToolCallOutcome, ToolOutcomeSink,
};
#[cfg(test)]
-pub(crate) use reasoning::{parse_reasoning_effort, reasoning_for_config};
+pub(crate) use reasoning::{
+ apply_requested_effort, parse_reasoning_effort, reasoning_for_config, turn_reasoning,
+};
pub(crate) use turn_policy::is_subagent_spawn_or_delegate_tool;
pub(crate) use turn_policy::{agent_turn_wall_clock_ms, ToolPolicyEnforcement};
pub(crate) use turn_runner::{run_root_turn_via_hosted_agent, run_turn_via_tinyagents_shared};
diff --git a/crates/openhuman-core/src/agent/tinyagents/reasoning.rs b/crates/openhuman-core/src/agent/tinyagents/reasoning.rs
index 4cf3e01ad9..0eef11fd66 100644
--- a/crates/openhuman-core/src/agent/tinyagents/reasoning.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/reasoning.rs
@@ -2,15 +2,86 @@
//! the provider-neutral [`ReasoningConfig`] the harness attaches to every
//! model request (`RunPolicy::default_reasoning`).
//!
-//! The source is the turn's effective config: `runtime.reasoning_effort`
-//! (a chat thread's per-turn choice is written there by `web_chat`), falling
-//! back to `runtime.reasoning_enabled = false` meaning "no reasoning". Nothing
-//! set means the provider keeps its own default.
+//! Precedence, highest first:
+//!
+//! 1. the thread's own choice, recorded by `channel_web_chat`'s
+//! `reasoning_effort` param ([`apply_requested_effort`]) — read at turn
+//! start, so changing it never evicts the thread's warm session;
+//! 2. `runtime.reasoning_effort` in the session's effective config;
+//! 3. `runtime.reasoning_enabled = false`, meaning "no reasoning".
+//!
+//! Nothing set means the provider keeps its own default.
+
+use std::collections::HashMap;
+use std::sync::OnceLock;
+use parking_lot::Mutex;
use tinyinference_llm::model::{ReasoningConfig, ReasoningEffort};
use crate::config::Config;
+fn thread_efforts() -> &'static Mutex<HashMap<String, ReasoningEffort>> {
+ static EFFORTS: OnceLock<Mutex<HashMap<String, ReasoningEffort>>> = OnceLock::new();
+ EFFORTS.get_or_init(|| Mutex::new(HashMap::new()))
+}
+
+/// The effort a thread's user picked, if any.
+pub(crate) fn thread_effort(thread_id: &str) -> Option<ReasoningEffort> {
+ thread_efforts().lock().get(thread_id).copied()
+}
+
+/// Records (`Some`) or clears (`None`) a thread's chosen effort.
+pub(crate) fn set_thread_effort(thread_id: &str, effort: Option<ReasoningEffort>) {
+ let mut map = thread_efforts().lock();
+ let previous = match effort {
+ Some(effort) => map.insert(thread_id.to_string(), effort),
+ None => map.remove(thread_id),
+ };
+ if previous != effort {
+ log::info!(
+ "[agent][reasoning] thread reasoning effort changed thread_id={thread_id} {:?} -> {:?}",
+ previous.map(ReasoningEffort::as_str),
+ effort.map(ReasoningEffort::as_str)
+ );
+ }
+}
+
+/// Applies a chat request's `reasoning_effort` param to its thread.
+///
+/// Absent leaves the thread's choice as it was (older clients and the socket
+/// path never send it); `""`/`default`/`auto` clears it back to config; any
+/// other value must parse, otherwise the request is rejected so a typo'd
+/// client cannot silently run at the wrong level.
+pub(crate) fn apply_requested_effort(thread_id: &str, raw: Option<&str>) -> Result<(), String> {
+ let Some(raw) = raw else {
+ return Ok(());
+ };
+ let trimmed = raw.trim();
+ if trimmed.is_empty()
+ || trimmed.eq_ignore_ascii_case("default")
+ || trimmed.eq_ignore_ascii_case("auto")
+ {
+ set_thread_effort(thread_id, None);
+ return Ok(());
+ }
+ let effort = parse_reasoning_effort(trimmed)
+ .ok_or_else(|| format!("unknown reasoning_effort '{trimmed}'"))?;
+ set_thread_effort(thread_id, Some(effort));
+ Ok(())
+}
+
+/// The reasoning config one agent turn should request: the thread's own
+/// choice, else [`reasoning_for_config`].
+pub(crate) fn turn_reasoning(
+ thread_id: Option<&str>,
+ config: Option<&Config>,
+) -> Option<ReasoningConfig> {
+ if let Some(effort) = thread_id.and_then(thread_effort) {
+ return Some(ReasoningConfig::effort(effort));
+ }
+ config.and_then(reasoning_for_config)
+}
+
/// Parses a user-facing effort name. Accepts the wire tokens plus the aliases
/// `off`/`disabled` (none) and `max`/`maximum` (xhigh), case-insensitively.
pub(crate) fn parse_reasoning_effort(raw: &str) -> Option<ReasoningEffort> {
diff --git a/crates/openhuman-core/src/agent/tinyagents/reasoning_tests.rs b/crates/openhuman-core/src/agent/tinyagents/reasoning_tests.rs
index f8258ba891..4ffffc998c 100644
--- a/crates/openhuman-core/src/agent/tinyagents/reasoning_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/reasoning_tests.rs
@@ -52,3 +52,41 @@ fn unknown_effort_falls_back_to_the_provider_default() {
config.runtime.reasoning_effort = Some("turbo".into());
assert_eq!(reasoning_for_config(&config), None);
}
+
+#[test]
+fn thread_choice_wins_over_config_and_clears_back_to_it() {
+ let thread = "reasoning-test-thread-precedence";
+ let mut config = Config::default();
+ config.runtime.reasoning_effort = Some("low".into());
+
+ apply_requested_effort(thread, Some("high")).expect("valid effort");
+ assert_eq!(
+ turn_reasoning(Some(thread), Some(&config)),
+ Some(ReasoningConfig::effort(ReasoningEffort::High))
+ );
+
+ // Absent leaves the thread's choice untouched.
+ apply_requested_effort(thread, None).expect("absent is fine");
+ assert_eq!(thread_effort(thread), Some(ReasoningEffort::High));
+
+ apply_requested_effort(thread, Some("default")).expect("clear");
+ assert_eq!(thread_effort(thread), None);
+ assert_eq!(
+ turn_reasoning(Some(thread), Some(&config)),
+ Some(ReasoningConfig::effort(ReasoningEffort::Low))
+ );
+}
+
+#[test]
+fn unknown_requested_effort_is_rejected_and_keeps_the_prior_choice() {
+ let thread = "reasoning-test-thread-reject";
+ apply_requested_effort(thread, Some("off")).expect("valid effort");
+ assert!(apply_requested_effort(thread, Some("turbo")).is_err());
+ assert_eq!(thread_effort(thread), Some(ReasoningEffort::None));
+ set_thread_effort(thread, None);
+}
+
+#[test]
+fn turn_without_thread_or_config_has_no_reasoning() {
+ assert_eq!(turn_reasoning(None, None), None);
+}