Skip to content

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

fix(toolpacks): make a pack registry rebind actually take effect

Shanu1acd88fb127f
diff --git a/src/openhuman/tools/toolpacks/toolpacks_tests.rs b/src/openhuman/tools/toolpacks/toolpacks_tests.rs
index 767b6476bc..cd5ca13c7c 100644
--- a/src/openhuman/tools/toolpacks/toolpacks_tests.rs
+++ b/src/openhuman/tools/toolpacks/toolpacks_tests.rs
@@ -483,3 +483,47 @@ fn the_reactive_fleet_tools_are_never_packed() {
);
}
}
+
+#[test]
+fn rebinding_a_pack_handle_repoints_it_at_the_new_registry() {
+ // `bind_pack_registry`'s own docs say to "call this after **every**
+ // rebinding of the agent's tool `Arc`", but the handle used to hold a
+ // `OnceLock`, so the second write was dropped on the floor. An agent that
+ // rebuilt its tool vector kept a `Weak` into the old allocation; once that
+ // allocation went away the upgrade failed and every `load_skill` /
+ // `use_skill` reported the registry as unavailable for the rest of the
+ // session. Last write must win.
+ let name = pack("crypto").unwrap().tools[0];
+
+ // The agent's first tool `Arc`, with the packed tool marked Dangerous.
+ let first = registry_with(name, PermissionLevel::Dangerous);
+ let use_skill = find(&first, USE_SKILL);
+ let args = json!({"skill": "crypto", "tool": name});
+ assert_eq!(
+ use_skill.permission_level_with_args(&args),
+ PermissionLevel::Dangerous,
+ "sanity: the first binding resolves"
+ );
+
+ // The agent rebuilds its registry into a *different* allocation, where the
+ // same packed tool is only ReadOnly.
+ let mut rebuilt: Vec<Box<dyn Tool>> = vec![Box::new(FakeTool {
+ name,
+ level: PermissionLevel::ReadOnly,
+ external: false,
+ timeout: ToolTimeout::Inherit,
+ })];
+ append_pack_tools(&mut rebuilt);
+ let rebuilt = Arc::new(rebuilt);
+
+ // Re-point the ORIGINAL handle at it, which is what a rebuild does.
+ crate::openhuman::tools::traits::pack_registry_handle(use_skill)
+ .expect("use_skill exposes a pack registry handle")
+ .bind(Arc::downgrade(&rebuilt));
+
+ assert_eq!(
+ use_skill.permission_level_with_args(&args),
+ PermissionLevel::ReadOnly,
+ "the rebound handle must resolve against the new registry, not the old one"
+ );
+}
diff --git a/src/openhuman/tools/toolpacks/tools.rs b/src/openhuman/tools/toolpacks/tools.rs
index 3cc84fbcfc..be79238706 100644
--- a/src/openhuman/tools/toolpacks/tools.rs
+++ b/src/openhuman/tools/toolpacks/tools.rs
@@ -1,6 +1,6 @@
//! The two always-on tools that stand in for every packed tool.
-use std::sync::{Arc, OnceLock, Weak};
+use std::sync::{Arc, RwLock, Weak};
use async_trait::async_trait;
use serde_json::{json, Value};
@@ -26,21 +26,37 @@ type ToolRegistryRef = Weak<Vec<Box<dyn Tool>>>;
/// cycle that never drops.
#[derive(Clone, Default)]
pub struct PackRegistryHandle {
- inner: Arc<OnceLock<ToolRegistryRef>>,
+ inner: Arc<RwLock<Option<ToolRegistryRef>>>,
}
impl PackRegistryHandle {
+ /// Point this handle at the registry it lives in, replacing any previous
+ /// binding.
+ ///
+ /// Rebinding has to actually take effect. This was a `OnceLock` whose
+ /// second write was dropped, which silently contradicted
+ /// [`super::bind_pack_registry`]'s own instruction to "re-bind after any
+ /// later rebuild of this `Arc`": once an agent replaced its tool vector the
+ /// handle still pointed at the old allocation, the `Weak` failed to
+ /// upgrade, and every `load_skill` / `use_skill` call reported the registry
+ /// as unavailable for the rest of the session. Last write wins.
pub fn bind(&self, registry: ToolRegistryRef) {
- // Binding twice is not an error: an agent that rebuilds its tool `Arc`
- // re-binds, and the first write simply wins for that generation. What
- // must never happen is a *silent* mismatch, so log the redundant bind.
- if self.inner.set(registry).is_err() {
- tracing::trace!("[toolpacks] registry handle already bound — keeping first binding");
+ match self.inner.write() {
+ Ok(mut slot) => *slot = Some(registry),
+ // The lock is only ever held for a pointer read or write, so a
+ // poisoned lock means a panic elsewhere. Recover rather than
+ // propagate: a stale binding degrades to "skill unavailable",
+ // which is the failure this rebinding exists to prevent.
+ Err(poisoned) => *poisoned.into_inner() = Some(registry),
}
}
fn tools(&self) -> Option<ToolVec> {
- self.inner.get()?.upgrade()
+ let slot = match self.inner.read() {
+ Ok(slot) => slot,
+ Err(poisoned) => poisoned.into_inner(),
+ };
+ slot.as_ref()?.upgrade()
}
/// Resolve a packed tool by name, enforcing that it belongs to `skill`.