Skip to content

The agent that grows with you

review-fix(locks): keep the lock-free cache read only for manual codex /compress (BASE exception)

Teknium9e3df66e9515
diff --git a/gateway/slash_commands.py b/gateway/slash_commands.py
index 76beb5270a8..ea0ba86699c 100644
--- a/gateway/slash_commands.py
+++ b/gateway/slash_commands.py
@@ -167,17 +167,21 @@ class GatewaySlashCommandsMixin(
async_session_store: AsyncSessionStore
# ------------------------------------------------------------------ shared helpers
- def _cached_agent_for(self, session_key: str):
+ def _cached_agent_for(self, session_key: str, *, lockless_fallback: bool = False):
"""Peek the cached AIAgent for *session_key* without evicting it, or None. Entries are
- ``(agent, signature, ...)`` tuples (bare agents from test doubles accepted). Every historical
- caller read the cache ONLY under ``_agent_cache_lock``; fixtures that skip ``__init__`` (no lock
- or no cache) get None rather than an unlocked read."""
+ ``(agent, signature, ...)`` tuples (bare agents from test doubles accepted). Historical callers
+ read the cache ONLY under ``_agent_cache_lock`` and got None when a fixture that skipped
+ ``__init__`` had no lock; the manual codex ``/compress`` path was the one exception that read
+ lock-free (``lockless_fallback=True``)."""
cache = getattr(self, "_agent_cache", None)
lock = getattr(self, "_agent_cache_lock", None)
- if cache is None or lock is None:
+ if cache is None or (lock is None and not lockless_fallback):
return None
try:
- with lock:
+ if lock:
+ with lock:
+ entry = cache.get(session_key)
+ else:
entry = cache.get(session_key)
except Exception:
return None
diff --git a/gateway/slash_commands_session.py b/gateway/slash_commands_session.py
index e48850a79e9..f6f31054157 100644
--- a/gateway/slash_commands_session.py
+++ b/gateway/slash_commands_session.py
@@ -470,7 +470,7 @@ class GatewaySessionCommandsMixin:
"""
from gateway.run import _AGENT_PENDING_SENTINEL
- agent = self._cached_agent_for(session_key)
+ agent = self._cached_agent_for(session_key, lockless_fallback=True)
if agent is None or agent is _AGENT_PENDING_SENTINEL or getattr(agent, "_codex_session", None) is None:
return (
"🗜️ Nothing to compact: this session runs on the Codex app-server runtime, whose "
diff --git a/tests/agent/test_lock_fallback_base_semantics.py b/tests/agent/test_lock_fallback_base_semantics.py
index eca7dc6cc59..5297e6f9bb8 100644
--- a/tests/agent/test_lock_fallback_base_semantics.py
+++ b/tests/agent/test_lock_fallback_base_semantics.py
@@ -208,3 +208,12 @@ def _duck(outer=None, inner=None, *, has_outer=True, has_inner=True):
)
def test_is_openai_client_closed_truth_table(client, expected):
assert ClientLifecycleMixin._is_openai_client_closed(client) is expected
+
+
+def test_cached_agent_for_lockless_fallback_only_when_requested():
+ # Manual codex /compress historically read the cache lock-free when the lock was absent.
+ runner = _Runner()
+ runner._agent_cache = {"k": ("AGENT", "sig")}
+ runner._agent_cache_lock = None
+ assert GatewaySlashCommandsMixin._cached_agent_for(runner, "k") is None
+ assert GatewaySlashCommandsMixin._cached_agent_for(runner, "k", lockless_fallback=True) == "AGENT"