Skip to content

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

test(tools): assert the recovered registry handle is the same handle

M3gA-Mind27f8e8b1012f
diff --git a/tests/raw_coverage/tools_erased_host_extension_raw_coverage_e2e.rs b/tests/raw_coverage/tools_erased_host_extension_raw_coverage_e2e.rs
index 3faa615f3a..b5f43054b3 100644
--- a/tests/raw_coverage/tools_erased_host_extension_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/tools_erased_host_extension_raw_coverage_e2e.rs
@@ -22,6 +22,7 @@
//! only tested state were the same value. These tests assert the `Some` side.
use std::any::Any;
+use std::sync::Arc;
use async_trait::async_trait;
use serde_json::{json, Value};
@@ -30,28 +31,77 @@ use openhuman_core::openhuman::agent::tool_policy::{
GeneratedToolRuntimeContext, GeneratedToolRuntimeRisk,
};
use openhuman_core::openhuman::skills::types::tool_result_from_mcp;
+use openhuman_core::openhuman::tools::toolpacks::registry::PACKS;
use openhuman_core::openhuman::tools::toolpacks::tools::{LoadSkillTool, PackRegistryHandle};
use openhuman_core::openhuman::tools::traits::{
generated_runtime_context, pack_registry_handle, PermissionLevel, Tool, ToolResult,
};
/// A production pack tool's registry handle survives the round trip through
-/// `dyn Any`.
+/// `dyn Any` **as the same handle**, not merely as some handle.
///
/// `LoadSkillTool` is one of the two real producers in the tree. If its
/// `host_extension` ever stored something other than a `PackRegistryHandle`,
/// this is the only place that would notice: `toolpacks::ops` reads the handle
/// back through the same free function and, on `None`, silently skips the pack
/// registry rather than failing.
-#[test]
-fn a_pack_tools_registry_handle_reads_back_through_the_erased_extension() {
+///
+/// `is_some()` alone is too weak to catch the interesting half of that. A
+/// producer that handed back a *different* `PackRegistryHandle` — a fresh
+/// `default()`, or a second instance — satisfies `is_some()` while
+/// `toolpacks::ops` reads an unbound registry and skips the pack exactly as if
+/// the downcast had failed. `PackRegistryHandle` is `Clone + Default` with a
+/// private `Arc<OnceLock<_>>` and no `PartialEq`, so identity is not directly
+/// comparable; it is observable, because two handles share one `OnceLock` only
+/// if they are the same handle. So bind through the **recovered** handle and
+/// observe the effect on the **tool**, which reads its own.
+///
+/// `render_pack` distinguishes the two states by message, which is what makes
+/// this a real discriminator rather than a smoke test:
+///
+/// * handle unbound ⇒ "The skill registry is not available in this session"
+/// * handle bound, registry empty ⇒ "Skill `…` has no tools available"
+///
+/// An empty registry is deliberate: the second message proves the binding was
+/// observed without needing to construct a real packed tool.
+#[tokio::test]
+async fn a_pack_tools_registry_handle_reads_back_as_the_same_handle() {
let tool = LoadSkillTool::new(PackRegistryHandle::default());
- assert!(
- pack_registry_handle(&tool).is_some(),
+ let recovered = pack_registry_handle(&tool).expect(
"a pack tool must yield its PackRegistryHandle through the erased \
host extension; None here is the silent-downcast failure that makes \
- toolpacks::ops skip the registry"
+ toolpacks::ops skip the registry",
+ );
+
+ // Kept alive for the whole test: the handle stores a `Weak`, so dropping
+ // this would make the binding unobservable and the assertion vacuous.
+ let registry: Arc<Vec<Box<dyn Tool>>> = Arc::new(Vec::new());
+ recovered.bind(Arc::downgrade(&registry));
+
+ let skill = PACKS
+ .first()
+ .expect("the pack registry ships at least one pack")
+ .id;
+ let rendered = tool
+ .execute(json!({ "skill": skill }))
+ .await
+ .expect("load_skill reports failure in its ToolResult, never as Err")
+ .output()
+ .to_string();
+
+ assert!(
+ !rendered.contains("registry is not available"),
+ "binding through the recovered handle did not reach the tool's own \
+ handle, so the erased extension yielded a different \
+ PackRegistryHandle than the one supplied — the failure `is_some()` \
+ cannot see. Rendered: {rendered}"
+ );
+ assert!(
+ rendered.contains("has no tools available"),
+ "the tool should have got as far as walking the (empty) bound \
+ registry; a different message means this test is no longer \
+ discriminating between bound and unbound. Rendered: {rendered}"
);
}