Skip to content

The agent that grows with you

refactor(gateway): session — hug short parameter/argument lists

Teknium4f943a1cddf1
diff --git a/gateway/session.py b/gateway/session.py
index d2d87f066d7..0aedf130daf 100644
--- a/gateway/session.py
+++ b/gateway/session.py
@@ -852,10 +852,7 @@ class AsyncSessionStore:
class SessionStore(
- SessionPersistenceMixin,
- SessionRecoveryMixin,
- SessionLifecycleMixin,
- SessionTranscriptMixin,
+ SessionPersistenceMixin, SessionRecoveryMixin, SessionLifecycleMixin, SessionTranscriptMixin,
):
"""Session storage/retrieval: SQLite (SessionDB) for metadata and
transcripts, legacy JSONL fallback when SQLite is unavailable."""
@@ -972,10 +969,7 @@ class SessionStore(
return len(self._entries) > 1
def get_or_create_session(
- self,
- source: SessionSource,
- force_new: bool = False,
- touch_activity: bool = True,
+ self, source: SessionSource, force_new: bool = False, touch_activity: bool = True,
) -> SessionEntry:
"""Single-flight session lookup/create per routing key.
@@ -1018,10 +1012,7 @@ class SessionStore(
self._inflight_sessions.pop(session_key, None)
def _get_or_create_session_impl(
- self,
- source: SessionSource,
- force_new: bool = False,
- touch_activity: bool = True,
+ self, source: SessionSource, force_new: bool = False, touch_activity: bool = True,
) -> SessionEntry:
"""Perform one session routing transition for the single-flight owner.
@@ -1197,10 +1188,7 @@ class SessionStore(
)
def update_session(
- self,
- session_key: str,
- last_prompt_tokens: int = None,
- touch_activity: bool = True,
+ self, session_key: str, last_prompt_tokens: int = None, touch_activity: bool = True,
) -> None:
"""Update lightweight session metadata after an interaction.
@@ -1373,9 +1361,7 @@ class SessionStore(
def build_session_context(
- source: SessionSource,
- config: GatewayConfig,
- session_entry: Optional[SessionEntry] = None
+ source: SessionSource, config: GatewayConfig, session_entry: Optional[SessionEntry] = None
) -> SessionContext:
"""Build a full session context (for system prompt injection)."""
connected = config.get_connected_platforms()
diff --git a/gateway/session_lifecycle.py b/gateway/session_lifecycle.py
index 0a77992e104..e583ee27ec9 100644
--- a/gateway/session_lifecycle.py
+++ b/gateway/session_lifecycle.py
@@ -280,8 +280,7 @@ class SessionLifecycleMixin:
started_at = entry.active_turn_started_at
try:
marker_is_stale = (
- started_at is None
- or (max_age_seconds > 0 and now - started_at > max_age)
+ started_at is None or (max_age_seconds > 0 and now - started_at > max_age)
)
except TypeError:
# Mixed aware/naive timestamps: clear rather than risk an
diff --git a/gateway/session_persistence.py b/gateway/session_persistence.py
index 220debd3554..ed2c30d667d 100644
--- a/gateway/session_persistence.py
+++ b/gateway/session_persistence.py
@@ -389,10 +389,7 @@ class SessionPersistenceMixin:
if entry.origin is not None:
try:
recovered_entry = self._recover_session_from_db(
- session_key=key,
- source=entry.origin,
- now=_now(),
- raise_on_lookup_error=True,
+ session_key=key, source=entry.origin, now=_now(), raise_on_lookup_error=True,
)
except Exception as exc:
# Indeterminate: keep the only routing handle.
diff --git a/gateway/session_recovery.py b/gateway/session_recovery.py
index a3c01229bd0..f39183a564c 100644
--- a/gateway/session_recovery.py
+++ b/gateway/session_recovery.py
@@ -68,10 +68,7 @@ class SessionRecoveryMixin:
return "default"
def _recovered_row_allowed_for_active_profile(
- self,
- *,
- requested_session_key: str,
- recovered: Dict[str, Any],
+ self, *, requested_session_key: str, recovered: Dict[str, Any],
) -> bool:
"""Prevent a gateway from reviving another profile's row.
@@ -146,12 +143,7 @@ class SessionRecoveryMixin:
return origin.get("scope_id", origin.get("guild_id")) == source.scope_id
def _create_entry_from_recovered_row(
- self,
- *,
- row: Dict[str, Any],
- session_key: str,
- source: SessionSource,
- now: datetime,
+ self, *, row: Dict[str, Any], session_key: str, source: SessionSource, now: datetime,
) -> SessionEntry:
from gateway.session import SessionEntry
@@ -301,8 +293,7 @@ class SessionRecoveryMixin:
if not self._recovered_row_matches_source_scope(recovered, source):
return None, False
if not self._recovered_row_allowed_for_active_profile(
- requested_session_key=session_key,
- recovered=recovered,
+ requested_session_key=session_key, recovered=recovered,
):
logger.warning(
"Gateway session DB recovery ignored %s for %s because "
diff --git a/gateway/session_transcript.py b/gateway/session_transcript.py
index fe97425c2e1..ff67af59c04 100644
--- a/gateway/session_transcript.py
+++ b/gateway/session_transcript.py
@@ -87,10 +87,7 @@ class SessionTranscriptMixin:
return True
def advance_compression_session(
- self,
- session_key: str,
- expected_session_id: str,
- target_session_id: str,
+ self, session_key: str, expected_session_id: str, target_session_id: str,
) -> Optional[SessionEntry]:
"""CAS-advance one route along an already-verified compression lineage.
@@ -279,9 +276,7 @@ class SessionTranscriptMixin:
self._append_transcript_message(session_id, msg)
except Exception as exc:
from hermes_state import (
- CompressionSessionClosedError,
- StateDbCorruptError,
- StateDbReplacedError,
+ CompressionSessionClosedError, StateDbCorruptError, StateDbReplacedError,
)
if isinstance(exc, (StateDbReplacedError, StateDbCorruptError)):
@@ -537,11 +532,7 @@ class SessionTranscriptMixin:
raise TranscriptReadError(session_id) from e
def rewind_session(
- self,
- session_id: str,
- n: int = 1,
- *,
- require_retryable_composite: bool = False,
+ self, session_id: str, n: int = 1, *, require_retryable_composite: bool = False,
) -> Optional[Dict[str, Any]]:
"""Back up ``n`` user turns via soft-delete (``active=0``), mirroring CLI ``/undo [N]``.
@@ -557,9 +548,7 @@ class SessionTranscriptMixin:
with self._get_transcript_drain_lock():
n = max(n, 1)
from agent.context_compressor import (
- retryable_user_text,
- split_user_originated_turn,
- user_originated_turn_view,
+ retryable_user_text, split_user_originated_turn, user_originated_turn_view,
)
try: