Skip to content

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

fix: restore recorded Juice tools on resume

Steven Enamakel116e7e767551
diff --git a/crates/openhuman-core/src/agent/session_host/prelude_integrations.rs b/crates/openhuman-core/src/agent/session_host/prelude_integrations.rs
index 15de2e856f..3f187b7cf4 100644
--- a/crates/openhuman-core/src/agent/session_host/prelude_integrations.rs
+++ b/crates/openhuman-core/src/agent/session_host/prelude_integrations.rs
@@ -53,6 +53,15 @@ impl OpenHumanTurnPrelude {
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
mutable.recorded_integration_actions = actions;
+ mutable.recorded_tool_snapshot_adopted = true;
+ {
+ mutable.recorded_repl_tools = recorded
+ .specs()
+ .iter()
+ .filter(|spec| crate::inference::tokenjuice::is_repl_tool(&spec.name))
+ .cloned()
+ .collect();
+ }
// MCP tools are rebuilt from the tool cache every turn; all a resumed
// thread needs remembered is which names it was sent, so a tool it
// knew under the pre-readable hashed name keeps resolving.
@@ -120,6 +129,32 @@ impl OpenHumanTurnPrelude {
));
#[cfg(not(feature = "modules"))]
let _ = base;
+ {
+ let recorded_repl_tools = self
+ .mutable
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .recorded_repl_tools
+ .clone();
+ let live_names = base
+ .iter()
+ .chain(synthesized.iter())
+ .map(|tool| tool.name())
+ .collect::<std::collections::HashSet<_>>();
+ let missing = recorded_repl_tools
+ .into_iter()
+ .filter(|spec| !live_names.contains(spec.name.as_str()))
+ .collect::<Vec<_>>();
+ if !missing.is_empty() {
+ rebuilt.extend(
+ crate::inference::tokenjuice::repl_tools::repl_tools_for_recorded(
+ self.runtime_config.as_deref(),
+ &self.workspace_dir,
+ &missing,
+ ),
+ );
+ }
+ }
rebuilt
}
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 e51845e84a..78ca1a6278 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
@@ -168,6 +168,12 @@ struct OpenHumanTurnPreludeMutable {
/// the tinyagents session on resume. Rebuilt into deferred executors whenever
/// the live integrations list does not supply them (see `recorded_tools`).
recorded_integration_actions: Vec<tinytools::ToolSpec>,
+ /// TinyJuice REPL declarations restored by a resumed thread. These are
+ /// rebuilt independently of the current setting that creates new handles.
+ recorded_repl_tools: Vec<tinytools::ToolSpec>,
+ /// Distinguishes a new session from a resumed session whose recorded tool
+ /// snapshot simply had no TinyJuice declarations.
+ recorded_tool_snapshot_adopted: bool,
workflows: Vec<crate::skills::Workflow>,
composio_events: Option<tinybus::events::EventReceiver<crate::core::events::DomainEvent>>,
skill_events: Option<tinybus::events::EventReceiver<crate::core::events::DomainEvent>>,
@@ -369,10 +375,15 @@ impl OpenHumanTurnPrelude {
let Some(definition) = self.session_definition(&registry) else {
return Ok(());
};
- if definition.subagents.is_empty() {
+ let has_recorded_tool_snapshot = self
+ .mutable
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .recorded_tool_snapshot_adopted;
+ if definition.subagents.is_empty() && !has_recorded_tool_snapshot {
return Ok(());
}
- let (integrations, integrations_are_authoritative) = {
+ let (integrations, integrations_are_authoritative, recorded_tool_snapshot_adopted) = {
let mutable = self
.mutable
.lock()
@@ -380,6 +391,7 @@ impl OpenHumanTurnPrelude {
(
mutable.connected_integrations.clone(),
mutable.connected_integrations_authoritative,
+ mutable.recorded_tool_snapshot_adopted,
)
};
#[cfg(feature = "mcp")]
@@ -420,15 +432,56 @@ impl OpenHumanTurnPrelude {
&synthesized_names,
auto_include_new_synthesized_tools,
);
+ if recorded_tool_snapshot_adopted {
+ surface.visible_tool_names.extend(
+ self.mutable
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .recorded_repl_tools
+ .iter()
+ .map(|spec| spec.name.clone()),
+ );
+ }
// Preserve permanently attached tools when re-deriving the surface.
permanent::refresh_visibility(&mut surface, &synthesized);
- let specs = surface
+ let mut specs = surface
.durable_tool_specs
.iter()
.cloned()
.chain(synthesized.iter().map(|tool| Arc::new(tool.spec())))
.collect::<Vec<_>>();
+ {
+ let recorded = self
+ .mutable
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .recorded_repl_tools
+ .clone();
+ let recorded_names = recorded
+ .iter()
+ .map(|spec| spec.name.as_str())
+ .collect::<std::collections::HashSet<_>>();
+ if recorded_tool_snapshot_adopted {
+ specs.retain(|spec| {
+ !crate::inference::tokenjuice::is_repl_tool(&spec.name)
+ || recorded_names.contains(spec.name.as_str())
+ });
+ surface.visible_tool_names.retain(|name| {
+ !crate::inference::tokenjuice::is_repl_tool(name)
+ || recorded_names.contains(name.as_str())
+ });
+ }
+ let recorded = recorded
+ .iter()
+ .map(|spec| (spec.name.as_str(), spec))
+ .collect::<std::collections::HashMap<_, _>>();
+ for spec in &mut specs {
+ if let Some(frozen) = recorded.get(spec.name.as_str()) {
+ *spec = Arc::new((*frozen).clone());
+ }
+ }
+ }
let synthesized_tools = Arc::new(synthesized);
crate::tools::toolpacks::bind_synthesized_pack_registry(&surface.tools, &synthesized_tools);
let all_tools = surface
@@ -994,6 +1047,8 @@ impl OpenHumanSessionHost {
connected_integrations_authoritative: false,
integration_announcements_seeded: false,
recorded_integration_actions: Vec::new(),
+ recorded_repl_tools: Vec::new(),
+ recorded_tool_snapshot_adopted: false,
workflows: self.workflows.clone(),
composio_events: None,
skill_events: None,
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session_tests.rs b/crates/openhuman-core/src/agent/session_host/runtime_session_tests.rs
index a19e1d18ad..3b106637e1 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session_tests.rs
@@ -189,6 +189,231 @@ fn spec(name: &str) -> ToolSpec {
}
}
+#[tokio::test]
+async fn a_resumed_thread_keeps_recorded_juice_specs_and_executors_when_repl_is_off() {
+ use crate::inference::tokenjuice::{repl_tools_for, REPL_TOOL_NAMES};
+
+ crate::agent::harness::definition::AgentDefinitionRegistry::init_global_builtins()
+ .expect("builtin definitions");
+ let tmp = tempfile::tempdir().expect("tempdir");
+ let mut active_config = crate::config::Config {
+ workspace_dir: tmp.path().join("workspace"),
+ action_dir: tmp.path().join("workspace"),
+ config_path: tmp.path().join("config.toml"),
+ ..Default::default()
+ };
+ std::fs::create_dir_all(&active_config.workspace_dir).expect("workspace");
+ active_config.context.compaction_enabled = true;
+ active_config.tokenjuice.router_enabled = true;
+ active_config.tokenjuice.ccr_enabled = true;
+ active_config.tokenjuice.repl_handle_enabled = true;
+ let mut recorded_specs = repl_tools_for(&active_config)
+ .iter()
+ .map(|tool| tool.spec())
+ .collect::<Vec<_>>();
+ assert_eq!(
+ recorded_specs
+ .iter()
+ .map(|spec| spec.name.as_str())
+ .collect::<Vec<_>>(),
+ REPL_TOOL_NAMES
+ );
+ for spec in &mut recorded_specs {
+ spec.description = format!("frozen declaration for {}", spec.name);
+ spec.parameters = serde_json::json!({"type":"object","frozen":spec.name});
+ }
+ let recorded = ToolSnapshot::new(recorded_specs.clone()).expect("recorded tool snapshot");
+
+ // A fresh process has the same workspace but current policy no longer
+ // creates new handles. The resumed thread must still regain only the
+ // REPL tools it was sent, with their original declarations.
+ let mut current_config = crate::config::Config {
+ workspace_dir: active_config.workspace_dir.clone(),
+ action_dir: active_config.action_dir.clone(),
+ config_path: active_config.config_path.clone(),
+ modules: crate::config::schema::ModulesConfig {
+ enabled: false,
+ ..Default::default()
+ },
+ ..Default::default()
+ };
+ current_config.context.compaction_enabled = false;
+ current_config.tokenjuice.router_enabled = false;
+ current_config.tokenjuice.ccr_enabled = false;
+ current_config.tokenjuice.repl_handle_enabled = false;
+ assert!(repl_tools_for(&current_config).is_empty());
+ let mut host =
+ crate::agent::OpenHumanSessionHost::from_config_for_agent(&current_config, "summarizer")
+ .expect("summarizer session");
+ host.ensure_runtime_session().expect("runtime session");
+ let prelude = host
+ .runtime_state
+ .lock()
+ .expect("runtime state")
+ .prelude
+ .clone()
+ .expect("prelude");
+
+ prelude.adopt_recorded_tools(Some(&recorded));
+ assert_eq!(
+ prelude
+ .mutable
+ .lock()
+ .expect("prelude state")
+ .recorded_repl_tools
+ .len(),
+ recorded_specs.len()
+ );
+ prelude.refresh_turn_boundary(false).await.expect("refresh");
+ let surface_names = {
+ let surface = prelude.tool_surface.lock().expect("tool surface");
+ (
+ surface
+ .tools
+ .iter()
+ .map(|tool| tool.name().to_string())
+ .collect::<Vec<_>>(),
+ surface
+ .synthesized_tools
+ .iter()
+ .map(|tool| tool.name().to_string())
+ .collect::<Vec<_>>(),
+ surface
+ .visible_tool_specs
+ .iter()
+ .map(|spec| spec.name.clone())
+ .collect::<Vec<_>>(),
+ surface.visible_tool_names.clone(),
+ )
+ };
+ let prepared = prelude.prepare(false).await.expect("prepared surface");
+ let prepared = prepared.tools.expect("prepared tool snapshot");
+
+ for recorded_spec in &recorded_specs {
+ let prepared_spec = prepared
+ .specs()
+ .iter()
+ .find(|spec| spec.name == recorded_spec.name)
+ .unwrap_or_else(|| {
+ panic!(
+ "the resumed request lost {}; surface={surface_names:?}",
+ recorded_spec.name
+ )
+ });
+ assert_eq!(prepared_spec.description, recorded_spec.description);
+ assert_eq!(prepared_spec.parameters, recorded_spec.parameters);
+ let surface = prelude.tool_surface.lock().expect("tool surface");
+ assert!(
+ surface
+ .tools
+ .iter()
+ .chain(surface.synthesized_tools.iter())
+ .any(|tool| tool.name() == recorded_spec.name),
+ "the recorded declaration {} needs an executor",
+ recorded_spec.name
+ );
+ }
+ let unavailable = {
+ let surface = prelude.tool_surface.lock().expect("tool surface");
+ let tool = surface
+ .tools
+ .iter()
+ .chain(surface.synthesized_tools.iter())
+ .find(|tool| tool.name() == "juice_find")
+ .expect("recorded executor remains registered");
+ tool.execute(serde_json::json!({"handle":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}))
+ .await
+ .expect("tool reports disabled module as a result")
+ };
+ assert!(
+ unavailable.is_error,
+ "a recorded executor must fail closed when modules are disabled: {}",
+ unavailable.output()
+ );
+}
+
+#[tokio::test]
+async fn resumed_juice_snapshot_does_not_add_live_repl_declarations() {
+ use crate::inference::tokenjuice::{repl_tools_for, REPL_TOOL_NAMES};
+
+ crate::agent::harness::definition::AgentDefinitionRegistry::init_global_builtins()
+ .expect("builtin definitions");
+ let tmp = tempfile::tempdir().expect("tempdir");
+ let mut config = crate::config::Config {
+ workspace_dir: tmp.path().join("workspace"),
+ action_dir: tmp.path().join("workspace"),
+ config_path: tmp.path().join("config.toml"),
+ ..Default::default()
+ };
+ std::fs::create_dir_all(&config.workspace_dir).expect("workspace");
+ config.context.compaction_enabled = true;
+ config.tokenjuice.router_enabled = true;
+ config.tokenjuice.ccr_enabled = true;
+ config.tokenjuice.repl_handle_enabled = true;
+ let mut recorded_spec = repl_tools_for(&config)
+ .into_iter()
+ .find(|tool| tool.name() == REPL_TOOL_NAMES[0])
+ .expect("juice_find")
+ .spec();
+ recorded_spec.description = "frozen juice_find declaration".into();
+ recorded_spec.parameters = serde_json::json!({"type":"object","frozen":true});
+ let recorded = ToolSnapshot::new(vec![recorded_spec.clone()]).expect("recorded snapshot");
+
+ let mut host = crate::agent::OpenHumanSessionHost::from_config_for_agent(&config, "summarizer")
+ .expect("summarizer session");
+ host.ensure_runtime_session().expect("runtime session");
+ let prelude = host
+ .runtime_state
+ .lock()
+ .expect("runtime state")
+ .prelude
+ .clone()
+ .expect("prelude");
+ prelude.adopt_recorded_tools(Some(&recorded));
+ prelude.refresh_turn_boundary(false).await.expect("refresh");
+ let prepared = prelude
+ .prepare(false)
+ .await
+ .expect("prepared surface")
+ .tools
+ .expect("tool snapshot");
+
+ let juice_names = prepared
+ .specs()
+ .iter()
+ .filter(|spec| crate::inference::tokenjuice::is_repl_tool(&spec.name))
+ .map(|spec| spec.name.as_str())
+ .collect::<Vec<_>>();
+ assert_eq!(juice_names, [REPL_TOOL_NAMES[0]]);
+ let frozen = prepared
+ .specs()
+ .iter()
+ .find(|spec| spec.name == recorded_spec.name)
+ .expect("recorded spec");
+ assert_eq!(frozen.description, recorded_spec.description);
+ assert_eq!(frozen.parameters, recorded_spec.parameters);
+
+ let empty = ToolSnapshot::new(Vec::new()).expect("empty recorded snapshot");
+ prelude.adopt_recorded_tools(Some(&empty));
+ prelude
+ .refresh_turn_boundary(false)
+ .await
+ .expect("refresh empty");
+ let prepared = prelude
+ .prepare(false)
+ .await
+ .expect("prepared empty snapshot")
+ .tools
+ .expect("tool snapshot");
+ assert!(
+ prepared
+ .specs()
+ .iter()
+ .all(|spec| !crate::inference::tokenjuice::is_repl_tool(&spec.name)),
+ "an empty frozen transcript must not gain Juice declarations from the live catalog"
+ );
+}
+
#[cfg(feature = "mcp")]
#[test]
fn connected_mcp_actions_enter_search_and_leave_on_disconnect() {
diff --git a/crates/openhuman-core/src/inference/tokenjuice/mod.rs b/crates/openhuman-core/src/inference/tokenjuice/mod.rs
index a9aa0404d8..d733b40d5c 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/mod.rs
+++ b/crates/openhuman-core/src/inference/tokenjuice/mod.rs
@@ -156,7 +156,6 @@ fn client(config: &crate::config::Config) -> crate::modules::client::ModuleClien
let mut config = config.clone();
#[cfg(test)]
if let Some(path) = std::env::var_os("TINYJUICE_TEST_MODULE") {
- config.modules.enabled = true;
config
.modules
.overrides
@@ -421,8 +420,15 @@ pub async fn query(
let config = crate::config::Config::load_or_init()
.await
.map_err(|e| e.to_string())?;
- install_from_config(&config).await?;
- client(&config)
+ query_for_config(&config, request).await
+}
+
+pub(crate) async fn query_for_config(
+ config: &crate::config::Config,
+ request: tinyjuice_bus::wire::QueryRequest,
+) -> Result<tinyjuice_bus::wire::QueryResponse, String> {
+ install_from_config(config).await?;
+ client(config)
.call("tinyjuice", methods::QUERY, (request,))
.await
.map_err(|e| e.to_string())
diff --git a/crates/openhuman-core/src/inference/tokenjuice/mod_tests.rs b/crates/openhuman-core/src/inference/tokenjuice/mod_tests.rs
index 8d5abff808..bdd031b4c7 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/mod_tests.rs
+++ b/crates/openhuman-core/src/inference/tokenjuice/mod_tests.rs
@@ -136,6 +136,16 @@ async fn an_off_profile_still_discloses_a_registered_summary() {
#[tokio::test]
async fn a_module_disabled_in_configuration_discloses_a_wanted_summary() {
let _lock = crate::config::TEST_ENV_LOCK.lock().await;
+ let mut config = crate::config::Config::default();
+ config.modules.enabled = false;
+ let fixture_aware_call = client(&config)
+ .call::<()>("tinyjuice", methods::INSTALL, (install_request(&config),))
+ .await;
+ assert!(
+ fixture_aware_call.is_err(),
+ "a test module artifact must not override modules.enabled=false"
+ );
+
let previous = std::env::var_os("TINYJUICE_TEST_MODULE");
// SAFETY: serialized by TEST_ENV_LOCK; restored below for every exit path.
unsafe { std::env::remove_var("TINYJUICE_TEST_MODULE") };
@@ -151,8 +161,6 @@ async fn a_module_disabled_in_configuration_discloses_a_wanted_summary() {
}
let _restore = RestoreEnv(previous);
- let mut config = crate::config::Config::default();
- config.modules.enabled = false;
let config = std::sync::Arc::new(config);
let output = compact_tool_output(ToolOutputCompaction {
diff --git a/crates/openhuman-core/src/inference/tokenjuice/repl_tools.rs b/crates/openhuman-core/src/inference/tokenjuice/repl_tools.rs
index 2c77bb207f..69a5dade5b 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/repl_tools.rs
+++ b/crates/openhuman-core/src/inference/tokenjuice/repl_tools.rs
@@ -34,12 +34,17 @@ pub(crate) trait QuerySource: Send + Sync {
async fn query(&self, request: QueryRequest) -> Result<QueryResponse, String>;
}
-struct ModuleSource;
+struct ModuleSource {
+ config: Option<Arc<crate::config::Config>>,
+}
#[async_trait]
impl QuerySource for ModuleSource {
async fn query(&self, request: QueryRequest) -> Result<QueryResponse, String> {
- super::query(request).await
+ match &self.config {
+ Some(config) => super::query_for_config(config, request).await,
+ None => super::query(request).await,
+ }
}
}
@@ -59,7 +64,10 @@ struct ModuleReplTool {
/// The three REPL tools, reading through the TinyJuice module. Without a
/// workspace they cannot resolve an `artifact_path`; see [`repl_tools_for`].
pub fn repl_tools() -> Vec<Box<dyn Tool>> {
- repl_tools_with(Arc::new(ModuleSource), ReplLimits::default())
+ repl_tools_with(
+ Arc::new(ModuleSource { config: None }),
+ ReplLimits::default(),
+ )
}
/// The REPL tools, or none while large results are not stored behind a handle
@@ -75,7 +83,9 @@ pub fn repl_tools_for(config: &crate::config::Config) -> Vec<Box<dyn Tool>> {
REPL_TOOL_NAMES.join(", ")
);
repl_tools_with_artifacts(
- Arc::new(ModuleSource),
+ Arc::new(ModuleSource {
+ config: Some(Arc::new(config.clone())),
+ }),
ReplLimits::default(),
Some(crate::security::policy::tool_result_artifacts_dir(
&config.workspace_dir,
@@ -83,6 +93,40 @@ pub fn repl_tools_for(config: &crate::config::Config) -> Vec<Box<dyn Tool>> {
)
}
+/// Rebuild only the REPL tools a resumed transcript already declared. Their
+/// schemas stay frozen while execution uses the current workspace and module
+/// policy. This deliberately does not consult `repl_handle_active`: that flag
+/// controls creating new handles, not reading handles a thread already has.
+pub(crate) fn repl_tools_for_recorded(
+ config: Option<&crate::config::Config>,
+ workspace_dir: &Path,
+ recorded: &[tinytools::ToolSpec],
+) -> Vec<Box<dyn Tool>> {
+ let declarations = tinyjuice_bus::tools::repl_tool_declarations();
+ recorded
+ .iter()
+ .filter(|spec| is_repl_tool(&spec.name))
+ .filter_map(|spec| {
+ let declaration = declarations.iter().find(|item| item.name == spec.name)?;
+ let limits = ReplLimits::default();
+ Some(Box::new(ModuleReplTool {
+ name: spec.name.clone(),
+ op: declaration.op.clone(),
+ description: spec.description.clone(),
+ schema: spec.parameters.clone(),
+ cap: Some(limits.max_output_chars.saturating_mul(2)),
+ source: Arc::new(ModuleSource {
+ config: config.cloned().map(Arc::new),
+ }),
+ limits,
+ artifacts_dir: Some(crate::security::policy::tool_result_artifacts_dir(
+ workspace_dir,
+ )),
+ }) as Box<dyn Tool>)
+ })
+ .collect()
+}
+
pub(crate) fn repl_tools_with(
source: Arc<dyn QuerySource>,
limits: ReplLimits,