Skip to content

The agent that grows with you

fix(api): peer-dm title fallback honours archived and source filters

Teknium4d8af75cb1a3
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.py
index fa8f488e789..042314b2645 100644
--- a/gateway/platforms/api_server.py
+++ b/gateway/platforms/api_server.py
@@ -3037,7 +3037,7 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):
rows = [s for s in rows if (s.get("title") or "").strip() == title_filter]
return rows
- sessions = await _list() or (await reset_local_title_owner(db, title_filter, include_hidden))
+ sessions = await _list() or (await reset_local_title_owner(db, title_filter, include_hidden, source))
if title_filter and not sessions:
# A canonical Bot Chat auto-archived by the orphan reaper would make `hermes peer dm`
# mint transient sessions: resurrect and re-list; deliberate archives stay put.
diff --git a/gateway/platforms/api_server_bot_chat.py b/gateway/platforms/api_server_bot_chat.py
index 7a39165a268..04cffbc0c00 100644
--- a/gateway/platforms/api_server_bot_chat.py
+++ b/gateway/platforms/api_server_bot_chat.py
@@ -84,17 +84,21 @@ def _refusal(authority, tip, exc):
return reason, f'Bot Chat admission refused: {reason}'
-async def reset_local_title_owner(db, title, include_hidden):
+async def reset_local_title_owner(db, title, include_hidden, source=None):
"""``[row]`` for the exact-title lookup of a LOCAL conversation that was reset (``/new``, the
compression-exhaustion auto-reset), else ``[]``. The listing shows a reset lineage as its latest,
untitled segment, so ``hermes peer dm`` stopped finding its Bot Chat and its create collided
- with the title. The titled creation id still owns the FIFO and is what the chat door admits."""
- return await asyncio.to_thread(_reset_local_title_owner, db, title, include_hidden) if title else []
+ with the title. The titled creation id still owns the FIFO and is what the chat door admits.
+ It applies the listing's own filters: hidden only with ``include_hidden``, never an archived
+ row (a deliberate archive stays put; the reaper-archive resurrection is the caller's), and
+ ``source`` when one is given."""
+ return await asyncio.to_thread(_reset_local_title_owner, db, title, include_hidden, source) if title else []
-def _reset_local_title_owner(db, title, include_hidden):
+def _reset_local_title_owner(db, title, include_hidden, source=None):
row = db.get_session_by_title(title)
- if row is None or row.get('end_reason') != 'session_reset' or (row.get('hidden') and not include_hidden):
+ if (row is None or row.get('end_reason') != 'session_reset' or row.get('archived')
+ or (row.get('hidden') and not include_hidden) or (source and row.get('source') != source)):
return []
from hermes_state_local import local_receipt
try:
diff --git a/tests/gateway/test_api_server_bot_chat_title_fallback.py b/tests/gateway/test_api_server_bot_chat_title_fallback.py
new file mode 100644
index 00000000000..7bbd518ffd6
--- /dev/null
+++ b/tests/gateway/test_api_server_bot_chat_title_fallback.py
@@ -0,0 +1,39 @@
+"""The peer-dm title fallback for a reset LOCAL Bot Chat honours the listing's own filters.
+
+``GET /api/sessions?title=Bot+Chat`` lists a reset lineage as its latest, untitled segment, so a
+fallback returns the titled root. It must not resurrect a deliberately archived Bot Chat into
+``hermes peer dm`` routing, nor answer a ``source=`` query the row does not match."""
+import pytest
+
+from gateway.platforms import api_server_bot_chat as bot_chat
+
+
+class FakeDB:
+ def __init__(self, **row):
+ self.row = dict({'id': 'local-root', 'title': 'Bot Chat', 'end_reason': 'session_reset',
+ 'hidden': 0, 'archived': 0, 'source': 'local'}, **row)
+
+ def get_session_by_title(self, title):
+ return dict(self.row) if title == self.row['title'] else None
+
+
[email protected](autouse=True)
+def reset_lineage(monkeypatch):
+ monkeypatch.setattr('hermes_state_local.local_receipt', lambda db, sid: {'lineage': [sid, 'fresh']})
+
+
+def test_the_reset_bot_chat_is_found():
+ assert [r['id'] for r in bot_chat._reset_local_title_owner(FakeDB(), 'Bot Chat', True)] == ['local-root']
+ assert bot_chat._reset_local_title_owner(FakeDB(), 'Bot Chat', True, source='local') != []
+
+
+def test_a_deliberately_archived_bot_chat_is_not_returned():
+ assert bot_chat._reset_local_title_owner(FakeDB(archived=1), 'Bot Chat', True) == []
+
+
+def test_a_source_filter_the_row_does_not_match_excludes_it():
+ assert bot_chat._reset_local_title_owner(FakeDB(), 'Bot Chat', True, source='telegram') == []
+
+
+def test_hidden_needs_include_hidden():
+ assert bot_chat._reset_local_title_owner(FakeDB(hidden=1), 'Bot Chat', False) == []