Skip to content

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

feat(skills): add workflow run tool and cost scope tracking

Steven Enamakel1e6f6e0c9f28
diff --git a/crates/openhuman-core/src/agent/tools/run_workflow.rs b/crates/openhuman-core/src/agent/tools/run_workflow.rs
index a768e5c093..e5f4a3f4c2 100644
--- a/crates/openhuman-core/src/agent/tools/run_workflow.rs
+++ b/crates/openhuman-core/src/agent/tools/run_workflow.rs
@@ -323,6 +323,7 @@ impl Tool for RunWorkflowTool {
context: Option<&dyn tinytools::ToolRunContext>,
) -> anyhow::Result<ToolResult> {
let dispatch = crate::agent::tinyagents::host::run_context::dispatch_ctx_for_tool(context);
+ let origin = crate::agent::tinyagents::host::run_context::turn_origin_for_tool(context);
// Accept `workflow_id`, falling back to the legacy `skill_id` alias so
// any in-flight caller from before the rename still works.
let workflow_id = match args
@@ -343,7 +344,7 @@ impl Tool for RunWorkflowTool {
// Fire-and-forget: only the spawn backstop applies — no await, so no
// re-entrancy/nesting slot to take.
if wait_seconds == 0 {
- return match spawn_workflow_run_background(workflow_id.clone(), inputs).await {
+ return match spawn_workflow_run_background(workflow_id.clone(), inputs, origin).await {
// Count only spawns that actually start against the backstop —
// unknown-workflow / bad-input rejections (the Err arm) must not
// burn the budget, or rejected calls accumulate and trip the
@@ -377,7 +378,7 @@ impl Tool for RunWorkflowTool {
Err(e) => return Ok(ToolResult::error(format!("run_workflow: {e}"))),
};
- let started = match spawn_workflow_run_background(workflow_id.clone(), inputs).await {
+ let started = match spawn_workflow_run_background(workflow_id.clone(), inputs, origin).await {
Ok(s) => {
if let Err(e) = guard::account_spawn(&dispatch) {
return Ok(ToolResult::error(format!("run_workflow: {e}")));
diff --git a/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs b/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
index 35c88bbdbe..b7eff68ac9 100644
--- a/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
+++ b/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
@@ -151,7 +151,16 @@ impl DispatchCtx {
self.core.as_ref().and_then(|core| core.turn_origin().cloned())
}
- /// A non-owning handle on the bound context, for state that outlives the
+ /// [`turn_origin`](Self::turn_origin) of this call, else the origin the
+ /// calling task's turn scope carries. For a handler whose context was
+ /// built by a transport that does not carry the turn's origin, while a
+ /// turn that called it into being still holds it (transitional, #7801).
+ pub fn turn_origin_or_scoped(&self) -> Option<crate::agent::turn_origin::AgentTurnOrigin> {
+ self.turn_origin()
+ .or_else(|| Self::ambient_turn().turn_origin())
+ }
+
+, for state that outlives the
/// call (a session, a built turn). The tenant lease is the strong count of
/// the context ([`CoreContext::tenant_in_use`]), so a long-lived holder
/// must not keep it alive.
diff --git a/crates/openhuman-core/src/platform/cost/scope.rs b/crates/openhuman-core/src/platform/cost/scope.rs
index ae5c6078ab..2011cbd2b0 100644
--- a/crates/openhuman-core/src/platform/cost/scope.rs
+++ b/crates/openhuman-core/src/platform/cost/scope.rs
@@ -24,7 +24,7 @@ impl UsageScope {
provider: Option<&str>,
subagent: Option<(&str, &str)>,
) -> Self {
- let origin = crate::core::runtime::CoreContext::current_turn_origin();
+ let origin = ctx.turn_origin_or_scoped();
let (thread_id, origin) = describe_origin(origin.as_ref());
let session_agent = ctx.tenant().ok().and_then(|tenant| tenant.agent);
let (agent_id, subagent_task_id) = match subagent {
diff --git a/crates/openhuman-core/src/skills/runtime/run_machinery.rs b/crates/openhuman-core/src/skills/runtime/run_machinery.rs
index c282d8a31b..aa8bcc250f 100644
--- a/crates/openhuman-core/src/skills/runtime/run_machinery.rs
+++ b/crates/openhuman-core/src/skills/runtime/run_machinery.rs
@@ -40,6 +40,7 @@ pub struct WorkflowRunStarted {
pub async fn spawn_workflow_run_background(
skill_id_param: String,
inputs_param: Option<Value>,
+ parent_origin: Option<crate::agent::turn_origin::AgentTurnOrigin>,
) -> Result<WorkflowRunStarted, String> {
let workspace = resolve_workspace_dir().await;
let skill = registry::get_workflow(&workspace, &skill_id_param)
@@ -148,8 +149,8 @@ pub async fn spawn_workflow_run_background(
// toolchain are loaded fresh per run; the parent returns the handle
// immediately. Same flow handle_skills_run used to inline — extracted
// so the `run_workflow` agent tool can re-use it for skill chaining.
- let inherited_origin = crate::agent::turn_origin::current()
- .unwrap_or(crate::agent::turn_origin::AgentTurnOrigin::Unknown);
+ let inherited_origin =
+ parent_origin.unwrap_or(crate::agent::turn_origin::AgentTurnOrigin::Unknown);
{
let run_id = run_id.clone();
let workflow_id = workflow_id.clone();
diff --git a/crates/openhuman-core/src/skills/runtime/schemas.rs b/crates/openhuman-core/src/skills/runtime/schemas.rs
index 6ccc1fd3fd..607076af21 100644
--- a/crates/openhuman-core/src/skills/runtime/schemas.rs
+++ b/crates/openhuman-core/src/skills/runtime/schemas.rs
@@ -264,15 +264,16 @@ pub fn skill_runtime_schemas(function: &str) -> ControllerSchema {
}
}
-fn handle_run(_ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
+fn handle_run(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let payload = deserialize_params::<RunParams>(params)?;
tracing::info!(skill_id = %payload.skill_id, "[skill_runtime][rpc] run");
- let origin = crate::agent::turn_origin::current()
+ let origin = ctx
+ .turn_origin_or_scoped()
.unwrap_or(crate::agent::turn_origin::AgentTurnOrigin::Cli);
let started = crate::agent::turn_origin::with_origin(
- origin,
- spawn_workflow_run_background(payload.skill_id, payload.inputs),
+ origin.clone(),
+ spawn_workflow_run_background(payload.skill_id, payload.inputs, Some(origin)),
)
.await?;
to_json(Outcome::new(
diff --git a/crates/openhuman-core/src/skills/schemas/handlers.rs b/crates/openhuman-core/src/skills/schemas/handlers.rs
index 60d7c72963..72d253b172 100644
--- a/crates/openhuman-core/src/skills/schemas/handlers.rs
+++ b/crates/openhuman-core/src/skills/schemas/handlers.rs
@@ -162,14 +162,15 @@ pub(super) fn handle_skills_recent_runs(
})
}
-pub(super) fn handle_skills_run(_ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
+pub(super) fn handle_skills_run(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let payload = deserialize_params::<WorkflowsRunParams>(params)?;
- let origin = crate::agent::turn_origin::current()
+ let origin = ctx
+ .turn_origin_or_scoped()
.unwrap_or(crate::agent::turn_origin::AgentTurnOrigin::Cli);
let started = match crate::agent::turn_origin::with_origin(
- origin,
- spawn_workflow_run_background(payload.workflow_id, payload.inputs),
+ origin.clone(),
+ spawn_workflow_run_background(payload.workflow_id, payload.inputs, Some(origin)),
)
.await
{
diff --git a/crates/openhuman-core/src/threads/ops/crud.rs b/crates/openhuman-core/src/threads/ops/crud.rs
index e4f73c83f9..8361394bb6 100644
--- a/crates/openhuman-core/src/threads/ops/crud.rs
+++ b/crates/openhuman-core/src/threads/ops/crud.rs
@@ -165,7 +165,7 @@ pub async fn message_append(
ctx: &DispatchCtx,
request: AppendConversationMessageRequest,
) -> Result<Outcome<ApiEnvelope<ConversationMessageRecord>>, ThreadsError> {
- let origin = crate::core::runtime::CoreContext::current_turn_origin();
+ let origin = ctx.turn_origin_or_scoped();
let config = ctx
.load_config()
.await
diff --git a/crates/openhuman-core/src/tools/impl/browser/browser.rs b/crates/openhuman-core/src/tools/impl/browser/browser.rs
index 364c1c4847..60023faa9f 100644
--- a/crates/openhuman-core/src/tools/impl/browser/browser.rs
+++ b/crates/openhuman-core/src/tools/impl/browser/browser.rs
@@ -559,11 +559,8 @@ impl Tool for BrowserTool {
}
// The typed origin bound to this turn's immutable CoreContext by its
// entry point (cron, background job, workflow, chat).
- let origin = crate::core::runtime::CoreContext::current_turn_origin();
- match self.run(&args, origin.as_ref()).await {
- Ok(v) => Ok(ToolResult::success(serde_json::to_string_pretty(&v)?)),
- Err(e) => Ok(ToolResult::error(e.to_string())),
- }
+ let origin = crate::core::runtime::DispatchCtx::ambient_turn().turn_origin();
+ self.execute_with_origin(args, origin).await
}
async fn execute_with_context(
@@ -579,7 +576,8 @@ impl Tool for BrowserTool {
*held = Some(key);
}
}
- self.execute(args).await
+ let origin = crate::agent::tinyagents::host::run_context::turn_origin_for_tool(context);
+ self.execute_with_origin(args, origin).await
}
}