Skip to content

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

refactor(agent): extract shared turn idle policy into turn_policy

Steven Enamakele724055cd440
diff --git a/crates/openhuman-core/src/agent/mod.rs b/crates/openhuman-core/src/agent/mod.rs
index cd56aed5a3..54c0476fb6 100644
--- a/crates/openhuman-core/src/agent/mod.rs
+++ b/crates/openhuman-core/src/agent/mod.rs
@@ -80,15 +80,17 @@ pub mod tools;
pub mod triage;
/// Explicit per-turn host environment handed to the session an entry point runs.
pub mod turn_env;
+/// Trusted host execution shared by web, native channels and other inbound
+/// surfaces. Presentation and delivery remain adapters at each surface.
+pub(crate) mod turn_host;
/// Turn-origin task-local — explicit trust/routing label scoped by every
/// entry point that invokes the agent (web chat, channel runtime, cron,
/// background jobs, CLI). Read by the approval gate to make
/// origin-aware decisions rather than inferring trust from the absence of
/// a chat approval context.
pub mod turn_origin;
-/// Trusted host execution shared by web, native channels and other inbound
-/// surfaces. Presentation and delivery remain adapters at each surface.
-pub(crate) mod turn_host;
+/// Shared idle and backstop policy for top-level agent turns.
+pub(crate) mod turn_policy;
/// How a turn that reached the completion path was stopped early (breaker,
/// deadline wind-down, iteration cap), carried to traces and parent tools.
pub mod turn_projection;
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
index f406884aea..00d1c306b7 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
@@ -50,10 +50,10 @@ use persistence::{persist_delivery_reply, persist_then_announce};
use super::background_completions;
use super::busy_guard::is_busy;
#[cfg(test)]
-use super::busy_guard::{TurnBusy, busy, clear_busy_for_thread};
+use super::busy_guard::{busy, clear_busy_for_thread, TurnBusy};
use super::completion_notice::build_undelivered_notice;
use super::completion_owners;
-use super::delivery_drain::{DrainFence, DrainOutcome, RECOVERY_BUSY_POLL, run_drain};
+use super::delivery_drain::{run_drain, DrainFence, DrainOutcome, RECOVERY_BUSY_POLL};
use crate::core::runtime::CoreContext;
/// Coalesce completions landing within this window into one delivery turn.
diff --git a/crates/openhuman-core/src/agent/turn_policy.rs b/crates/openhuman-core/src/agent/turn_policy.rs
new file mode 100644
index 0000000000..0d18cb07f1
--- /dev/null
+++ b/crates/openhuman-core/src/agent/turn_policy.rs
@@ -0,0 +1,32 @@
+//! Shared wall-clock policy for a user-facing agent turn.
+//!
+//! The idle guard catches turns that stop making progress; the independent
+//! backstop lets a turn remain active while it is still producing progress.
+
+use std::time::Duration;
+
+/// Default silence window for a top-level user turn.
+pub(crate) const DEFAULT_IDLE_SECS: u64 = 900;
+
+/// Existing environment override retained for compatibility with deployments.
+pub(crate) const IDLE_ENV: &str = "OPENHUMAN_WEB_TURN_IDLE_SECS";
+
+const IDLE_DISABLED: Duration = Duration::from_secs(10 * 365 * 24 * 3600);
+
+/// Parse an idle window; zero disables the guard and invalid values use the
+/// product default.
+pub(crate) fn parse_idle_window(value: Option<&str>) -> Duration {
+ let secs = value
+ .and_then(|value| value.trim().parse::<u64>().ok())
+ .unwrap_or(DEFAULT_IDLE_SECS);
+ if secs == 0 {
+ IDLE_DISABLED
+ } else {
+ Duration::from_secs(secs)
+ }
+}
+
+/// The process-wide idle window for top-level turns.
+pub(crate) fn configured_idle_window() -> Duration {
+ parse_idle_window(std::env::var(IDLE_ENV).ok().as_deref())
+}
diff --git a/crates/openhuman-core/src/web_chat/turn_activity.rs b/crates/openhuman-core/src/web_chat/turn_activity.rs
index 3eec2deb94..c76ef87859 100644
--- a/crates/openhuman-core/src/web_chat/turn_activity.rs
+++ b/crates/openhuman-core/src/web_chat/turn_activity.rs
@@ -14,32 +14,10 @@ use std::sync::{Arc, Mutex};
use tinyagents_harness::progress::AgentProgress;
-/// Default idle window for a web-chat turn, in seconds.
-pub(crate) const DEFAULT_TURN_IDLE_SECS: u64 = 900;
-
-/// Environment override for the idle window, in seconds; `0` disables it.
-pub(crate) const TURN_IDLE_ENV: &str = "OPENHUMAN_WEB_TURN_IDLE_SECS";
-
-/// A window long enough to stand for "never" where the runner needs a value.
-const IDLE_DISABLED: std::time::Duration = std::time::Duration::from_secs(10 * 365 * 24 * 3600);
-
-/// Pure core of [`configured_idle_window`]: an absent or unparseable value
-/// falls back to [`DEFAULT_TURN_IDLE_SECS`]; `0` disables the idle guard.
-pub(crate) fn parse_idle_window(env_value: Option<&str>) -> std::time::Duration {
- let secs = env_value
- .and_then(|v| v.trim().parse::<u64>().ok())
- .unwrap_or(DEFAULT_TURN_IDLE_SECS);
- if secs > 0 {
- std::time::Duration::from_secs(secs)
- } else {
- IDLE_DISABLED
- }
-}
-
-/// The idle window in force for this process (env [`TURN_IDLE_ENV`]).
-pub(crate) fn configured_idle_window() -> std::time::Duration {
- parse_idle_window(std::env::var(TURN_IDLE_ENV).ok().as_deref())
-}
+pub(crate) use crate::agent::turn_policy::{
+ configured_idle_window, parse_idle_window, DEFAULT_IDLE_SECS as DEFAULT_TURN_IDLE_SECS,
+ IDLE_ENV as TURN_IDLE_ENV,
+};
#[derive(Debug, Default)]
struct State {