The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
fix(cost): track global spend per scope instead of a single total
Steven Enamakelaeef4a6232ea
diff --git a/crates/openhuman-core/src/platform/cost/global.rs b/crates/openhuman-core/src/platform/cost/global.rsindex 41429e1be4..85a25f4531 100644--- a/crates/openhuman-core/src/platform/cost/global.rs+++ b/crates/openhuman-core/src/platform/cost/global.rs@@ -126,10 +126,47 @@ pub fn rebind_global(config: CostConfig, workspace_dir: &Path) {/// Fetch the global tracker if it has been initialised. Returns `None`/// before bootstrap or after an init failure — callers must treat the/// absence as a soft no-op.+///+/// In SaaS this is the calling tenant's own tracker+/// ([`seed_tenant_tracker`]), never the process-wide one: one user's spend+/// must not land in, or be read from, another's ledger. A SaaS task with no+/// scope, or the operator, gets `None`.pub fn try_global() -> Option<Arc<CostTracker>> {+ let saas = crate::core::runtime::is_saas();+ if saas {+ return tracker_in(crate::core::runtime::tenant::context_in(saas).as_deref());+ }GLOBAL_TRACKER.read().clone()}+/// A SaaS tenant's cost tracker, held in its context's state slots.+#[derive(Default)]+pub(crate) struct TenantTracker(RwLock<Option<Arc<CostTracker>>>);++/// Give `ctx` (a SaaS profile's context) a cost tracker over the profile's+/// own workspace. Called when the profile opens; a failure leaves it without+/// one (its usage goes unrecorded) rather than borrowing another ledger.+pub fn seed_tenant_tracker(ctx: &crate::core::runtime::CoreContext, config: &crate::config::Config) {+ match CostTracker::new(config.cost.clone(), &config.workspace_dir) {+ Ok(tracker) => {+ *ctx.agent_state().slot::<TenantTracker>().0.write() = Some(Arc::new(tracker));+ log::debug!(+ "[cost] tenant tracker seeded workspace={}",+ config.workspace_dir.display()+ );+ }+ Err(err) => log::warn!(+ "[cost] could not seed the tenant tracker at {}: {err}",+ config.workspace_dir.display()+ ),+ }+}++/// The tracker `ctx` was seeded with; `None` without a context.+pub(crate) fn tracker_in(ctx: Option<&crate::core::runtime::CoreContext>) -> Option<Arc<CostTracker>> {+ ctx.and_then(|ctx| ctx.agent_state().slot::<TenantTracker>().0.read().clone())+}+/// Convenience hook used by the agent turn loop: translates a provider/// [`BilledUsage`] into a [`TokenUsage`] record and persists it via the/// global tracker. Silently skipped when the tracker is uninitialised.diff --git a/crates/openhuman-core/src/platform/cost/scope.rs b/crates/openhuman-core/src/platform/cost/scope.rsindex c105ae2103..39c44d668b 100644--- a/crates/openhuman-core/src/platform/cost/scope.rs+++ b/crates/openhuman-core/src/platform/cost/scope.rs@@ -20,8 +20,9 @@ impl UsageScope {let origin = crate::core::runtime::CoreContext::current_turn_origin();let (thread_id, origin) = describe_origin(origin.as_ref());let definition = crate::memory::scope::current().and_then(|identity| identity.agent_id);- let session_agent = crate::core::runtime::CoreContext::current()- .and_then(|ctx| ctx.session_agent().map(str::to_owned));+ let session_agent = crate::core::runtime::current_tenant()+ .ok()+ .and_then(|tenant| tenant.agent);let (agent_id, subagent_task_id) = match subagent {Some((agent, task)) => (Some(agent.to_string()), Some(task.to_string())),None => (definition, None),