Skip to content

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

refactor(task_sources): thread dispatch context through fetch pipeline

Steven Enamakel6b31672db309
diff --git a/crates/openhuman-core/src/integrations/task_sources/bus.rs b/crates/openhuman-core/src/integrations/task_sources/bus.rs
index ebf8fb70df..813f22d938 100644
--- a/crates/openhuman-core/src/integrations/task_sources/bus.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/bus.rs
@@ -84,12 +84,13 @@ async fn fire_in_scope(
if !config.task_sources.enabled {
return;
}
- fire_for_connection(&config, provider, toolkit, connection_id).await;
+ fire_for_connection(ctx, &config, provider, toolkit, connection_id).await;
}
/// Starts a one-shot fetch for every enabled source of `provider` in the
/// current storage scope that the new connection serves.
async fn fire_for_connection(
+ ctx: &DispatchCtx,
config: &crate::config::Config,
provider: ProviderSlug,
toolkit: &str,
@@ -123,10 +124,11 @@ async fn fire_for_connection(
// pattern as the periodic poll). Each fetch captures its own
// owned config + source.
let config = config.clone();
+ let ctx = ctx.clone();
// Scoped: the fetch keeps the scope's agent context.
crate::core::runtime::spawn_scoped(async move {
- let _ =
- pipeline::run_source_once(&config, &source, FetchReason::ConnectionCreated).await;
+ let _ = pipeline::run_source_once(&ctx, &config, &source, FetchReason::ConnectionCreated)
+ .await;
});
}
}
diff --git a/crates/openhuman-core/src/integrations/task_sources/bus_tests.rs b/crates/openhuman-core/src/integrations/task_sources/bus_tests.rs
index 1ef59090ff..c4923dd248 100644
--- a/crates/openhuman-core/src/integrations/task_sources/bus_tests.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/bus_tests.rs
@@ -40,9 +40,9 @@ async fn a_new_connection_fires_the_matching_sources_of_the_scope() {
)
.unwrap();
}
- fire_for_connection(&config, ProviderSlug::Github, "github", "conn-1").await;
+ fire_for_connection(&crate::core::runtime::DispatchCtx::host_default(), &config, ProviderSlug::Github, "github", "conn-1").await;
// A toolkit with no sources fires nothing.
- fire_for_connection(&config, ProviderSlug::Notion, "notion", "conn-1").await;
+ fire_for_connection(&crate::core::runtime::DispatchCtx::host_default(), &config, ProviderSlug::Notion, "notion", "conn-1").await;
assert_eq!(store::list_sources(&config).unwrap().len(), 2);
}
@@ -58,7 +58,7 @@ async fn an_unreadable_scope_is_skipped() {
action_dir: blocked,
..crate::config::Config::default()
};
- fire_for_connection(&config, ProviderSlug::Github, "github", "conn-1").await;
+ fire_for_connection(&crate::core::runtime::DispatchCtx::host_default(), &config, ProviderSlug::Github, "github", "conn-1").await;
}
/// Smoke test: the subscriber handles connection events through the scope's
diff --git a/crates/openhuman-core/src/integrations/task_sources/ops.rs b/crates/openhuman-core/src/integrations/task_sources/ops.rs
index 8097b069b5..960c4a7d45 100644
--- a/crates/openhuman-core/src/integrations/task_sources/ops.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/ops.rs
@@ -99,18 +99,25 @@ pub async fn remove(config: &Config, id: &str) -> Result<Outcome<Value>, String>
}
/// Manually fetch one source now (`FetchReason::Manual`).
-pub async fn fetch(config: &Config, id: &str) -> Result<Outcome<super::FetchOutcome>, String> {
+pub async fn fetch(
+ ctx: &crate::core::runtime::DispatchCtx,
+ config: &Config,
+ id: &str,
+) -> Result<Outcome<super::FetchOutcome>, String> {
let source = store::get_source(config, id).map_err(|e| e.to_string())?;
- let outcome = pipeline::run_source_once(config, &source, FetchReason::Manual).await;
+ let outcome = pipeline::run_source_once(ctx, config, &source, FetchReason::Manual).await;
Ok(Outcome::new(outcome, vec![]))
}
/// Manually sync all enabled task sources now.
-pub async fn sync(config: &Config) -> Result<Outcome<Vec<super::FetchOutcome>>, String> {
+pub async fn sync(
+ ctx: &crate::core::runtime::DispatchCtx,
+ config: &Config,
+) -> Result<Outcome<Vec<super::FetchOutcome>>, String> {
let sources = store::list_sources(config).map_err(|e| e.to_string())?;
let mut outcomes = Vec::new();
for source in sources.into_iter().filter(|source| source.enabled) {
- outcomes.push(pipeline::run_source_once(config, &source, FetchReason::Manual).await);
+ outcomes.push(pipeline::run_source_once(ctx, config, &source, FetchReason::Manual).await);
}
tracing::info!(
source_count = outcomes.len(),
diff --git a/crates/openhuman-core/src/integrations/task_sources/periodic.rs b/crates/openhuman-core/src/integrations/task_sources/periodic.rs
index bc5b1b0846..9a9c8944bd 100644
--- a/crates/openhuman-core/src/integrations/task_sources/periodic.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/periodic.rs
@@ -165,7 +165,7 @@ pub(crate) async fn run_one_tick(ctx: &DispatchCtx) -> Result<(), String> {
// Record the attempt up front so a slow/failing fetch doesn't
// re-fire every tick.
record_poll(&source.id);
- let outcome = pipeline::run_source_once(&config, &source, FetchReason::Periodic).await;
+ let outcome = pipeline::run_source_once(ctx, &config, &source, FetchReason::Periodic).await;
fired += 1;
tracing::debug!(
source_id = %source.id,
diff --git a/crates/openhuman-core/src/integrations/task_sources/periodic_tests.rs b/crates/openhuman-core/src/integrations/task_sources/periodic_tests.rs
index 0b1c6917e7..dc2bece709 100644
--- a/crates/openhuman-core/src/integrations/task_sources/periodic_tests.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/periodic_tests.rs
@@ -196,7 +196,7 @@ async fn manual_fetch_still_returns_the_explanatory_error() {
std::fs::create_dir_all(&config.workspace_dir).expect("workspace");
let s = source("ts-manual-path-xyz", 1800);
- let outcome = pipeline::run_source_once(&config, &s, FetchReason::Manual).await;
+ let outcome = pipeline::run_source_once(&crate::core::runtime::DispatchCtx::host_default(), &config, &s, FetchReason::Manual).await;
let error = outcome
.error
diff --git a/crates/openhuman-core/src/integrations/task_sources/pipeline.rs b/crates/openhuman-core/src/integrations/task_sources/pipeline.rs
index f4d1514df1..70e679bdd1 100644
--- a/crates/openhuman-core/src/integrations/task_sources/pipeline.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/pipeline.rs
@@ -19,6 +19,7 @@ use super::{enrich, route, store};
/// Run a single fetch pass over `source`. Captures errors into the
/// returned [`FetchOutcome`] rather than propagating them.
pub async fn run_source_once(
+ ctx: &crate::core::runtime::DispatchCtx,
config: &Config,
source: &TaskSource,
reason: FetchReason,
@@ -36,7 +37,7 @@ pub async fn run_source_once(
"[task_sources:pipeline] fetch pass starting"
);
- match run_inner(config, source, reason, &mut outcome).await {
+ match run_inner(ctx, config, source, reason, &mut outcome).await {
Ok(()) => {
let status = format!(
"fetched {} routed {} dupes {} pruned {}",
@@ -124,6 +125,7 @@ fn fetch_tasks_unavailable(
/// Fetch, process, and reconcile one source pass.
async fn run_inner(
+ ctx: &crate::core::runtime::DispatchCtx,
config: &Config,
source: &TaskSource,
_reason: FetchReason,
@@ -184,7 +186,7 @@ async fn run_inner(
// Route first; only mark ingested on success so a routing
// failure retries on the next pass instead of being silently
// dropped.
- if let Err(e) = route::route_enriched(config, source, &enriched).await {
+ if let Err(e) = route::route_enriched(ctx, config, source, &enriched).await {
tracing::warn!(
source_id = %source.id,
external_id = %enriched.task.external_id,
diff --git a/crates/openhuman-core/src/integrations/task_sources/pipeline_tests.rs b/crates/openhuman-core/src/integrations/task_sources/pipeline_tests.rs
index be4893ef79..1329b341fe 100644
--- a/crates/openhuman-core/src/integrations/task_sources/pipeline_tests.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/pipeline_tests.rs
@@ -61,7 +61,7 @@ async fn fetch_surfaces_error_for_every_toolkit() {
let config = test_config(&tmp);
let source = add_github_source(&config);
- let outcome = run_source_once(&config, &source, FetchReason::Manual).await;
+ let outcome = run_source_once(&crate::core::runtime::DispatchCtx::host_default(), &config, &source, FetchReason::Manual).await;
assert!(outcome.error.is_some(), "fetch must refuse, not panic");
assert_eq!(outcome.fetched, 0);
assert_eq!(outcome.routed, 0);
@@ -78,8 +78,8 @@ async fn refusal_is_stable_across_repeated_passes() {
let config = test_config(&tmp);
let source = add_github_source(&config);
- let first = run_source_once(&config, &source, FetchReason::Manual).await;
- let second = run_source_once(&config, &source, FetchReason::Manual).await;
+ let first = run_source_once(&crate::core::runtime::DispatchCtx::host_default(), &config, &source, FetchReason::Manual).await;
+ let second = run_source_once(&crate::core::runtime::DispatchCtx::host_default(), &config, &source, FetchReason::Manual).await;
assert!(first.error.is_some());
assert!(second.error.is_some());
assert_eq!(second.routed, 0);
@@ -92,7 +92,7 @@ async fn refusal_records_a_fetch_history_entry() {
let config = test_config(&tmp);
let source = add_github_source(&config);
- let _ = run_source_once(&config, &source, FetchReason::Manual).await;
+ let _ = run_source_once(&crate::core::runtime::DispatchCtx::host_default(), &config, &source, FetchReason::Manual).await;
// `run_source_once` records the failed pass so the UI can show why a
// source has never ingested anything, rather than looking silently idle.
@@ -165,7 +165,7 @@ async fn missing_provider_surfaces_error_in_outcome() {
)
.unwrap();
- let outcome = run_source_once(&config, &source, FetchReason::Manual).await;
+ let outcome = run_source_once(&crate::core::runtime::DispatchCtx::host_default(), &config, &source, FetchReason::Manual).await;
assert!(outcome.error.is_some());
assert_eq!(outcome.routed, 0);
}
diff --git a/crates/openhuman-core/src/integrations/task_sources/route.rs b/crates/openhuman-core/src/integrations/task_sources/route.rs
index 9bef16a755..11e031c79b 100644
--- a/crates/openhuman-core/src/integrations/task_sources/route.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/route.rs
@@ -28,6 +28,7 @@ use super::types::{EnrichedTask, SourceTarget, TaskSource};
/// Route an enriched task: for proactive sources dispatch a triage turn;
/// collect-only sources stop at the ledger the caller already wrote.
pub async fn route_enriched(
+ ctx: &crate::core::runtime::DispatchCtx,
_config: &Config,
source: &TaskSource,
enriched: &EnrichedTask,
@@ -41,18 +42,20 @@ pub async fn route_enriched(
);
Ok(())
}
- SourceTarget::AgentTodoProactive => dispatch_triage(source, enriched).await,
+ SourceTarget::AgentTodoProactive => dispatch_triage(ctx, source, enriched).await,
}
}
/// Dispatch a triage turn for a proactive task, gated by scheduler
/// capacity. A gated-off or deferred turn is non-fatal — the task is already
/// in the ledger.
-async fn dispatch_triage(source: &TaskSource, enriched: &EnrichedTask) -> Result<(), String> {
+async fn dispatch_triage(
+ ctx: &crate::core::runtime::DispatchCtx,
+ source: &TaskSource,
+ enriched: &EnrichedTask,
+) -> Result<(), String> {
// Respect background-AI throttling. When the gate denies capacity
// (Off / paused), we keep the card but skip the proactive turn.
- // Entry point of a scheduler-driven turn: bind the calling task's context once.
- let ctx = crate::core::runtime::DispatchCtx::ambient();
let Some(_permit) = scheduler_gate::wait_for_capacity().await else {
tracing::info!(
source_id = %source.id,
diff --git a/crates/openhuman-core/src/integrations/task_sources/route_tests.rs b/crates/openhuman-core/src/integrations/task_sources/route_tests.rs
index 8e8cd2ab80..0801de810d 100644
--- a/crates/openhuman-core/src/integrations/task_sources/route_tests.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/route_tests.rs
@@ -55,7 +55,7 @@ async fn collect_only_target_routes_without_side_effects() {
let config = Config::default();
let src = github_source(SourceTarget::TodoOnly);
- route_enriched(&config, &src, &enriched("123"))
+ route_enriched(&crate::core::runtime::DispatchCtx::host_default(), &config, &src, &enriched("123"))
.await
.expect("collect-only routing is a no-op");
}
diff --git a/crates/openhuman-core/src/integrations/task_sources/schemas.rs b/crates/openhuman-core/src/integrations/task_sources/schemas.rs
index a1cffe525f..65e69ac74a 100644
--- a/crates/openhuman-core/src/integrations/task_sources/schemas.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/schemas.rs
@@ -469,14 +469,14 @@ fn handle_fetch(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFutur
Box::pin(async move {
let config = ctx.load_config().await?;
let id = read_required::<String>(&params, "id")?;
- to_json(ops::fetch(&config, id.trim()).await?)
+ to_json(ops::fetch(&ctx, &config, id.trim()).await?)
})
}
fn handle_sync(ctx: DispatchCtx, _params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let config = ctx.load_config().await?;
- to_json(ops::sync(&config).await?)
+ to_json(ops::sync(&ctx, &config).await?)
})
}
diff --git a/crates/openhuman-core/src/integrations/task_sources/tools.rs b/crates/openhuman-core/src/integrations/task_sources/tools.rs
index 3379097a80..2ba7f22cd8 100644
--- a/crates/openhuman-core/src/integrations/task_sources/tools.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/tools.rs
@@ -181,9 +181,20 @@ impl Tool for TaskSourceFetchTool {
}
async fn execute(&self, args: serde_json::Value) -> anyhow::Result<ToolResult> {
+ self.execute_with_context(args, tinytools::ToolCallOptions::default(), None)
+ .await
+ }
+
+ async fn execute_with_context(
+ &self,
+ args: serde_json::Value,
+ _options: tinytools::ToolCallOptions,
+ context: Option<&dyn tinytools::ToolRunContext>,
+ ) -> anyhow::Result<ToolResult> {
log::debug!("[tool][task_sources] fetch invoked");
let id = read_required_str(&args, "id")?;
- emit!(ops::fetch(&self.config, &id).await, "task_source_fetch")
+ let ctx = crate::agent::tinyagents::host::run_context::dispatch_ctx_for_tool(context);
+ emit!(ops::fetch(&ctx, &self.config, &id).await, "task_source_fetch")
}
}