Skip to content

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

feat(web_chat): correlate budget usage with parallel turns

Steven Enamakel31705ba919cd
diff --git a/crates/openhuman-core/src/web_chat/ops/budget_correlation.rs b/crates/openhuman-core/src/web_chat/ops/budget_correlation.rs
index 3de3895fa9..2874774d45 100644
--- a/crates/openhuman-core/src/web_chat/ops/budget_correlation.rs
+++ b/crates/openhuman-core/src/web_chat/ops/budget_correlation.rs
@@ -68,11 +68,11 @@ fn prune_stale_budget_signals(signals: &mut HashMap<String, BudgetSignal>) {
/// Record that this thread just hit an inference budget-exhausted error on the
/// given provider binding.
-pub(crate) async fn record_budget_signal(thread_id: &str, provider_binding: &str) {
+pub(crate) async fn record_budget_signal(ctx: &DispatchCtx, thread_id: &str, provider_binding: &str) {
let mut signals = thread_budget_signals().lock_owned().await;
prune_stale_budget_signals(&mut signals);
signals.insert(
- key_for(thread_id),
+ key_for(ctx, thread_id),
BudgetSignal {
provider_binding: provider_binding.to_string(),
at: Instant::now(),
@@ -82,18 +82,18 @@ pub(crate) async fn record_budget_signal(thread_id: &str, provider_binding: &str
/// Clear any recorded budget signal for this thread — called on a successful
/// turn, where the balance is evidently usable again.
-pub(crate) async fn clear_budget_signal(thread_id: &str) {
+pub(crate) async fn clear_budget_signal(ctx: &DispatchCtx, thread_id: &str) {
let mut signals = thread_budget_signals().lock_owned().await;
- signals.remove(&key_for(thread_id));
+ signals.remove(&key_for(ctx, thread_id));
}
/// Whether this thread has a budget signal recorded within `BUDGET_SIGNAL_TTL`
/// **on the same provider binding** as the current turn. A binding mismatch or
/// an expired entry evicts it and reads as not-fresh, so a re-routed turn never
/// inherits the prior provider's exhaustion.
-pub(crate) async fn has_fresh_budget_signal(thread_id: &str, provider_binding: &str) -> bool {
+pub(crate) async fn has_fresh_budget_signal(ctx: &DispatchCtx, thread_id: &str, provider_binding: &str) -> bool {
let mut signals = thread_budget_signals().lock_owned().await;
- let key = key_for(thread_id);
+ let key = key_for(ctx, thread_id);
match signals.get(&key) {
Some(sig)
if sig.provider_binding == provider_binding
@@ -112,11 +112,11 @@ pub(crate) async fn has_fresh_budget_signal(thread_id: &str, provider_binding: &
/// Test-only seeder: record a budget signal on `provider_binding` aged `age`
/// into the past so expiry can be exercised without sleeping.
#[cfg(test)]
-async fn record_budget_signal_aged(thread_id: &str, provider_binding: &str, age: Duration) {
+async fn record_budget_signal_aged(ctx: &DispatchCtx, thread_id: &str, provider_binding: &str, age: Duration) {
let mut signals = thread_budget_signals().lock_owned().await;
let when = Instant::now().checked_sub(age).unwrap_or_else(Instant::now);
signals.insert(
- key_for(thread_id),
+ key_for(ctx, thread_id),
BudgetSignal {
provider_binding: provider_binding.to_string(),
at: when,
diff --git a/crates/openhuman-core/src/web_chat/ops/parallel_turn.rs b/crates/openhuman-core/src/web_chat/ops/parallel_turn.rs
index a137dc9815..8037a05c8d 100644
--- a/crates/openhuman-core/src/web_chat/ops/parallel_turn.rs
+++ b/crates/openhuman-core/src/web_chat/ops/parallel_turn.rs
@@ -45,7 +45,7 @@ pub(crate) async fn spawn_parallel_turn(
let request_id_task = request_id.clone();
// Request and thread ids are caller-chosen and only unique per agent, so
// the table is keyed in the caller's scope (see `key_for`).
- let map_key = key_for(&request_id);
+ let map_key = key_for(ctx, &request_id);
let map_key_task = map_key.clone();
let user_message = message.to_string();
let ctx_task = ctx.clone();
@@ -169,7 +169,7 @@ pub(crate) async fn spawn_parallel_turn(
);
}
- publish_web_channel_event(WebChannelEvent {
+ publish_web_channel_event(&ctx_task, WebChannelEvent {
event: "chat_error".to_string(),
client_id: client_id_task.clone(),
thread_id: thread_id_task.clone(),
@@ -196,7 +196,7 @@ pub(crate) async fn spawn_parallel_turn(
// `chat_error` on this path today — leaving a client
// waiting on this request_id with no terminal event.
// `chat_cancelled` closes it out.
- publish_web_channel_event(WebChannelEvent {
+ publish_web_channel_event(&ctx_task, WebChannelEvent {
event: "chat_cancelled".to_string(),
client_id: client_id_task.clone(),
thread_id: thread_id_task.clone(),
@@ -217,7 +217,7 @@ pub(crate) async fn spawn_parallel_turn(
parallel_in_flight().lock_owned().await.insert(
map_key,
ParallelEntry {
- thread_id: key_for(thread_id),
+ thread_id: key_for(ctx, thread_id),
client_id: client_id.to_string(),
handle,
cancel_token,
@@ -228,9 +228,12 @@ pub(crate) async fn spawn_parallel_turn(
/// Cooperatively cancel every parallel turn on a thread. Returns the cancelled
/// `(request_id, client_id)` pairs. Used by the thread-level cancel paths so a cancel/stop also
/// tears down any concurrent forked turns, not just the primary turn.
-pub(crate) async fn cancel_parallel_turns_for_thread(thread_id: &str) -> Vec<(String, String)> {
+pub(crate) async fn cancel_parallel_turns_for_thread(
+ ctx: &crate::core::runtime::DispatchCtx,
+ thread_id: &str,
+) -> Vec<(String, String)> {
let mut cancelled = Vec::new();
- let scoped_thread = key_for(thread_id);
+ let scoped_thread = key_for(ctx, thread_id);
let mut parallel = parallel_in_flight().lock_owned().await;
let request_ids: Vec<String> = parallel
.iter()
@@ -249,7 +252,7 @@ pub(crate) async fn cancel_parallel_turns_for_thread(thread_id: &str) -> Vec<(St
}
}
});
- cancelled.push((unscope(&request_id), entry.client_id));
+ cancelled.push((unscope(ctx, &request_id), entry.client_id));
}
}
cancelled
@@ -260,11 +263,12 @@ pub(crate) async fn cancel_parallel_turns_for_thread(thread_id: &str) -> Vec<(St
/// vec of `(request_id, client_id)`, mirroring [`cancel_parallel_turns_for_thread`]) or empty when no such
/// parallel turn exists. Request-scoped cancel path (#4760).
pub(crate) async fn cancel_parallel_turn_by_request_id(
+ ctx: &crate::core::runtime::DispatchCtx,
thread_id: &str,
request_id: &str,
) -> Vec<(String, String)> {
- let map_key = key_for(request_id);
- let scoped_thread = key_for(thread_id);
+ let map_key = key_for(ctx, request_id);
+ let scoped_thread = key_for(ctx, thread_id);
let mut parallel = parallel_in_flight().lock_owned().await;
let matches = parallel
.get(&map_key)