diff --git a/CHANGELOG.md b/CHANGELOG.md index a41c351dd..af18d34e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## Unreleased + +- `tinyagents-harness`: `ToolExecutionContext::of(context)` recovers the + harness context from a canonical tool's erased + `Option<&dyn ToolRunContext>`, and `ToolExecutionContext::call_id_of(context)` + returns the provider-assigned call id through it (`None` without a + harness-driven context). Hosts no longer need their own `host_extension` + downcast to read the call id. +- `tinyagents-harness-tools`: `PackRegistryHandle::of(tool)` recovers the + shared registry handle from an erased registered `use_skill` tool. The free + function `skills::pack_registry_handle` remains and delegates to it. + ## 3.1.4 - Releases are cut from `main`: the version bump merges through a pull request diff --git a/crates/tinyagents-harness-tools/README.md b/crates/tinyagents-harness-tools/README.md index 1da857750..caff7cea0 100644 --- a/crates/tinyagents-harness-tools/README.md +++ b/crates/tinyagents-harness-tools/README.md @@ -33,7 +33,8 @@ default set. - `skills` provides `UseSkillTool`, `PackCatalog`, `PackRegistryHandle`, and `UseSkillDispatch` for binding a selected tool from a named pack. Hosts can recover the shared registry from an erased registered tool with - `skills::pack_registry_handle`; the generic `ToolCallBinding` still pins each + `PackRegistryHandle::of` (or the equivalent free function + `skills::pack_registry_handle`); the generic `ToolCallBinding` still pins each call's admission and execution target. - `subagents` provides `DriverSubAgentTool`, `SubAgentJobsTool`, and `SubAgentMessageTool` over the registry owned by diff --git a/crates/tinyagents-harness-tools/src/media/artifact_tool.rs b/crates/tinyagents-harness-tools/src/media/artifact_tool.rs index b44804012..84bcc8974 100644 --- a/crates/tinyagents-harness-tools/src/media/artifact_tool.rs +++ b/crates/tinyagents-harness-tools/src/media/artifact_tool.rs @@ -25,12 +25,7 @@ pub type ToolCallIdExtractor = Arc Fn(Option<&'a dyn ToolRunContext>) -> Option + Send + Sync>; fn canonical_tool_call_id(context: Option<&dyn ToolRunContext>) -> Option { - context - .and_then(ToolRunContext::host_extension) - .and_then(|extension| { - extension.downcast_ref::() - }) - .map(|context| context.call_id.to_string()) + tinyagents_harness::tool::ToolExecutionContext::call_id_of(context) } fn progress_label(kind: &MediaArtifactKind) -> &'static str { diff --git a/crates/tinyagents-harness-tools/src/skills/mod_tests.rs b/crates/tinyagents-harness-tools/src/skills/mod_tests.rs index 6a2cc86f6..3e24470c0 100644 --- a/crates/tinyagents-harness-tools/src/skills/mod_tests.rs +++ b/crates/tinyagents-harness-tools/src/skills/mod_tests.rs @@ -300,6 +300,30 @@ fn unrelated_tool_has_no_pack_registry_handle() { assert!(pack_registry_handle(&tool).is_none()); } +#[test] +fn of_returns_the_handle_use_skill_was_built_with() { + let handle = PackRegistryHandle::default(); + let tool: Arc = Arc::new(UseSkillTool::new(handle.clone(), CATALOG)); + let recovered = PackRegistryHandle::of(tool.as_ref()).expect("registered use_skill handle"); + + // Same shared registry: binding through the recovered handle is visible + // through the original one. + let registry = Arc::new(vec![Box::new(FakeTool::plain("a_one")) as Box]); + recovered.bind(Arc::downgrade(®istry)); + assert!(handle.resolve(&CATALOG, "alpha", "a_one").is_some()); +} + +#[test] +fn of_is_none_for_a_tool_without_a_pack_registry_handle() { + let tool = FakeTool::plain("anything_else"); + assert!(PackRegistryHandle::of(&tool).is_none()); + + let binding = ToolCallBinding::new(|_, _| unreachable!("metadata lookup does not bind")) + .with_metadata(String::from("unrelated metadata")); + let tool = FakeTool::plain("another_tool").with_binding(binding); + assert!(PackRegistryHandle::of(&tool).is_none()); +} + #[tokio::test] async fn catalogs_with_the_same_start_pointer_but_different_lengths_conflict() { let handle = PackRegistryHandle::default(); diff --git a/crates/tinyagents-harness-tools/src/skills/tool.rs b/crates/tinyagents-harness-tools/src/skills/tool.rs index 47d1ca272..8aa996afa 100644 --- a/crates/tinyagents-harness-tools/src/skills/tool.rs +++ b/crates/tinyagents-harness-tools/src/skills/tool.rs @@ -276,11 +276,26 @@ impl UseSkillTool { /// The handle lets a host bind durable and synthesized registries and render /// their filtered pack listings. Calls still pass through the generic /// [`ToolCallBinding`] so admission and execution use the same pinned target. +/// +/// Equivalent to [`PackRegistryHandle::of`]. pub fn pack_registry_handle(tool: &dyn Tool) -> Option { - tool.host_extension() - .and_then(|extension| extension.downcast_ref::()) - .and_then(|binding| binding.metadata::()) - .cloned() + PackRegistryHandle::of(tool) +} + +impl PackRegistryHandle { + /// The shared registry handle of an erased registered `use_skill` tool. + /// + /// [`UseSkillTool::new`] attaches the handle as metadata on the tool's + /// [`ToolCallBinding`], which the tool exposes as its host extension; this + /// reads it back from there. `None` for any tool whose binding carries no + /// `PackRegistryHandle` (every tool other than `use_skill`). + #[must_use] + pub fn of(tool: &dyn Tool) -> Option { + tool.host_extension() + .and_then(|extension| extension.downcast_ref::()) + .and_then(|binding| binding.metadata::()) + .cloned() + } } #[async_trait] diff --git a/crates/tinyagents-harness/src/tool/README.md b/crates/tinyagents-harness/src/tool/README.md index 8762ca2e9..4729fbfd9 100644 --- a/crates/tinyagents-harness/src/tool/README.md +++ b/crates/tinyagents-harness/src/tool/README.md @@ -39,7 +39,9 @@ scrub) lives in `tinytools-agent`, reached through (`RunContext::with_namespaced_store`), and an optional typed state view (`RunContext::with_state_view`, read with `state::()`). `custom(payload)` emits `AgentEvent::Custom` correlated to the call. A `tinytools::Tool` - reaches all of this by downcasting `ToolRunContext::host_extension()`; + reaches all of this by downcasting `ToolRunContext::host_extension()` + — `ToolExecutionContext::of(ctx)` does that downcast and + `ToolExecutionContext::call_id_of(ctx)` reads the call id through it; the portable methods still cover only workspace/thread/output cap. A tool that needs a real child `RunContext` goes through the explicit dispatch seam instead (B1; see `docs/modules/harness/tool-context.md`). diff --git a/crates/tinyagents-harness/src/tool/context_tests.rs b/crates/tinyagents-harness/src/tool/context_tests.rs index 9f7284eb7..e46328af5 100644 --- a/crates/tinyagents-harness/src/tool/context_tests.rs +++ b/crates/tinyagents-harness/src/tool/context_tests.rs @@ -108,3 +108,39 @@ fn a_child_context_inherits_the_store_and_state_view() { Some(&AppState { user: "bob" }) ); } + +#[test] +fn of_recovers_the_harness_context_from_the_erased_run_context() { + let ctx: RunContext = RunContext::new(RunConfig::new("run-1"), ()); + let tool_ctx = ToolExecutionContext::from_run_context(&ctx, CallId::new("call-7")); + let erased: &dyn tinytools::ToolRunContext = &tool_ctx; + let recovered = ToolExecutionContext::of(Some(erased)).expect("harness context"); + assert!(std::ptr::eq(recovered, &tool_ctx)); +} + +#[test] +fn of_is_none_without_a_context_or_for_a_foreign_context() { + struct Foreign; + impl tinytools::ToolRunContext for Foreign {} + + assert!(ToolExecutionContext::of(None).is_none()); + let foreign: &dyn tinytools::ToolRunContext = &Foreign; + assert!(ToolExecutionContext::of(Some(foreign)).is_none()); +} + +#[test] +fn call_id_of_reads_the_harness_execution_context() { + let run_ctx: RunContext = RunContext::new(RunConfig::new("test-run"), ()); + let exec_ctx = ToolExecutionContext::from_run_context(&run_ctx, CallId::new("call-42")); + + let erased: &dyn tinytools::ToolRunContext = &exec_ctx; + assert_eq!( + ToolExecutionContext::call_id_of(Some(erased)), + Some("call-42".to_string()) + ); +} + +#[test] +fn call_id_of_is_none_without_a_context() { + assert_eq!(ToolExecutionContext::call_id_of(None), None); +} diff --git a/crates/tinyagents-harness/src/tool/types.rs b/crates/tinyagents-harness/src/tool/types.rs index 6cd493ce5..37939d6bd 100644 --- a/crates/tinyagents-harness/src/tool/types.rs +++ b/crates/tinyagents-harness/src/tool/types.rs @@ -34,6 +34,9 @@ use crate::store::namespaced::NamespacedStore; /// .and_then(tinytools::ToolRunContext::host_extension) /// .and_then(|any| any.downcast_ref::()); /// ``` +/// +/// [`Self::of`] performs exactly that downcast, and [`Self::call_id_of`] +/// reads the call id through it. #[derive(Clone)] pub struct ToolExecutionContext { /// Run that invoked the tool. @@ -97,6 +100,31 @@ impl ToolExecutionContext { } } + /// The harness context behind a canonical tool's erased run context. + /// + /// This is the one downcast documented above: it reads + /// [`tinytools::ToolRunContext::host_extension`] and returns the + /// `ToolExecutionContext` the agent loop installed there. `None` when there + /// is no context, or when the context is not harness-driven (a test double + /// or another host's `ToolRunContext`). + #[must_use] + pub fn of(context: Option<&dyn tinytools::ToolRunContext>) -> Option<&Self> { + context + .and_then(tinytools::ToolRunContext::host_extension) + .and_then(|extension| extension.downcast_ref::()) + } + + /// The provider-assigned id of the tool call a canonical tool is running + /// as, read from its erased run context. + /// + /// The portable [`tinytools::ToolRunContext`] surface deliberately does not + /// carry the call id; it is harness-owned. This reads [`Self::call_id`] off + /// the context [`Self::of`] recovers, so it is `None` exactly when that is. + #[must_use] + pub fn call_id_of(context: Option<&dyn tinytools::ToolRunContext>) -> Option { + Self::of(context).map(|harness| harness.call_id.as_str().to_string()) + } + /// Attaches an isolated workspace descriptor the tool may operate in. #[must_use] pub fn with_workspace(mut self, workspace: tinytools::WorkspaceDescriptor) -> Self {