Skip to content

The agent that grows with you

fix: keep original entry object on same-id recovery — preserve live state (review follow-up)

kshitijk4poor726f0ce1b519
diff --git a/gateway/session.py b/gateway/session.py
index 518b953c8c6..d7770188613 100644
--- a/gateway/session.py
+++ b/gateway/session.py
@@ -1618,24 +1618,22 @@ 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).
+ # A non-None recovery with the SAME session id is a
+ # successful resume (all recovery gates passed, row
+ # reopened): keep the routing entry — it is proven valid,
+ # not a dead route (#95957). Keep the ORIGINAL entry
+ # object, not the recovered one: the recovered entry is
+ # rebuilt minimal from the DB row and would silently drop
+ # live state the existing entry carries (token/cost
+ # counters, model_override, resume_pending/queued-work
+ # markers, metadata). Nothing in sessions.json changes,
+ # so no save is needed for this branch.
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"],
+ logger.info(
+ "gateway.session: reopened ended session %s for "
+ "sessions.json entry %r (end_reason=%r); keeping route",
+ entry.session_id, key, row["end_reason"],
)
- self._entries[key] = recovered_entry
- recovered_keys += 1
continue
logger.warning(
diff --git a/tests/gateway/test_session_store_stale_prune.py b/tests/gateway/test_session_store_stale_prune.py
index cd7dde82427..41167ab9f6a 100644
--- a/tests/gateway/test_session_store_stale_prune.py
+++ b/tests/gateway/test_session_store_stale_prune.py
@@ -128,7 +128,10 @@ class TestPruneStaleSessionsLocked:
).timestamp(),
}
store = _make_store_with_db(tmp_path, db) # default mode="none"
- store._entries[key] = _make_entry_with_origin(key, "sid_parent")
+ original_entry = _make_entry_with_origin(key, "sid_parent")
+ original_entry.model_override = {"model": "custom/model", "provider": "openrouter"}
+ original_entry.resume_pending = True
+ store._entries[key] = original_entry
with patch.object(store, "_save") as mock_save:
store._prune_stale_sessions_locked()
@@ -136,8 +139,17 @@ class TestPruneStaleSessionsLocked:
# 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()
-
+ # The ORIGINAL entry object is kept — a rebuilt entry would silently
+ # drop live state (model_override, resume_pending, token counters).
+ assert store._entries[key] is original_entry
+ assert store._entries[key].model_override == {
+ "model": "custom/model", "provider": "openrouter"
+ }
+ assert store._entries[key].resume_pending is True
+ # The row is reopened in state.db by recovery.
+ db.reopen_session.assert_called_once_with("sid_parent")
+ # Nothing in sessions.json changed, so no rewrite is needed.
+ mock_save.assert_not_called()
def test_noop_when_db_is_none(self, tmp_path):
config = GatewayConfig(default_reset_policy=SessionResetPolicy(mode="none"))