Skip to content

The agent that grows with you

fix(plugins): drop expired key-less inject refusal marks

unsupportedpastelsc7ce038c969c
diff --git a/gateway/plugin_keyless_inject.py b/gateway/plugin_keyless_inject.py
index c8ed3322c09..8e113cf3c69 100644
--- a/gateway/plugin_keyless_inject.py
+++ b/gateway/plugin_keyless_inject.py
@@ -19,6 +19,7 @@ from __future__ import annotations
import contextlib
import logging
from pathlib import Path
+import threading
import time
from gateway.config import Platform
@@ -26,16 +27,23 @@ from gateway.config import Platform
logger = logging.getLogger("gateway.run")
# One refusal warning per (plugin, kind) per window: a plugin retrying on a timer must not flood the log.
+# (plugin, kind) -> when it last warned; a mark past the window is dropped at the next refusal.
_WARN_WINDOW_S = 300.0
_warned: dict[tuple[str, str], float] = {}
+_warned_lock = threading.Lock() # refusals run on plugin threads
def _refuse(plugin_id: str, kind: str, detail: str) -> None:
now = time.monotonic()
- if now - _warned.get((plugin_id, kind), -_WARN_WINDOW_S) < _WARN_WINDOW_S:
+ with _warned_lock:
+ for key in [key for key, at in _warned.items() if now - at >= _WARN_WINDOW_S]:
+ del _warned[key]
+ repeated = (plugin_id, kind) in _warned
+ if not repeated:
+ _warned[(plugin_id, kind)] = now
+ if repeated:
logger.debug("inject_message from plugin %s refused again: %s", plugin_id, detail)
return
- _warned[(plugin_id, kind)] = now
logger.warning("inject_message from plugin %s has no session key and %s; nothing was injected. Pass "
"session_key= (or origin=) to choose the chat.", plugin_id, detail)
diff --git a/tests/gateway/test_plugin_keyless_inject.py b/tests/gateway/test_plugin_keyless_inject.py
index eaba957052d..7ace0144e86 100644
--- a/tests/gateway/test_plugin_keyless_inject.py
+++ b/tests/gateway/test_plugin_keyless_inject.py
@@ -206,3 +206,21 @@ def test_with_no_calling_session_only_the_one_attached_classic_cli_session_is_pi
if 'WARNING' in line and 'plugin probe-inj has no session key' in line and 'Pass session_key=' in line]
assert len(refusals) == 2, refusals
assert 'no classic CLI session' in refusals[0] and '2 classic CLI sessions' in refusals[1], refusals
+
+
+def test_a_refusal_warns_once_per_window_and_expired_marks_are_dropped(monkeypatch, caplog):
+ """One warning per (plugin, kind) per window; a mark older than the window is dropped on the next
+ refusal, so the map holds only the plugins refused within the last window."""
+ from types import SimpleNamespace
+ from gateway import plugin_keyless_inject as keyless
+ clock = [1000.0]
+ monkeypatch.setattr(keyless, 'time', SimpleNamespace(monotonic=lambda: clock[0]))
+ monkeypatch.setattr(keyless, '_warned', {})
+ with caplog.at_level('WARNING', logger='gateway.run'):
+ keyless._refuse('old', 'none', 'no classic CLI session is attached in its profile')
+ keyless._refuse('old', 'none', 'no classic CLI session is attached in its profile')
+ clock[0] += keyless._WARN_WINDOW_S
+ keyless._refuse('new', 'several', '2 classic CLI sessions are attached in its profile')
+ assert [r.getMessage().split(' has ')[0] for r in caplog.records if r.levelname == 'WARNING'] == [
+ 'inject_message from plugin old', 'inject_message from plugin new']
+ assert keyless._warned == {('new', 'several'): clock[0]}