Skip to content

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

fix(inference): budgets apply without a model; auth errors stay in their scope

M3gA-Mindf18ea9719cf9
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/budget_gate.rs b/crates/openhuman-core/src/agent/tinyagents/host/budget_gate.rs
index 36121a6158..8811a6de75 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/budget_gate.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/budget_gate.rs
@@ -237,12 +237,15 @@ impl OpenHumanBudgetGate {
} else {
Some(est.model.clone())
};
- let Some(model) = model else {
+ // With no model to name, the call still counts against every budget
+ // that doesn't depend on it (global, thread, agent, user agent); only
+ // a model-scoped budget can't match it.
+ let model = model.unwrap_or_else(|| {
log::debug!(
- "[tinyagents][budget] no model available for estimate; skipping budget check"
+ "[tinyagents][budget] no model for estimate; checking model-independent budgets"
);
- return None;
- };
+ String::new()
+ });
let call = cost::budget::CallUnderCheck {
model: &model,
scope: &scope,
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/budget_gate_tests.rs b/crates/openhuman-core/src/agent/tinyagents/host/budget_gate_tests.rs
index 6a365d6417..334892df20 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/budget_gate_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/budget_gate_tests.rs
@@ -334,3 +334,24 @@ async fn acquire_refuses_an_over_budget_call() {
// The same gate admits an agent the budget does not cover.
assert!(gate.acquire(&estimate_for("orchestrator")).await.is_ok());
}
+
+/// With no `default_model` and an estimate that names none, the call still
+/// counts against budgets that don't depend on the model; skipping them let
+/// every configured cap through.
+#[test]
+fn a_call_with_no_model_is_still_checked_against_its_budgets() {
+ let (_tmp, tracker) = ledger_with(2.0, "planner");
+ let gate = budgeted_gate(crate::config::BudgetAction::Refuse);
+ let gate = OpenHumanBudgetGate::new(Arc::new(Config {
+ default_model: None,
+ ..(*gate.config).clone()
+ }));
+ let estimate = CallEstimate {
+ model: String::new(),
+ ..estimate_for("planner")
+ };
+ let refusal = gate
+ .check_budgets_against(&estimate, &gate.live_budgets(), &tracker)
+ .expect("planner is over its cap whatever the model");
+ assert!(refusal.starts_with("BUDGET_EXCEEDED:"), "{refusal}");
+}
diff --git a/crates/openhuman-core/src/inference/auth_error_registry.rs b/crates/openhuman-core/src/inference/auth_error_registry.rs
index 6b2acca86b..bec1107c49 100644
--- a/crates/openhuman-core/src/inference/auth_error_registry.rs
+++ b/crates/openhuman-core/src/inference/auth_error_registry.rs
@@ -76,14 +76,20 @@ pub fn record(provider: &str, status: u16) -> bool {
changed
}
-/// Clear any recorded auth error for `provider` (the user updated / removed
-/// the key, or a call newly succeeded). Returns `true` if an entry was
-/// removed. Re-arms the [`record`] latch so a fresh failure re-notifies.
+/// Clear the recorded auth error for `provider` in the current scope (the
+/// user updated / removed the key, or a call newly succeeded). Another
+/// scope's failure for the same provider is its own key and stays. Returns
+/// `true` if the entry was removed, which re-arms the [`record`] latch so a
+/// fresh failure re-notifies.
pub fn clear(provider: &str) -> bool {
- origins()
- .lock()
- .unwrap_or_else(|error| error.into_inner())
- .remove(provider);
+ let mut origins = origins().lock().unwrap_or_else(|error| error.into_inner());
+ if let (Some(scope), Some(origin)) = (current_scope(), origins.get_mut(provider)) {
+ origin.scopes.remove(&scope);
+ if !origin.scopes.is_empty() || origin.unscoped {
+ return false;
+ }
+ }
+ origins.remove(provider);
registry().clear(provider)
}
@@ -93,6 +99,24 @@ pub fn snapshot() -> Vec<ProviderAuthError> {
registry().snapshot()
}
+/// The auth errors the current workspace/tenant may see: those it recorded.
+/// Without a bound context (a single-user process) every error is its own.
+pub fn snapshot_current_scope() -> Vec<ProviderAuthError> {
+ let Some(scope) = current_scope() else {
+ return snapshot();
+ };
+ let origins = origins().lock().unwrap_or_else(|error| error.into_inner());
+ registry()
+ .snapshot()
+ .into_iter()
+ .filter(|error| {
+ origins
+ .get(&error.provider)
+ .is_some_and(|origin| origin.scopes.contains(&scope))
+ })
+ .collect()
+}
+
#[derive(Default)]
struct ErrorOrigins {
scopes: HashSet<ScopeIdentity>,
diff --git a/crates/openhuman-core/src/inference/auth_error_registry_tests.rs b/crates/openhuman-core/src/inference/auth_error_registry_tests.rs
index 8395733d22..df6069d9bc 100644
--- a/crates/openhuman-core/src/inference/auth_error_registry_tests.rs
+++ b/crates/openhuman-core/src/inference/auth_error_registry_tests.rs
@@ -234,3 +234,55 @@ fn normal_boot_without_preloaded_config_uses_the_resolved_config_path() {
reset_for_tests();
});
}
+
+/// One process can serve several workspaces/tenants. The settings RPC must
+/// list only the caller's own rejected keys, and one tenant re-keying a
+/// provider must not clear another tenant's notice for it.
+#[test]
+fn auth_errors_stay_in_the_scope_that_recorded_them() {
+ with_test_lock(async {
+ use crate::core::runtime::{CoreContext, DomainSet};
+ reset_for_tests();
+ let workspace_a = tempfile::tempdir().expect("workspace A");
+ let workspace_b = tempfile::tempdir().expect("workspace B");
+ let context_a = CoreContext::for_test(DomainSet::full(), Some(workspace_a.path().into()));
+ let context_b = CoreContext::for_test(DomainSet::full(), Some(workspace_b.path().into()));
+ let rpc_providers = |context: CoreContext| {
+ CoreContext::scope(context, async {
+ let outcome = crate::inference::ops::inference_provider_auth_errors()
+ .await
+ .expect("auth errors rpc");
+ outcome.value["errors"]
+ .as_array()
+ .expect("errors array")
+ .iter()
+ .map(|error| error["provider"].as_str().unwrap_or_default().to_owned())
+ .collect::<Vec<_>>()
+ })
+ };
+
+ CoreContext::scope(context_a.clone(), async {
+ record("tenant-a-only", 401);
+ record("shared-provider", 401);
+ })
+ .await;
+ CoreContext::scope(context_b.clone(), async {
+ record("shared-provider", 401);
+ })
+ .await;
+
+ assert_eq!(rpc_providers(context_b.clone()).await, ["shared-provider"]);
+
+ CoreContext::scope(context_a.clone(), async {
+ clear("shared-provider");
+ })
+ .await;
+ assert_eq!(
+ rpc_providers(context_b).await,
+ ["shared-provider"],
+ "tenant A re-keying cleared tenant B's notice"
+ );
+ assert_eq!(rpc_providers(context_a).await, ["tenant-a-only"]);
+ reset_for_tests();
+ });
+}
diff --git a/crates/openhuman-core/src/inference/ops/oauth.rs b/crates/openhuman-core/src/inference/ops/oauth.rs
index f9a9a1e138..7e8f03bca0 100644
--- a/crates/openhuman-core/src/inference/ops/oauth.rs
+++ b/crates/openhuman-core/src/inference/ops/oauth.rs
@@ -6,7 +6,7 @@ use tracing::{debug, warn};
const LOG_PREFIX: &str = "[inference::ops]";
pub async fn inference_provider_auth_errors() -> Result<Outcome<Value>, String> {
- let errors = crate::inference::auth_error_registry::snapshot();
+ let errors = crate::inference::auth_error_registry::snapshot_current_scope();
debug!(count = errors.len(), "{LOG_PREFIX} provider_auth_errors:ok");
Ok(Outcome::single_log(
json!({ "errors": errors }),