Skip to content

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

feat(agent): scope tool snapshots to the active turn

Steven Enamakelac5cadd2c9da
diff --git a/crates/openhuman-core/src/agent/mod.rs b/crates/openhuman-core/src/agent/mod.rs
index bd517dc574..ce1abb1232 100644
--- a/crates/openhuman-core/src/agent/mod.rs
+++ b/crates/openhuman-core/src/agent/mod.rs
@@ -78,12 +78,13 @@ pub mod subagent_host;
pub mod tinyagents;
pub mod todos;
pub mod tool_policy;
-pub mod tool_snapshot_scope;
pub mod tools;
pub mod triage;
/// Wall-clock deadline of one top-level turn: the outer backstop and the
/// harness wind-down / hard-stop points derived from it.
pub mod turn_deadline;
+/// Explicit per-turn host environment handed to the session an entry point runs.
+pub mod turn_env;
/// 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
diff --git a/crates/openhuman-core/src/agent/session_store/mod_tests.rs b/crates/openhuman-core/src/agent/session_store/mod_tests.rs
index 6c09f57b9f..2b41c5903e 100644
--- a/crates/openhuman-core/src/agent/session_store/mod_tests.rs
+++ b/crates/openhuman-core/src/agent/session_store/mod_tests.rs
@@ -1,6 +1,17 @@
use super::*;
use tinyagents_session::port::InMemorySessionStores;
+/// Runs `future` under a derived context whose host overrides carry `provider`
+/// as the agent's session store, the explicit per-context seam `installed()`
+/// consults ahead of the process-wide provider.
+async fn scope<F: std::future::Future>(
+ provider: Arc<dyn SessionStoreProvider>,
+ future: F,
+) -> F::Output {
+ crate::core::runtime::CoreContext::scope(derived_with_store(None, None, provider), future)
+ .await
+}
+
#[tokio::test]
async fn a_scoped_provider_serves_only_its_own_task() {
let provider: Arc<dyn SessionStoreProvider> = Arc::new(InMemorySessionStores::new());
@@ -73,6 +84,22 @@ async fn without_a_store_transcripts_are_workspace_files() {
}
fn derived(profile: Option<&str>, agent: Option<&str>) -> Arc<crate::core::runtime::CoreContext> {
+ derived_overlay(profile, agent, None)
+}
+
+fn derived_with_store(
+ profile: Option<&str>,
+ agent: Option<&str>,
+ provider: Arc<dyn SessionStoreProvider>,
+) -> Arc<crate::core::runtime::CoreContext> {
+ derived_overlay(profile, agent, Some(provider))
+}
+
+fn derived_overlay(
+ profile: Option<&str>,
+ agent: Option<&str>,
+ provider: Option<Arc<dyn SessionStoreProvider>>,
+) -> Arc<crate::core::runtime::CoreContext> {
use crate::core::runtime::{ContextOverlay, CoreContext, DomainSet};
let mut overlay = ContextOverlay::new(
crate::config::Config::default(),
@@ -85,6 +112,13 @@ fn derived(profile: Option<&str>, agent: Option<&str>) -> Arc<crate::core::runti
if let Some(agent) = agent {
overlay = overlay.session_agent(agent);
}
+ if let Some(provider) = provider {
+ let overrides = crate::agent::host_overrides::HostOverrides {
+ session_store: Some(provider),
+ ..Default::default()
+ };
+ overlay = overlay.host_overrides(Arc::new(overrides));
+ }
CoreContext::for_test(DomainSet::full(), None).derive_with(overlay)
}
@@ -124,12 +158,10 @@ async fn two_profiles_default_agents_keep_separate_session_keys_and_stores() {
let provider: Arc<dyn SessionStoreProvider> = Arc::new(InMemorySessionStores::new());
let stores_of = |profile: &'static str| {
let provider = provider.clone();
- scope(provider, async move {
- crate::core::runtime::CoreContext::scope(derived(Some(profile), None), async {
- current().map(|stores| stores.transcripts.destination_key())
- })
- .await
- })
+ crate::core::runtime::CoreContext::scope(
+ derived_with_store(Some(profile), None, provider),
+ async { current().map(|stores| stores.transcripts.destination_key()) },
+ )
};
let (alice, bob) = (stores_of("alice").await, stores_of("bob").await);
assert_ne!(alice, bob);
diff --git a/crates/openhuman-core/src/agent/tool_snapshot_scope.rs b/crates/openhuman-core/src/agent/tool_snapshot_scope.rs
deleted file mode 100644
index 497d7ad5b1..0000000000
--- a/crates/openhuman-core/src/agent/tool_snapshot_scope.rs
+++ /dev/null
@@ -1,15 +0,0 @@
-//! Explicit one-turn tool authority over restored transcript declarations.
-
-use std::future::Future;
-tokio::task_local! { static FRESH: (); }
-
-/// Run a turn with the current host belt as authoritative. Historical tool
-/// declarations remain in the transcript but cannot restore a revoked tool.
-/// Host-spawned dispatch tasks must explicitly carry this scope.
-pub async fn with_fresh_snapshot<F: Future>(future: F) -> F::Output {
- FRESH.scope((), future).await
-}
-
-pub(crate) fn retain_recorded_tools() -> bool {
- FRESH.try_with(|_| ()).is_err()
-}
diff --git a/crates/openhuman-core/src/agent/turn_env.rs b/crates/openhuman-core/src/agent/turn_env.rs
new file mode 100644
index 0000000000..3c35524223
--- /dev/null
+++ b/crates/openhuman-core/src/agent/turn_env.rs
@@ -0,0 +1,59 @@
+//! Explicit host environment for one agent turn.
+//!
+//! An entry point that drives a turn on behalf of a host (the embed facade, the
+//! web channel, a scheduler) owns a handful of per-turn values the harness has
+//! to honour: the shared model-spend budget, a narrower delegation depth, the
+//! caller's cancellation, the wall-clock backstop, hook callbacks, and whether
+//! the host's tool belt is authoritative over a restored transcript. They used
+//! to ride ambient task-locals that every layer below re-read, which silently
+//! stopped working across a `tokio::spawn` and could not be seen from a
+//! signature. This value is handed to the session instead
+//! ([`OpenHumanSessionHost::set_host_env`](crate::agent::OpenHumanSessionHost::set_host_env));
+//! the session copies it onto the turn's explicit
+//! [`OpenHumanRunContext`](crate::agent::tinyagents::host::OpenHumanRunContext),
+//! whose `child()` hands it down the sub-agent tree.
+
+use tinyagents_harness::cancel::CancellationToken;
+use tinyagents_harness::turn_deadline::TurnDeadline;
+
+use crate::agent::hooks::HookScope;
+use crate::agent::tinyagents::budget::ModelBudget;
+
+/// Per-turn values an entry point hands to the session it runs.
+///
+/// Every field defaults to "nothing narrowed", so `HostTurnEnv::default()` is a
+/// turn exactly as it ran before any host policy existed.
+#[derive(Clone, Default)]
+pub struct HostTurnEnv {
+ /// Shared model-spend ledger and per-call bounds every model call of the
+ /// turn tree is charged against.
+ pub model_budget: Option<ModelBudget>,
+ /// A narrower model-directed delegation depth than the product ceiling.
+ pub spawn_depth_limit: Option<usize>,
+ /// The caller's cancellation, shared with every child run. `None` runs the
+ /// turn under a fresh token only the session itself can fire.
+ pub cancellation: Option<CancellationToken>,
+ /// The wall-clock backstop the harness winds down before.
+ pub turn_deadline: Option<TurnDeadline>,
+ /// A cap on real tool invocations, never wider than the product default.
+ pub tool_call_limit: Option<usize>,
+ /// Agent/turn hook callbacks added after the runtime's global ones.
+ pub hooks: HookScope,
+ /// The host's tool belt for this turn is authoritative: tool declarations
+ /// recorded in a restored transcript must not bring a revoked tool back.
+ pub fresh_tool_snapshot: bool,
+}
+
+impl std::fmt::Debug for HostTurnEnv {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("HostTurnEnv")
+ .field("model_budget", &self.model_budget.is_some())
+ .field("spawn_depth_limit", &self.spawn_depth_limit)
+ .field("cancellation", &self.cancellation.is_some())
+ .field("turn_deadline", &self.turn_deadline.is_some())
+ .field("tool_call_limit", &self.tool_call_limit)
+ .field("hooks_empty", &self.hooks.is_empty())
+ .field("fresh_tool_snapshot", &self.fresh_tool_snapshot)
+ .finish()
+ }
+}