Skip to content

The agent that grows with you

fix(usage): plugin usage hook runs under run_bounded_sync; base no-op spawns no thread

kshitijk4poor144890829b93
diff --git a/agent/account_usage.py b/agent/account_usage.py
index 5f8641e9b67..579346a1426 100644
--- a/agent/account_usage.py
+++ b/agent/account_usage.py
@@ -643,18 +643,17 @@ PLUGIN_USAGE_HOOK_DEADLINE_S = 10.0
def _call_plugin_usage_hook(profile, base_url: Optional[str], api_key: Optional[str]) -> Optional[AccountUsageSnapshot]:
- """Run the profile hook on a daemon thread; past the deadline (or on any exception) → None."""
- import contextvars
- import threading
- result: list = []
- context = contextvars.copy_context() # the hook may read profile-scoped secrets
- worker = threading.Thread(
- target=lambda: result.append(
- context.run(profile.fetch_account_usage, base_url=base_url, api_key=api_key)),
- name="plugin-account-usage", daemon=True)
- worker.start()
- worker.join(PLUGIN_USAGE_HOOK_DEADLINE_S)
- return result[0] if result else None
+ """Run the profile hook under the shared deadline; past it → None. Exceptions re-raise in the
+ caller so ``fetch_account_usage`` fails open without a worker-thread traceback on ``/usage``."""
+ from agent.deadline import run_bounded_sync
+ from providers.base import ProviderProfile
+
+ if type(profile).fetch_account_usage is ProviderProfile.fetch_account_usage:
+ return None # base no-op: no thread to spawn
+ bounded = run_bounded_sync(
+ lambda: profile.fetch_account_usage(base_url=base_url, api_key=api_key),
+ PLUGIN_USAGE_HOOK_DEADLINE_S, label="plugin-account-usage")
+ return None if bounded.timed_out else bounded.value
def fetch_account_usage(
diff --git a/tests/agent/test_account_usage_fetch.py b/tests/agent/test_account_usage_fetch.py
index 93d7adbb72b..e3086d1c4b1 100644
--- a/tests/agent/test_account_usage_fetch.py
+++ b/tests/agent/test_account_usage_fetch.py
@@ -1,5 +1,7 @@
from datetime import datetime, timezone
+import pytest
+
from agent.account_usage import (
AccountUsageSnapshot,
AccountUsageWindow,
@@ -286,3 +288,40 @@ def test_plugin_usage_hook_is_bounded_and_fails_open(monkeypatch):
assert time.monotonic() - t0 < 1.3 and started.is_set()
account_usage.fetch_account_usage("openrouter")
assert builtin_calls == [1]
+
+
+def test_plugin_usage_hook_failure_never_reaches_threading_excepthook(monkeypatch):
+ """A raising hook fails open in the caller — it must not die on a worker thread, where
+ ``threading.excepthook`` prints a traceback into every ``/usage`` surface."""
+ import threading
+
+ from agent import account_usage
+
+ class _Boom(ProviderProfile):
+ def fetch_account_usage(self, *, base_url=None, api_key=None):
+ raise RuntimeError("boom from plugin")
+
+ _register_profile(monkeypatch, _Boom(name="plugin-boom"))
+ monkeypatch.setattr(account_usage, "_USAGE_FETCHERS", {})
+ escaped = []
+ monkeypatch.setattr(threading, "excepthook", lambda args: escaped.append(args.exc_value))
+
+ assert account_usage.fetch_account_usage("plugin-boom") is None
+ for t in threading.enumerate():
+ if t is not threading.current_thread() and "account-usage" in t.name:
+ t.join(2)
+ assert escaped == []
+
+
+def test_base_noop_usage_hook_spawns_no_thread(monkeypatch):
+ """A profile inheriting ``ProviderProfile.fetch_account_usage`` costs no thread."""
+ import threading
+
+ from agent import account_usage
+
+ _register_profile(monkeypatch, ProviderProfile(name="plugin-noop"))
+ monkeypatch.setattr(account_usage, "_USAGE_FETCHERS", {})
+ monkeypatch.setattr(threading.Thread, "start",
+ lambda self: pytest.fail(f"base no-op hook spawned thread {self.name!r}"))
+
+ assert account_usage.fetch_account_usage("plugin-noop") is None