Skip to content

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

refactor(core): update reasoning config mapping to tinyinference

Steven Enamakela98d46cf3f5a
diff --git a/crates/openhuman-core/src/agent/tinyagents/thread_reasoning.rs b/crates/openhuman-core/src/agent/tinyagents/thread_reasoning.rs
new file mode 100644
index 0000000000..90aaf92ba9
--- /dev/null
+++ b/crates/openhuman-core/src/agent/tinyagents/thread_reasoning.rs
@@ -0,0 +1,57 @@
+//! OpenHuman's adapter for TinyAgents' caller-owned thread reasoning store.
+
+use std::sync::Arc;
+
+use tinyagents_harness::ids::ThreadId;
+use tinyagents_harness::thread_reasoning::ThreadReasoningStore;
+use tinyinference_llm::model::ReasoningEffort;
+
+use crate::core::runtime::DispatchCtx;
+
+/// The reasoning store belongs to this dispatch context/profile. Context
+/// clones share the slot; separately derived profiles remain isolated.
+pub(crate) fn store(ctx: &DispatchCtx) -> Arc<ThreadReasoningStore> {
+ crate::core::runtime::slot_for::<ThreadReasoningStore>(ctx)
+}
+
+/// Applies a chat request's optional reasoning choice, preserving the wire
+/// error text used by existing clients for an unrecognized effort.
+pub(crate) fn apply_requested_effort(
+ ctx: &DispatchCtx,
+ thread_id: &str,
+ raw: Option<&str>,
+) -> Result<(), String> {
+ let Some(raw) = raw else {
+ return Ok(());
+ };
+ let trimmed = raw.trim();
+ let requested = if trimmed.is_empty()
+ || trimmed.eq_ignore_ascii_case("default")
+ || trimmed.eq_ignore_ascii_case("auto")
+ {
+ None
+ } else {
+ Some(ReasoningEffort::parse(trimmed)
+ .ok_or_else(|| format!("unknown reasoning_effort '{trimmed}'"))?)
+ };
+
+ let thread_id = ThreadId::new(thread_id);
+ let store = store(ctx);
+ let previous = match requested {
+ Some(effort) => store.set_effort(&thread_id, effort),
+ None => store.clear(&thread_id),
+ };
+ if previous != requested {
+ log::info!(
+ "[agent][reasoning] thread reasoning effort changed thread_id={} {:?} -> {:?}",
+ thread_id.as_str(),
+ previous.map(ReasoningEffort::as_str),
+ requested.map(ReasoningEffort::as_str)
+ );
+ }
+ Ok(())
+}
+
+#[cfg(test)]
+#[path = "thread_reasoning_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/inference/reasoning.rs b/crates/openhuman-core/src/inference/reasoning.rs
index db4385504a..3940d15043 100644
--- a/crates/openhuman-core/src/inference/reasoning.rs
+++ b/crates/openhuman-core/src/inference/reasoning.rs
@@ -1,198 +1,15 @@
-//! The user's reasoning ("thinking") choice for an agent turn, resolved into
-//! the provider-neutral [`ReasoningConfig`] the harness attaches to every
-//! model request (`RunPolicy::default_reasoning`).
+//! Map effective OpenHuman runtime reasoning settings to TinyInference config.
//!
-//! 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. the turn model's own level, `runtime.reasoning_effort_by_model`;
-//! 3. `runtime.reasoning_effort` in the session's effective config;
-//! 4. `runtime.reasoning_enabled = false`, meaning "no reasoning".
-//!
-//! Nothing set means the provider keeps its own default.
-
-use std::collections::HashMap;
-use std::sync::Arc;
+//! Per-model settings take precedence over the global setting, which takes
+//! precedence over `reasoning_enabled = false`. A thread's explicit choice is
+//! applied by the TinyAgents host adapter before this mapping is consulted.
-use parking_lot::Mutex;
use tinyinference_llm::model::{ReasoningConfig, ReasoningEffort};
use crate::config::Config;
-use crate::core::runtime::DispatchCtx;
-
-type ThreadEfforts = Mutex<HashMap<String, ReasoningEffort>>;
-
-fn thread_efforts(ctx: &DispatchCtx) -> Arc<ThreadEfforts> {
- crate::core::runtime::slot_for::<ThreadEfforts>(ctx)
-}
-
-/// The effort a thread's user picked, if any.
-pub(crate) fn thread_effort(ctx: &DispatchCtx, thread_id: &str) -> Option<ReasoningEffort> {
- thread_efforts(ctx).lock().get(thread_id).copied()
-}
-
-/// Records (`Some`) or clears (`None`) a thread's chosen effort.
-pub(crate) fn set_thread_effort(
- ctx: &DispatchCtx,
- thread_id: &str,
- effort: Option<ReasoningEffort>,
-) {
- let efforts = thread_efforts(ctx);
- let mut map = 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(
- ctx: &DispatchCtx,
- 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(ctx, thread_id, None);
- return Ok(());
- }
- let effort = parse_reasoning_effort(trimmed)
- .ok_or_else(|| format!("unknown reasoning_effort '{trimmed}'"))?;
- set_thread_effort(ctx, thread_id, Some(effort));
- Ok(())
-}
-
-/// The reasoning config for the turn `ctx` is about to run, or `None` for the
-/// provider default.
-///
-/// Only a root turn (spawn depth 0) follows the user's thinking level; a
-/// sub-agent runs at its provider default, since the user picked a level for
-/// the conversation, not for every delegated helper. `config` is the session
-/// config a hosted root turn carries (`OpenHumanHostBase::config`).
-///
-/// `max_output_tokens` is the turn's per-call output cap. When reasoning is
-/// on and the turn is capped, the config also carries a thinking budget
-/// ([`with_output_room`]) so the visible reply always has room left.
-pub(crate) fn turn_reasoning_for(
- ctx: &crate::agent::tinyagents::host::OpenHumanRunContext,
- config: Option<&Config>,
- max_output_tokens: Option<u32>,
-) -> Option<ReasoningConfig> {
- if ctx.spawn_depth > 0 {
- return None;
- }
- let reasoning = turn_reasoning(&ctx.dispatch_ctx(), ctx.thread_id.as_deref(), config)
- .map(|reasoning| with_output_room(reasoning, max_output_tokens));
- if let Some(reasoning) = reasoning.as_ref() {
- log::debug!(
- "[agent][reasoning] turn reasoning effort={:?} budget_tokens={:?} thread_id={:?}",
- reasoning.effort,
- reasoning.budget_tokens,
- ctx.thread_id
- );
- }
- reasoning
-}
-
-/// The reasoning config one agent turn should request: the thread's own
-/// choice, else [`reasoning_for_config`].
-pub(crate) fn turn_reasoning(
- dispatch: &DispatchCtx,
- thread_id: Option<&str>,
- config: Option<&Config>,
-) -> Option<ReasoningConfig> {
- if let Some(effort) = thread_id.and_then(|thread_id| thread_effort(dispatch, thread_id)) {
- return Some(ReasoningConfig::effort(effort));
- }
- config.and_then(reasoning_for_config)
-}
-
-/// Share of a turn's output cap a reasoning model may spend thinking. The rest
-/// is left for the visible reply — in an agent turn usually a tool call whose
-/// arguments can be a whole file. Without a budget only an effort level is
-/// sent, and a high-effort model can think through the entire cap and return
-/// `finish_reason = length` with no tool call (#6951).
-const REASONING_BUDGET_PERCENT: u32 = 55;
-
-/// Smallest thinking budget worth sending. Anthropic models (reached through
-/// OpenRouter or the managed backend, which pass `reasoning.max_tokens` on as
-/// a thinking budget) reject budgets under 1024; below it the effort level
-/// alone is sent.
-const MIN_REASONING_BUDGET_TOKENS: u32 = 1024;
-/// Adds a thinking budget of [`REASONING_BUDGET_PERCENT`] of the turn's output
-/// cap to an *enabled* reasoning config that has none.
-///
-/// Left unchanged: reasoning switched off (`effort = none`), no effort chosen
-/// (a bare budget would turn reasoning on where the provider default is off),
-/// an uncapped turn, a config that already names a budget, and a cap too small
-/// for [`MIN_REASONING_BUDGET_TOKENS`]. Only OpenRouter and the managed
-/// backend consume the budget, sending it as `reasoning.max_tokens`. Every
-/// other route sends what it sent before: native Anthropic keeps adaptive
-/// thinking with the effort (the budget applies there only with no effort),
-/// and plain OpenAI-compatible endpoints and the Responses API drop it.
-pub(crate) fn with_output_room(
- mut reasoning: ReasoningConfig,
- max_output_tokens: Option<u32>,
-) -> ReasoningConfig {
- let enabled = matches!(reasoning.effort, Some(effort) if effort != ReasoningEffort::None);
- if !enabled || reasoning.budget_tokens.is_some() {
- return reasoning;
- }
- let Some(cap) = max_output_tokens else {
- return reasoning;
- };
- let budget = (u64::from(cap) * u64::from(REASONING_BUDGET_PERCENT) / 100) as u32;
- if budget < MIN_REASONING_BUDGET_TOKENS {
- log::debug!(
- "[agent][reasoning] output cap {cap} too small for a thinking budget; effort only"
- );
- return reasoning;
- }
- reasoning.budget_tokens = Some(budget);
- reasoning
-}
-
-/// 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> {
- match raw.trim().to_ascii_lowercase().as_str() {
- "none" | "off" | "disabled" => Some(ReasoningEffort::None),
- "minimal" => Some(ReasoningEffort::Minimal),
- "low" => Some(ReasoningEffort::Low),
- "medium" | "med" => Some(ReasoningEffort::Medium),
- "high" => Some(ReasoningEffort::High),
- "xhigh" | "max" | "maximum" => Some(ReasoningEffort::XHigh),
- _ => None,
- }
-}
-
-/// The reasoning config an agent turn on `config` should request, or `None`
-/// to leave the provider default alone.
-///
-/// The turn's model's own level (`runtime.reasoning_effort_by_model`, keyed by
-/// `default_model`, which a chat's `model_override` has already replaced in
-/// the session config) outranks the global `runtime.reasoning_effort`.
+/// The config-level reasoning choice for the selected model, or `None` to
+/// leave reasoning to the provider.
pub(crate) fn reasoning_for_config(config: &Config) -> Option<ReasoningConfig> {
let per_model = config
.default_model
@@ -203,7 +20,7 @@ pub(crate) fn reasoning_for_config(config: &Config) -> Option<ReasoningConfig> {
.map(|raw| raw.trim())
.filter(|raw| !raw.is_empty())
{
- return match parse_reasoning_effort(raw) {
+ return match ReasoningEffort::parse(raw) {
Some(effort) => Some(ReasoningConfig::effort(effort)),
None => {
log::warn!(
@@ -220,6 +37,40 @@ pub(crate) fn reasoning_for_config(config: &Config) -> Option<ReasoningConfig> {
None
}
+/// Reasoning configuration for one root turn. The store snapshots an explicit
+/// thread choice once; subsequent updates apply to later turns only.
+pub(crate) fn turn_reasoning_for(
+ ctx: &crate::agent::tinyagents::host::OpenHumanRunContext,
+ config: Option<&Config>,
+ max_output_tokens: Option<u32>,
+) -> Option<ReasoningConfig> {
+ if ctx.spawn_depth > 0 {
+ return None;
+ }
+
+ let dispatch = ctx.dispatch_ctx();
+ let thread_id = ctx.thread_id.as_deref();
+ let reasoning = crate::agent::tinyagents::thread_reasoning::store(&dispatch)
+ .reasoning_for_turn(
+ thread_id.map(tinyagents_harness::ids::ThreadId::new).as_ref(),
+ max_output_tokens,
+ )
+ .or_else(|| {
+ reasoning_for_config(config?)
+ .map(|reasoning| reasoning.with_output_room(max_output_tokens))
+ });
+
+ if let Some(reasoning) = reasoning.as_ref() {
+ log::debug!(
+ "[agent][reasoning] turn reasoning effort={:?} budget_tokens={:?} thread_id={:?}",
+ reasoning.effort,
+ reasoning.budget_tokens,
+ ctx.thread_id
+ );
+ }
+ reasoning
+}
+
#[cfg(test)]
#[path = "reasoning_tests.rs"]
mod tests;