Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion crates/tinyagents-harness-tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions crates/tinyagents-harness-tools/src/media/artifact_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ pub type ToolCallIdExtractor =
Arc<dyn for<'a> Fn(Option<&'a dyn ToolRunContext>) -> Option<String> + Send + Sync>;

fn canonical_tool_call_id(context: Option<&dyn ToolRunContext>) -> Option<String> {
context
.and_then(ToolRunContext::host_extension)
.and_then(|extension| {
extension.downcast_ref::<tinyagents_harness::tool::ToolExecutionContext>()
})
.map(|context| context.call_id.to_string())
tinyagents_harness::tool::ToolExecutionContext::call_id_of(context)
}

fn progress_label(kind: &MediaArtifactKind) -> &'static str {
Expand Down
24 changes: 24 additions & 0 deletions crates/tinyagents-harness-tools/src/skills/mod_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Tool> = 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<dyn Tool>]);
recovered.bind(Arc::downgrade(&registry));
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();
Expand Down
23 changes: 19 additions & 4 deletions crates/tinyagents-harness-tools/src/skills/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PackRegistryHandle> {
tool.host_extension()
.and_then(|extension| extension.downcast_ref::<ToolCallBinding>())
.and_then(|binding| binding.metadata::<PackRegistryHandle>())
.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<Self> {
tool.host_extension()
.and_then(|extension| extension.downcast_ref::<ToolCallBinding>())
.and_then(|binding| binding.metadata::<Self>())
.cloned()
}
}

#[async_trait]
Expand Down
4 changes: 3 additions & 1 deletion crates/tinyagents-harness/src/tool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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::<S>()`). `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`).
Expand Down
36 changes: 36 additions & 0 deletions crates/tinyagents-harness/src/tool/context_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
28 changes: 28 additions & 0 deletions crates/tinyagents-harness/src/tool/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ use crate::store::namespaced::NamespacedStore;
/// .and_then(tinytools::ToolRunContext::host_extension)
/// .and_then(|any| any.downcast_ref::<ToolExecutionContext>());
/// ```
///
/// [`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.
Expand Down Expand Up @@ -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::<Self>())
}

/// 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<String> {
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 {
Expand Down
Loading