Skip to content

The agent that grows with you

fix(gateway): keep sessions.json entry when startup recovery succeeds with same session id

Jackal991ff3f25e0417d
diff --git a/gateway/session.py b/gateway/session.py
index fb7526f3c07..518b953c8c6 100644
--- a/gateway/session.py
+++ b/gateway/session.py
@@ -1618,6 +1618,26 @@ class SessionStore:
recovered_keys += 1
continue
+ # A non-None recovery with the SAME session id is a successful
+ # resume: _recover_session_from_db only returns non-None after
+ # passing all safety gates (recoverable end reason, reset
+ # boundary NOT EXISTS, source-scope match, active-profile
+ # check, _should_reset). Keep the routing entry in place — it
+ # is still valid, not a dead route. The `!=` guard above is
+ # only for the compression-rotation case where a newer live
+ # child session exists; a same-id recovery must NOT fall
+ # through to the prune branch (#95957).
+ if recovered_entry is not None:
+ logger.warning(
+ "gateway.session: keeping stale sessions.json entry "
+ "%r -> %s (end_reason=%r); recovery succeeded with "
+ "same session id",
+ key, entry.session_id, row["end_reason"],
+ )
+ self._entries[key] = recovered_entry
+ recovered_keys += 1
+ continue
+
logger.warning(
"gateway.session: pruning stale sessions.json entry "
"%r -> %s (end_reason=%r); left by a crashed gateway",
diff --git a/tests/gateway/test_session_store_stale_prune.py b/tests/gateway/test_session_store_stale_prune.py
index 9d5f6cb57ef..cd7dde82427 100644
--- a/tests/gateway/test_session_store_stale_prune.py
+++ b/tests/gateway/test_session_store_stale_prune.py
@@ -104,6 +104,41 @@ class TestPruneStaleSessionsLocked:
assert key in store._entries
assert store._entries[key].session_id == "sid_parent"
+ def test_keeps_stale_entry_when_recovery_returns_same_session_id(self, tmp_path):
+ """A successful same-id recovery must NOT prune the routing entry.
+
+ When the startup sweep finds a stale entry whose session has ended in
+ state.db but ``_recover_session_from_db`` succeeds and returns the SAME
+ session id (proving the route is still resumable — the ``!=`` repoint
+ guard only exists for the compression-rotation child case), the entry
+ must be kept in place. The old code fell through to the prune branch
+ whenever the recovered id did not differ, deleting a perfectly valid
+ resumable mapping (#95957).
+ """
+ key = "agent:main:telegram:dm:5140768830"
+ db = _db_returning(
+ {"sid_parent": {"end_reason": "agent_close", "id": "sid_parent"}}
+ )
+ # Recovery returns the row for sid_parent itself — same id as the entry.
+ db.find_latest_gateway_session_for_peer.return_value = {
+ "id": "sid_parent",
+ "started_at": (datetime.now() - timedelta(hours=5)).timestamp(),
+ "last_activity_at": (
+ datetime.now() - timedelta(hours=4)
+ ).timestamp(),
+ }
+ store = _make_store_with_db(tmp_path, db) # default mode="none"
+ store._entries[key] = _make_entry_with_origin(key, "sid_parent")
+
+ with patch.object(store, "_save") as mock_save:
+ store._prune_stale_sessions_locked()
+
+ # The successfully-recovered route must survive the sweep.
+ assert key in store._entries
+ assert store._entries[key].session_id == "sid_parent"
+ mock_save.assert_called_once()
+
+
def test_noop_when_db_is_none(self, tmp_path):
config = GatewayConfig(default_reset_policy=SessionResetPolicy(mode="none"))
with patch("gateway.session.SessionStore._ensure_loaded"):