Skip to content

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

fix(inference): model budgets skip a model-less call; auth errors keep each scope's status

M3gA-Mind65c96096369c
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 8811a6de75..1290bf1cf1 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/budget_gate.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/budget_gate.rs
@@ -238,8 +238,8 @@ impl OpenHumanBudgetGate {
Some(est.model.clone())
};
// 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.
+ // that doesn't depend on it (global, thread, agent, user agent);
+ // model-scoped budgets skip an empty model (`budget::bucket_of`).
let model = model.unwrap_or_else(|| {
log::debug!(
"[tinyagents][budget] no model for estimate; checking model-independent budgets"
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 334892df20..c45ac8e393 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
@@ -355,3 +355,33 @@ fn a_call_with_no_model_is_still_checked_against_its_budgets() {
.expect("planner is over its cap whatever the model");
assert!(refusal.starts_with("BUDGET_EXCEEDED:"), "{refusal}");
}
+
+/// ...but a model-scoped budget, which has no model to match, does not count it.
+#[test]
+fn a_call_with_no_model_is_outside_model_scoped_budgets() {
+ let (_tmp, tracker) = ledger_with(0.0, "planner");
+ let mut config = config_without_file();
+ config.default_model = None;
+ config.cost.budgets = vec![crate::config::BudgetPolicy {
+ name: Some("per-model token cap".into()),
+ scope: crate::config::BudgetScope::Model,
+ matches: None,
+ period: crate::config::BudgetPeriod::Month,
+ max_usd: None,
+ max_tokens: Some(1),
+ warn_fraction: 0.8,
+ action: crate::config::BudgetAction::Refuse,
+ }];
+ let gate = OpenHumanBudgetGate::new(Arc::new(config));
+ let estimate = CallEstimate {
+ model: String::new(),
+ estimated_input_tokens: 10,
+ estimated_output_tokens: 10,
+ ..estimate_for("planner")
+ };
+ assert!(
+ gate.check_budgets_against(&estimate, &gate.live_budgets(), &tracker)
+ .is_none(),
+ "a model budget refused a call that names no model"
+ );
+}
diff --git a/crates/openhuman-core/src/inference/auth_error_registry.rs b/crates/openhuman-core/src/inference/auth_error_registry.rs
index bec1107c49..e82afc2c33 100644
--- a/crates/openhuman-core/src/inference/auth_error_registry.rs
+++ b/crates/openhuman-core/src/inference/auth_error_registry.rs
@@ -19,7 +19,7 @@
//! and an unguarded publish would re-flood the notification center the same
//! way the raw error flooded Sentry.
-use std::collections::{HashMap, HashSet};
+use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};
use std::time::{SystemTime, UNIX_EPOCH};
@@ -59,17 +59,23 @@ pub fn auth_error_message(provider: &str, status: u16) -> String {
/// a 401 that repeats per-retry doesn't re-notify. An existing entry is
/// refreshed (status / timestamp) and returns `false`.
pub fn record(provider: &str, status: u16) -> bool {
- let scope = current_scope();
+ let timestamp_ms = now_ms();
let changed = registry().record(ProviderAuthError {
provider: provider.to_string(),
status,
message: auth_error_message(provider, status),
- timestamp_ms: now_ms(),
+ timestamp_ms,
});
let mut origins = origins().lock().unwrap_or_else(|error| error.into_inner());
let entry = origins.entry(provider.to_owned()).or_default();
- if let Some(scope) = scope {
- entry.scopes.insert(scope);
+ if let CurrentScope::Bound(scope) = current_scope() {
+ entry.scopes.insert(
+ scope,
+ ScopedFailure {
+ status,
+ timestamp_ms,
+ },
+ );
} else {
entry.unscoped = true;
}
@@ -83,11 +89,22 @@ pub fn record(provider: &str, status: u16) -> bool {
/// fresh failure re-notifies.
pub fn clear(provider: &str) -> bool {
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 {
+ match current_scope() {
+ CurrentScope::Unbound => {}
+ CurrentScope::Unresolved(error) => {
+ log::warn!(
+ "[auth-errors] clear provider={provider} skipped: scope unresolved ({error})"
+ );
return false;
}
+ CurrentScope::Bound(scope) => {
+ if let Some(origin) = origins.get_mut(provider) {
+ origin.scopes.remove(&scope);
+ if !origin.scopes.is_empty() || origin.unscoped {
+ return false;
+ }
+ }
+ }
}
origins.remove(provider);
registry().clear(provider)
@@ -99,30 +116,60 @@ 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();
+/// The auth errors the current workspace/tenant may see: those it recorded,
+/// with its own status and time. Without a bound context (a single-user
+/// process) every error is its own; a bound context whose workspace can't be
+/// resolved gets an error rather than every scope's failures.
+pub fn snapshot_current_scope() -> Result<Vec<ProviderAuthError>, String> {
+ let scope = match current_scope() {
+ CurrentScope::Unbound => return Ok(snapshot()),
+ CurrentScope::Unresolved(error) => return Err(error),
+ CurrentScope::Bound(scope) => scope,
};
let origins = origins().lock().unwrap_or_else(|error| error.into_inner());
- registry()
+ Ok(registry()
.snapshot()
.into_iter()
- .filter(|error| {
- origins
- .get(&error.provider)
- .is_some_and(|origin| origin.scopes.contains(&scope))
+ .filter_map(|error| {
+ let own = origins.get(&error.provider)?.scopes.get(&scope)?;
+ Some(own.view(error))
})
- .collect()
+ .collect())
}
#[derive(Default)]
struct ErrorOrigins {
- scopes: HashSet<ScopeIdentity>,
+ /// Each scope's own latest failure for the provider.
+ scopes: HashMap<ScopeIdentity, ScopedFailure>,
unscoped: bool,
}
+#[derive(Clone, Copy)]
+struct ScopedFailure {
+ status: u16,
+ timestamp_ms: u64,
+}
+
+impl ScopedFailure {
+ /// `error` as this scope saw it.
+ fn view(self, error: ProviderAuthError) -> ProviderAuthError {
+ ProviderAuthError {
+ message: auth_error_message(&error.provider, self.status),
+ status: self.status,
+ timestamp_ms: self.timestamp_ms,
+ provider: error.provider,
+ }
+ }
+}
+
+enum CurrentScope {
+ /// No core context is bound (a single-user process).
+ Unbound,
+ /// A bound context whose workspace binding could not be read.
+ Unresolved(String),
+ Bound(ScopeIdentity),
+}
+
#[derive(Clone, PartialEq, Eq, Hash)]
struct ScopeIdentity {
workspace: std::path::PathBuf,
@@ -135,14 +182,18 @@ fn origins() -> &'static Mutex<HashMap<String, ErrorOrigins>> {
ORIGINS.get_or_init(|| Mutex::new(HashMap::new()))
}
-fn current_scope() -> Option<ScopeIdentity> {
- let context = crate::core::runtime::CoreContext::current()?;
- let (workspace, config_path) = context.workspace_config_binding().ok()?;
- Some(ScopeIdentity {
- workspace,
- config_path,
- tenant: context.profile().unwrap_or_default().to_owned(),
- })
+fn current_scope() -> CurrentScope {
+ let Some(context) = crate::core::runtime::CoreContext::current() else {
+ return CurrentScope::Unbound;
+ };
+ match context.workspace_config_binding() {
+ Ok((workspace, config_path)) => CurrentScope::Bound(ScopeIdentity {
+ workspace,
+ config_path,
+ tenant: context.profile().unwrap_or_default().to_owned(),
+ }),
+ Err(error) => CurrentScope::Unresolved(error),
+ }
}
/// Auth failures scoped to the current workspace/tenant. Known foreign
@@ -167,7 +218,7 @@ pub fn snapshot_for_scope(
let Some(origin) = origins.get(&error.provider) else {
return false;
};
- let partial_match = origin.scopes.iter().any(|recorded| {
+ let partial_match = origin.scopes.keys().any(|recorded| {
recorded.config_path.is_none()
&& recorded.workspace == scope.workspace
&& recorded.tenant == scope.tenant
@@ -175,7 +226,7 @@ pub fn snapshot_for_scope(
if partial_match {
has_unscoped = true;
}
- if origin.scopes.contains(&scope) {
+ if origin.scopes.contains_key(&scope) {
return true;
}
if !partial_match {
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 2146527fb2..133706d967 100644
--- a/crates/openhuman-core/src/inference/auth_error_registry_tests.rs
+++ b/crates/openhuman-core/src/inference/auth_error_registry_tests.rs
@@ -256,7 +256,12 @@ fn auth_errors_stay_in_the_scope_that_recorded_them() {
.as_array()
.expect("errors array")
.iter()
- .map(|error| error["provider"].as_str().unwrap_or_default().to_owned())
+ .map(|error| {
+ (
+ error["provider"].as_str().unwrap_or_default().to_owned(),
+ error["status"].as_u64().unwrap_or_default(),
+ )
+ })
.collect::<Vec<_>>()
})
};
@@ -267,11 +272,22 @@ fn auth_errors_stay_in_the_scope_that_recorded_them() {
})
.await;
CoreContext::scope(context_b.clone(), async {
- record("shared-provider", 401);
+ record("shared-provider", 403);
})
.await;
- assert_eq!(rpc_providers(context_b.clone()).await, ["shared-provider"]);
+ // Each scope sees its own failures, with its own status.
+ assert_eq!(
+ rpc_providers(context_b.clone()).await,
+ [("shared-provider".to_owned(), 403)]
+ );
+ assert_eq!(
+ rpc_providers(context_a.clone()).await,
+ [
+ ("shared-provider".to_owned(), 401),
+ ("tenant-a-only".to_owned(), 401)
+ ]
+ );
CoreContext::scope(context_a.clone(), async {
clear("shared-provider");
@@ -279,10 +295,13 @@ fn auth_errors_stay_in_the_scope_that_recorded_them() {
.await;
assert_eq!(
rpc_providers(context_b).await,
- ["shared-provider"],
+ [("shared-provider".to_owned(), 403)],
"tenant A re-keying cleared tenant B's notice"
);
- assert_eq!(rpc_providers(context_a).await, ["tenant-a-only"]);
+ assert_eq!(
+ rpc_providers(context_a).await,
+ [("tenant-a-only".to_owned(), 401)]
+ );
reset_for_tests();
});
}
diff --git a/crates/openhuman-core/src/inference/ops/oauth.rs b/crates/openhuman-core/src/inference/ops/oauth.rs
index 7e8f03bca0..0e5f0e8cbd 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_current_scope();
+ 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 }),
diff --git a/crates/openhuman-core/src/platform/cost/budget.rs b/crates/openhuman-core/src/platform/cost/budget.rs
index c699731da0..a2a410f329 100644
--- a/crates/openhuman-core/src/platform/cost/budget.rs
+++ b/crates/openhuman-core/src/platform/cost/budget.rs
@@ -108,7 +108,8 @@ pub fn earliest_start(policies: &[BudgetPolicy], now: DateTime<Utc>) -> Option<D
fn bucket_of<'a>(scope: BudgetScope, model: &'a str, usage: &'a UsageScope) -> Option<&'a str> {
match scope {
BudgetScope::Global => Some("*"),
- BudgetScope::Model => Some(model),
+ // A call (or record) with no model is outside every model budget.
+ BudgetScope::Model => (!model.is_empty()).then_some(model),
BudgetScope::Thread => usage.thread_id.as_deref(),
BudgetScope::Agent => usage.agent_id.as_deref(),
BudgetScope::SessionAgent => usage.session_agent.as_deref(),