The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(discord): fall back to the session row whenever the routing entry lacks the markers
kshitijk4poorcc1e4e904c3a
diff --git a/gateway/run_topics.py b/gateway/run_topics.pyindex 32f45138ca3..ffdd2a7331f 100644--- a/gateway/run_topics.py+++ b/gateway/run_topics.py@@ -295,17 +295,25 @@ class GatewayTopicThreadsMixin:entry = self.session_store.lookup_by_session_key(session_key)if entry is None:return source++ def opens_this_thread(origin: Optional[SessionSource]) -> bool:+ return (+ origin is not None and str(origin.thread_id) == str(source.thread_id)+ and self._is_discord_auto_thread_lane(origin)+ )+origin = entry.origin- if origin is not None and source.message_id and origin.message_id == source.message_id:- # An entry rebuilt from this very event (lost routing index) holds the unmarked live- # source; the session row keeps the opening origin until this turn's peer refresh.- origin = None- session_db = self._sync_session_db()- with suppress(Exception):- row = session_db.get_session(entry.session_id) if session_db is not None else None- origin = SessionSource.from_dict(json.loads(row["origin_json"]))- if origin is None or origin.thread_id != source.thread_id or not self._is_discord_auto_thread_lane(origin):- return source+ if not opens_this_thread(origin):+ # An entry rebuilt after a lost routing index holds an unmarked event, while the session+ # row keeps the opening origin until that entry's first peer refresh overwrites it.+ db = self.session_store._db_for_key(session_key)+ row = db.get_session(entry.session_id) if db is not None else None+ raw, origin = (row or {}).get("origin_json"), None+ if raw:+ with suppress(ValueError, KeyError, TypeError): # malformed/legacy row: no recovery+ origin = SessionSource.from_dict(json.loads(raw))+ if not opens_this_thread(origin):+ return sourcereturn replace_source(source, auto_thread_created=True, auto_thread_initial_name=origin.auto_thread_initial_name,)diff --git a/tests/gateway/test_session_title_rename_lane.py b/tests/gateway/test_session_title_rename_lane.pyindex 824111576f9..06ec5c40b20 100644--- a/tests/gateway/test_session_title_rename_lane.py+++ b/tests/gateway/test_session_title_rename_lane.py@@ -71,21 +71,23 @@ _OPENING = _thread_source()-def _attach_in_thread(current, entry_origin, row_origin=None):+def _attach_in_thread(current, entry_origin, row_origin=None, get_session=None):"""Attach the title callback for an unmarked in-thread follow-up *current* whose routing entryholds *entry_origin* (and whose session row holds *row_origin*), through the REAL lanepredicate and recovery. Returns (agent, scheduled renames)."""scheduled: list = []entry = types.SimpleNamespace(session_id="sess-1", origin=entry_origin)row = {"origin_json": json.dumps(row_origin.to_dict())} if row_origin else None+ db = types.SimpleNamespace(get_session=get_session or {"sess-1": row}.get)runner = types.SimpleNamespace(- session_store=types.SimpleNamespace(lookup_by_session_key={"key-1": entry}.get),- _session_db=types.SimpleNamespace(get_session={"sess-1": row}.get),+ session_store=types.SimpleNamespace(+ lookup_by_session_key={"key-1": entry}.get, _db_for_key={"key-1": db}.get,+ ),_is_telegram_topic_lane=lambda src: False,_is_relay_discord_channel_lane=lambda src: False,_schedule_discord_semantic_thread_rename=lambda src, sid, title: scheduled.append((src, sid, title)),)- for name in ("_sync_session_db", "_is_discord_auto_thread_lane", "_recover_discord_auto_thread_source"):+ for name in ("_is_discord_auto_thread_lane", "_recover_discord_auto_thread_source"):setattr(runner, name, types.MethodType(getattr(GatewayRunner, name), runner))holder = types.SimpleNamespace(_runner=runner,@@ -105,8 +107,8 @@ def test_discord_title_retry_recovers_auto_thread_origin(rebuilt_entry):adapter = type("Adapter", (), {})()current = _thread_source(message_id="follow-up", profile="runtime-profile")current._transport_adapter_ref = weakref.ref(adapter)- if rebuilt_entry: # routing index lost: the entry was rebuilt from this very event- agent, scheduled = _attach_in_thread(current, entry_origin=current, row_origin=_OPENING)+ if rebuilt_entry: # routing index lost: the entry was rebuilt from an unmarked event+ agent, scheduled = _attach_in_thread(current, entry_origin=_thread_source(), row_origin=_OPENING)else:agent, scheduled = _attach_in_thread(current, entry_origin=_OPENING)agent._on_session_title("Recovered semantic title", "llm")@@ -118,15 +120,23 @@ def test_discord_title_retry_recovers_auto_thread_origin(rebuilt_entry):assert (session_id, title) == ("sess-1", "Recovered semantic title")[email protected]("origin", [+def _unreadable(session_id):+ raise RuntimeError("state.db unavailable")++[email protected]("entry_origin,row_origin,get_session", [pytest.param(_thread_source("thread-2", auto_thread_created=True, auto_thread_initial_name="Other"),- id="other-thread",+ _thread_source("thread-2", auto_thread_created=True, auto_thread_initial_name="Other"),+ None, id="other-thread",),- pytest.param(_thread_source(message_id="opening-message"), id="user-created-thread"),+ pytest.param(_thread_source(message_id="opening-message"), None, None, id="user-created-thread"),+ pytest.param(_thread_source(), None, _unreadable, id="unreadable-row"),])-def test_discord_title_retry_never_borrows_markers_from_another_origin(origin):- agent, scheduled = _attach_in_thread(_thread_source(message_id="follow-up"), entry_origin=origin)+def test_discord_title_retry_never_borrows_markers_from_another_origin(entry_origin, row_origin, get_session):+ agent, scheduled = _attach_in_thread(+ _thread_source(message_id="follow-up"), entry_origin, row_origin, get_session,+ )assert not hasattr(agent, "_on_session_title")assert scheduled == []