Skip to content

The agent that grows with you

fix(agent): provider hook failures are per session and reported per agent

Teknium857e29d4cc5e
diff --git a/agent/provider_route_hooks.py b/agent/provider_route_hooks.py
index 614e033da23..54df74b69f2 100644
--- a/agent/provider_route_hooks.py
+++ b/agent/provider_route_hooks.py
@@ -26,7 +26,8 @@ _PURE_HOOKS = frozenset({"request_headers", "max_tokens_param", "reasoning_paylo
_PURE_MEMO: dict[tuple, Any] = {}
_PURE_MEMO_MAX = 512
_MISSING = object()
-# (profile name, hook) pairs whose failure was already logged: one warning per pair per process.
+# (profile name, hook) pairs whose failure a scopeless (one-shot) caller already logged: one warning per
+# pair per process there; a scoped caller (an agent) warns once per pair per session.
_WARNED: set[tuple] = set()
@@ -96,10 +97,11 @@ def hook_failed(profile: Any, hook: str, scope: Any = None) -> bool:
def ask_profile(profile: Any, hook: str, fallback: Any, *args: Any, scope: Any = None, **kwargs: Any) -> Any:
"""*profile*'s answer to the per-request *hook* (documented-pure hooks are memoized).
- ONE failure policy for every per-request profile hook: a failure is logged once per (profile, hook)
- per process and answers *fallback* (the generic answer); *scope* (the agent) then keeps answering
- *fallback* without asking again, so a session's request bytes never flip between turns after a
- plugin bug or a lost plugin host. ``scope=None`` (one-shot auxiliary calls) asks again next call.
+ ONE failure policy for every per-request profile hook: a failure answers *fallback* (the generic
+ answer) and *scope* (the agent) keeps answering it without asking again until its session resets, so
+ a session's request bytes never flip between turns after a plugin bug or a lost plugin host. It is
+ logged once per (profile, hook) per session, so each served profile's agent reports its own
+ degradation. ``scope=None`` (one-shot auxiliary calls) asks again next call, warning once per process.
"""
failed = _failures(scope)
sticky = _sticky_key(profile, hook)
@@ -109,7 +111,7 @@ def ask_profile(profile: Any, hook: str, fallback: Any, *args: Any, scope: Any =
return _ask(profile, hook, args, kwargs)
except Exception as exc: # plugin code: a broken hook degrades to generic behaviour, never fails the request
failed.add(sticky)
- if sticky[2:] not in _WARNED:
+ if scope is not None or sticky[2:] not in _WARNED:
_WARNED.add(sticky[2:])
logger.warning("provider %s: %s hook failed (%s: %s); using the generic answer for the rest of "
"the session", sticky[2], hook, type(exc).__name__, exc, exc_info=True)
diff --git a/run_agent.py b/run_agent.py
index c6b2267fd4e..571599600cd 100644
--- a/run_agent.py
+++ b/run_agent.py
@@ -406,6 +406,8 @@ class AIAgent(
self._compaction_prompt_drift_logged = False
# Who wrote the current turn. build_turn_context() sets it at the start of every turn.
self._turn_author = None
+ # Provider hooks that failed in the old session (agent.provider_route_hooks) are asked again.
+ self._provider_hook_failures = set()
# True for the first API call of a user turn, False for tool-loop follow-ups (provider request_headers).
self._is_user_initiated_turn = False
diff --git a/tests/agent/test_provider_hook_policy.py b/tests/agent/test_provider_hook_policy.py
index 58701fd95f4..f29e5a28d61 100644
--- a/tests/agent/test_provider_hook_policy.py
+++ b/tests/agent/test_provider_hook_policy.py
@@ -71,6 +71,27 @@ def _replay(self, values, *, model, base_url):
return {k: values[k] for k in ("hp_opaque", "hp_text") if values.get(k)}
+def test_a_failed_hook_warns_per_session_and_is_asked_again_after_a_reset(make_profile, caplog):
+ from agent.provider_route_hooks import ask_profile
+
+ calls = []
+
+ def flaky(self, *, base_url, model="", user_initiated=False):
+ calls.append(model)
+ raise RuntimeError("plugin bug")
+
+ profile = make_profile(request_headers=flaky)
+ first, second = _agent(), _agent()
+ with caplog.at_level(logging.WARNING, logger="agent.provider_route_hooks"):
+ for agent in (first, first, second):
+ assert ask_profile(profile, "request_headers", {}, base_url=HOST, model="m", scope=agent) == {}
+ first.reset_session_state()
+ assert ask_profile(profile, "request_headers", {}, base_url=HOST, model="m", scope=first) == {}
+ assert len(calls) == 3 # first once (sticky), second once, first again after /new
+ warned = [r for r in caplog.records if "request_headers hook failed" in r.getMessage()]
+ assert len(warned) == 3 # one per session, not one per process
+
+
def test_long_history_asks_the_replay_hook_once_per_carrier_key_set(make_profile):
calls = []