Keep session cache invalidation signals live across the routes extraction

This commit is contained in:
Rod Boev authored and nesquena-hermes committed 2026-06-26 18:36:07 +00:00
1 parent 47485b3a82
commit 6cb55b5df8
4 files changed
+62 -7

No files matched your search

+3 -1
View File
@@ -368,7 +368,9 @@ def _session_list_cache_settings_write_version() -> int:
try:
import api.routes as _routes
return int(getattr(_routes, "_session_list_cache_settings_write_version", lambda: 0)())
override = getattr(_routes, "_session_list_cache_settings_write_version", None)
if callable(override) and override is not _session_list_cache_settings_write_version:
return int(override())
except Exception:
pass
try:
+12 -6
View File
@@ -1637,12 +1637,6 @@ def _clear_live_models_cache() -> None:
from api import route_session_list_cache as _route_session_list_cache
_SESSIONS_CACHE = _route_session_list_cache._SESSIONS_CACHE
_SESSIONS_CACHE_ALL_PROFILES_INVALIDATION_VERSION = (
_route_session_list_cache._SESSIONS_CACHE_ALL_PROFILES_INVALIDATION_VERSION
)
_SESSIONS_CACHE_GLOBAL_INVALIDATION_VERSION = (
_route_session_list_cache._SESSIONS_CACHE_GLOBAL_INVALIDATION_VERSION
)
_SESSIONS_CACHE_INFLIGHT = _route_session_list_cache._SESSIONS_CACHE_INFLIGHT
_SESSIONS_CACHE_LOCK = _route_session_list_cache._SESSIONS_CACHE_LOCK
_SESSIONS_CACHE_MAX_ENTRIES = _route_session_list_cache._SESSIONS_CACHE_MAX_ENTRIES
@@ -1674,6 +1668,18 @@ _session_list_cache_source_stamp = _route_session_list_cache._session_list_cache
_session_list_cache_state_db_fingerprint = _route_session_list_cache._session_list_cache_state_db_fingerprint
_session_list_cache_streaming_freeze_marker = _route_session_list_cache._session_list_cache_streaming_freeze_marker
_ROUTE_SESSION_LIST_CACHE_DYNAMIC_EXPORTS = {
"_SESSIONS_CACHE_ALL_PROFILES_INVALIDATION_VERSION",
"_SESSIONS_CACHE_GLOBAL_INVALIDATION_VERSION",
"_session_list_cache_settings_write_version",
}
def __getattr__(name):
if name in _ROUTE_SESSION_LIST_CACHE_DYNAMIC_EXPORTS:
return getattr(_route_session_list_cache, name)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
def _build_session_list_cache_payload(
active_profile: str | None,
@@ -51,6 +51,18 @@ def test_shared_cache_state_mutation():
routes._session_list_cache_clear()
def test_live_scalar_exports_follow_route_session_list_cache_state():
from api import route_session_list_cache as slc
import api.routes as routes
before = routes._SESSIONS_CACHE_GLOBAL_INVALIDATION_VERSION
routes._session_list_cache_clear()
after = routes._SESSIONS_CACHE_GLOBAL_INVALIDATION_VERSION
assert after == slc._SESSIONS_CACHE_GLOBAL_INVALIDATION_VERSION
assert after == before + 1
def test_no_circular_import():
import pathlib
+35
View File
@@ -3,6 +3,7 @@ from types import SimpleNamespace
import pytest
import api.config as config
import api.routes as routes
from api import session_events
@@ -395,6 +396,40 @@ def test_session_list_cache_source_stamp_tracks_settings_file(tmp_path, monkeypa
assert after != before
def test_session_list_cache_source_stamp_tracks_settings_write_version(
tmp_path,
monkeypatch,
):
state_db = tmp_path / "state.db"
state_db.write_text("db", encoding="utf-8")
gateway = tmp_path / "gateway-sessions.json"
gateway.write_text("{}", encoding="utf-8")
session_dir = tmp_path / "sessions"
session_dir.mkdir()
(session_dir / "_index.json").write_text("{}", encoding="utf-8")
settings_file = tmp_path / "settings.json"
settings_file.write_text("{}", encoding="utf-8")
monkeypatch.setattr(routes, "_active_state_db_path", lambda: str(state_db))
monkeypatch.setattr(routes, "_gateway_session_metadata_path", lambda: gateway)
monkeypatch.setattr(routes, "SESSION_DIR", session_dir)
monkeypatch.setattr(routes, "SETTINGS_FILE", settings_file)
key = routes._session_list_cache_key(
active_profile="default",
all_profiles=False,
show_cli_sessions=True,
show_previous_messaging_sessions=False,
show_cron_sessions=False,
)
before = routes._session_list_cache_source_stamp(key)
monkeypatch.setattr(config, "_SETTINGS_WRITE_VERSION", config._SETTINGS_WRITE_VERSION + 1)
after = routes._session_list_cache_source_stamp(key)
assert after != before
def test_session_list_payload_to_response_overlays_live_stream_runtime(monkeypatch):
payload = {
"sessions": [