Skip to content

The agent that grows with you

fix(tui): never auto-resume a source='unknown' guard placeholder

Hermes Agentc953b2a03ef5
diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.py
index 8ba57b1cd2c..265d4497a9d 100644
--- a/tests/tui_gateway/test_tui_gateway_server.py
+++ b/tests/tui_gateway/test_tui_gateway_server.py
@@ -17121,6 +17121,41 @@ def test_session_most_recent_returns_null_when_only_tool_rows(monkeypatch):
assert resp["result"]["session_id"] is None
+def test_session_most_recent_skips_unknown_source_rows(monkeypatch):
+ """#54320: a token-accounting guard placeholder (source='unknown') must never be
+ picked for auto-resume — the row can outrank the session the user actually opened."""
+
+ class _DB:
+ def list_sessions_rich(self, *, source=None, limit=200, order_by_last_active=False, compact_rows=False):
+ return [
+ {"id": "guard-1", "source": "unknown", "title": "", "started_at": 101},
+ {"id": "tui-1", "source": "tui", "title": "real", "started_at": 100},
+ ]
+
+ monkeypatch.setattr(server, "_get_db", lambda: _DB())
+
+ resp = server.handle_request(
+ {"id": "1", "method": "session.most_recent", "params": {}}
+ )
+
+ assert resp["result"]["session_id"] == "tui-1"
+ assert resp["result"]["source"] == "tui"
+
+
+def test_session_most_recent_returns_null_when_only_unknown_rows(monkeypatch):
+ class _DB:
+ def list_sessions_rich(self, *, source=None, limit=200, order_by_last_active=False, compact_rows=False):
+ return [{"id": "guard-1", "source": "unknown", "started_at": 1}]
+
+ monkeypatch.setattr(server, "_get_db", lambda: _DB())
+
+ resp = server.handle_request(
+ {"id": "1", "method": "session.most_recent", "params": {}}
+ )
+
+ assert resp["result"]["session_id"] is None
+
+
def test_session_most_recent_folds_db_exception_into_null_result(monkeypatch):
"""Per contract, errors are folded into the null-result shape so
callers don't have to special-case JSON-RPC error envelopes for
diff --git a/tui_gateway/methods_session.py b/tui_gateway/methods_session.py
index 10f92e3454d..515d89c86aa 100644
--- a/tui_gateway/methods_session.py
+++ b/tui_gateway/methods_session.py
@@ -134,6 +134,16 @@ def _denied_source(row: dict) -> bool:
return (row.get("source") or "").strip().lower() in _LISTING_DENY_SOURCES
+def _auto_resume_denied_source(row: dict) -> bool:
+ """``_denied_source`` plus ``source='unknown'``: auto-resume must never land on a
+ token-accounting guard placeholder (#54320). The guard mints those rows when legacy
+ message rows lack a ``sessions`` row, and such a placeholder can outrank the session
+ the user actually opened. Human-facing listings keep showing them (they may be a
+ real session awaiting repair); only the pick-a-session-for-me paths skip them."""
+ source = (row.get("source") or "").strip().lower()
+ return source in _LISTING_DENY_SOURCES or source == "unknown"
+
+
def _listing_rows(db, limit: int, **kwargs) -> list:
"""Human-facing ``list_sessions_rich`` rows (most recent first), deny-list applied."""
rows = db.list_sessions_rich(source=None, limit=limit, order_by_last_active=True, compact_rows=True, **kwargs)
@@ -503,11 +513,15 @@ def _(rid, params: dict, db) -> dict:
@method("session.most_recent")
def _(rid, params: dict) -> dict:
- """Most recent human-facing session (session.list deny-list); errors fold into ``session_id: null``."""
+ """Most recent human-facing session, skipping auto-resume-denied rows (deny-list
+ plus ``source='unknown'`` guard placeholders, #54320); errors fold into ``session_id: null``."""
with _profile_db(params) as db:
try:
- # Generous over-fetch: many ``tool`` rows must not yield a false "none".
- for row in _listing_rows(db, 200)[:1] if db is not None else ():
+ # Generous over-fetch: many denied rows must not yield a false "none".
+ rows = ([row for row in _listing_rows(db, 200)
+ if not _auto_resume_denied_source(row)]
+ if db is not None else [])
+ for row in rows[:1]:
return _ok(rid, {"session_id": row.get("id"), "title": row.get("title") or "",
"started_at": row.get("started_at") or 0, "source": row.get("source") or ""})
except Exception: