Skip to content

The agent that grows with you

fix(gateway): label a moved-aside Bot Chat by its source, check it cheaply

Tekniumcc856a06fd77
diff --git a/gateway/platforms/api_server_bot_chat.py b/gateway/platforms/api_server_bot_chat.py
index c64a0aacf4d..e13f003b62b 100644
--- a/gateway/platforms/api_server_bot_chat.py
+++ b/gateway/platforms/api_server_bot_chat.py
@@ -80,7 +80,7 @@ def _refusal(authority, tip, exc):
from gateway.session_bot import displaceable_bot_chat
reason = 'bot_chat_not_local'
if displaceable_bot_chat(authority, tip) is not None:
- hint = "it is a conversation the gateway cannot own; it is moved aside as 'Bot Chat (API <id>)' on the next delivery"
+ hint = "it is a conversation the gateway cannot own; it is moved aside as 'Bot Chat (<source> <id>)' on the next delivery"
else: # local history a peer may not adopt (a served remote login's Desktop row, another profile's): never moved
hint = 'it is local history owned by another login or profile; open or rename it from the client that owns it'
logger.warning('Peer DM into Bot Chat %s refused: %s (not_found: field=%s detail=%s)',
diff --git a/gateway/session_bot.py b/gateway/session_bot.py
index 5645cb02fc2..f7022d58741 100644
--- a/gateway/session_bot.py
+++ b/gateway/session_bot.py
@@ -210,14 +210,18 @@ def displaceable_bot_chat(authority, row):
local chat and old local history (CLI, TUI, Desktop rows a native open adopts) are never moved."""
from hermes_state_local import POLICY_PREFIX
from hermes_state_local_migration import LEGACY_LOCAL_SOURCES, LEGACY_PREFIX
+
+ def owned(candidate):
+ return (str(candidate.get('chat_id') or '').startswith('local-')
+ or candidate.get('source') in LEGACY_LOCAL_SOURCES)
+ if owned(row): # every delivery asks: a bound local chat answers from its own row, with no read
+ return None
db = authority.db
tip = db.get_session(db.get_compression_tip(row['id']) or row['id'])
for candidate in (row, tip):
- if candidate is None:
+ if candidate is None or owned(candidate):
return None
chat_id = str(candidate.get('chat_id') or '')
- if chat_id.startswith('local-') or candidate.get('source') in LEGACY_LOCAL_SOURCES:
- return None
with db._read_ctx() as conn:
if conn.execute('SELECT 1 FROM state_meta WHERE key IN (?,?)',
(POLICY_PREFIX + (chat_id or candidate['id']),
@@ -228,8 +232,11 @@ def displaceable_bot_chat(authority, row):
def _displace_bot_chat(db, sid):
"""Free the canonical title from *sid*: it keeps its id and history under a visible name the
- user can find, rename or delete (``Bot Chat (API <id>)``). False when the title moved meanwhile."""
- name = f'Bot Chat (API {sid})'
+ user can find, rename or delete: ``Bot Chat (API <id>)`` for an API conversation, else named by
+ its platform (``Bot Chat (telegram <id>)``). False when the title moved meanwhile."""
+ source = str((db.get_session(sid) or {}).get('source') or '')
+ label = 'API' if source in ('', 'api_server', 'bot_peer_dm') else source
+ name = f'Bot Chat ({label} {sid})'
if len(name) > db.MAX_TITLE_LENGTH:
name = name[:db.MAX_TITLE_LENGTH - 2] + '…)'
diff --git a/tests/gateway/test_peer_dm_bot_chat_headless.py b/tests/gateway/test_peer_dm_bot_chat_headless.py
index 8d38f38b77e..1db39ad44f6 100644
--- a/tests/gateway/test_peer_dm_bot_chat_headless.py
+++ b/tests/gateway/test_peer_dm_bot_chat_headless.py
@@ -442,3 +442,31 @@ async def test_a_protected_desktop_bot_chat_refusal_never_promises_a_move(headle
assert resp.status == 502 and body['error']['code'] == 'bot_chat_not_local', body
assert 'moved aside' not in body['error']['message'], body
assert db.get_session_by_title('Bot Chat')['id'] == 'desktop_alice' and len(_sessions(headless.authority)) == 1
+
+
+async def test_a_messaging_bot_chat_moved_aside_is_labelled_by_its_platform(headless):
+ # A messaging row that took the canonical title is moved aside too, but it is no API conversation:
+ # its new name says where it came from.
+ from pathlib import Path
+ from gateway.session_authorities import served_profile_name
+ from gateway.session_bot import deliver
+ db = headless.authority.db
+ db.create_session('tg_row', 'telegram')
+ db.set_session_title('tg_row', 'Bot Chat')
+ db._execute_write(lambda conn: conn.execute("UPDATE sessions SET chat_id='12345' WHERE id='tg_row'"))
+ profile = served_profile_name(Path(db.db_path).parent.resolve())
+ record = await deliver(_local_owner(headless.authority),
+ dict(id='c' * 32, profile=profile, message='relayed', author=AUTHOR))
+ assert record['session_id'] != 'tg_row'
+ assert db.get_session('tg_row')['title'] == 'Bot Chat (telegram tg_row)'
+
+
+def test_a_local_bot_chat_is_never_displaceable_without_a_lineage_read(headless, monkeypatch):
+ # Every delivery asks; a bound local chat answers from its own row, before any tip or session read.
+ from gateway.session_bot import displaceable_bot_chat
+ db = headless.authority.db
+ reads = []
+ monkeypatch.setattr(db, 'get_compression_tip', lambda sid: reads.append(sid) or sid)
+ assert displaceable_bot_chat(headless.authority, {'id': 'x', 'chat_id': 'local-abc', 'source': 'gui'}) is None
+ assert reads == []