Files
hermes-agent/tests/gateway/test_api_server_bot_chat_title_fallback.py
Teknium 4d8af75cb1 fix(api): peer-dm title fallback honours archived and source filters
The reset-Bot-Chat fallback added in 2cccb91d11 for
`GET /api/sessions?title=…` returned the titled root even when the user had
archived it on purpose, and it ignored a `source=` filter. That brought an
archived Bot Chat back into `hermes peer dm` routing and broke the listing's
`source` contract. The primary lookup returns `[]` for both.

The fallback now applies the listing's own filters:
- it never returns an archived row (the reaper-archive resurrection below it
  keeps handling recoverable archives);
- it returns a hidden row only with `include_hidden`;
- it requires a matching `source` when one is given.

Red -> green: tests/gateway/test_api_server_bot_chat_title_fallback.py (the
archived and `source=telegram` cases returned the row on a40e0c7b15).

Review finding (package 10 independent review, round 2) on #140968
Refs #134889 (LC-23)
Refs #140719
2026-10-18 09:58:15 -07:00

40 lines
1.6 KiB
Python

"""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
@pytest.fixture(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) == []