Skip to content

The agent that grows with you

fix(gateway): promise a move-aside only when the Bot Chat will be moved

Teknium7456e94789e8
diff --git a/gateway/platforms/api_server_bot_chat.py b/gateway/platforms/api_server_bot_chat.py
index c775b93dd14..c64a0aacf4d 100644
--- a/gateway/platforms/api_server_bot_chat.py
+++ b/gateway/platforms/api_server_bot_chat.py
@@ -77,8 +77,12 @@ def _refusal(authority, tip, exc):
validate_legacy_row(authority.db, tip)
reason, hint = 'bot_chat_unbound', 'open it once with `hermes chat -c "Bot Chat"` on that host'
except RuntimeStoreError:
- reason, hint = 'bot_chat_not_local', ("it is a conversation the gateway cannot own; it is moved aside "
- "as 'Bot Chat (API <id>)' on the next delivery")
+ 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"
+ 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)',
tip['id'], reason, exc.field, exc.detail)
return reason, f"Bot Chat admission refused: {reason} (this profile's 'Bot Chat' is not a local chat; {hint})"
diff --git a/tests/gateway/test_peer_dm_bot_chat_headless.py b/tests/gateway/test_peer_dm_bot_chat_headless.py
index 7871696abfe..8d38f38b77e 100644
--- a/tests/gateway/test_peer_dm_bot_chat_headless.py
+++ b/tests/gateway/test_peer_dm_bot_chat_headless.py
@@ -425,3 +425,20 @@ async def test_a_turn_after_the_move_aside_names_the_local_bot_chat(headless, do
new = db.get_session_by_title('Bot Chat')['id']
assert new != old and named == {new}, (named, old, new)
assert [sid for sid, _, _ in headless.executed] == [new]
+
+
+async def test_a_protected_desktop_bot_chat_refusal_never_promises_a_move(headless):
+ # A Desktop row a served remote login stamped is that login's local history: it is never moved
+ # aside (``displaceable_bot_chat``), so the refusal must not say it will be.
+ db = headless.authority.db
+ db.create_session('desktop_alice', 'desktop')
+ db.set_session_title('desktop_alice', 'Bot Chat')
+ db._execute_write(lambda conn: conn.execute(
+ "UPDATE sessions SET user_id='github:alice' WHERE id='desktop_alice'"))
+ for _attempt in range(2): # "on the next delivery": the second is refused the same way
+ resp = await headless.cli.post("/api/sessions/desktop_alice/chat", json={"message": "ping", "author": AUTHOR})
+ body = await resp.json()
+ 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