Skip to content

The agent that grows with you

fix(gateway): an unbound login-stamped Bot Chat holder is displaced, not refused

Julien Talbota442f5df85b7
diff --git a/gateway/session_bot.py b/gateway/session_bot.py
index a3488e007e7..f92dc484f70 100644
--- a/gateway/session_bot.py
+++ b/gateway/session_bot.py
@@ -218,7 +218,7 @@ def displaceable_bot_chat(authority, row):
unbound CLI/TUI/Desktop row no native open can adopt), else None. Such a row can never be
adopted (``validate_legacy_row``), renamed (the canonical title is its identity) or renamed
over the API, so it would refuse every delivery for good. A bound local chat, old local history
- a native open adopts and a local row a login stamped as its own are never moved."""
+ a native open adopts and a Desktop row a login stamped as its own are never moved."""
from hermes_state_local import POLICY_PREFIX
from hermes_state_local_migration import LEGACY_PREFIX
@@ -282,7 +282,11 @@ def _kept_local_history(db, row):
if row.get('source') not in LEGACY_LOCAL_SOURCES:
return False
if row.get('user_id') and not legacy_account_label(row):
- return True
+ # A Desktop row a served login stamped is that login's own chat, adopted in place (#140183).
+ # A stamp on any other local source names no owner this door can reach — it is a submit-only
+ # principal, a foreign stamp is refused, and a bound row is refused by its route — so a row
+ # with no binding at all is unadoptable for everyone and held the title for good (#143715).
+ return row.get('source') == 'desktop' or bool(row.get('chat_id') or row.get('session_key'))
try:
validate_legacy_row(db, row)
except RuntimeStoreError:
diff --git a/tests/gateway/test_bot_chat_unadoptable_local_title.py b/tests/gateway/test_bot_chat_unadoptable_local_title.py
index 81884d1a503..0378d0e479c 100644
--- a/tests/gateway/test_bot_chat_unadoptable_local_title.py
+++ b/tests/gateway/test_bot_chat_unadoptable_local_title.py
@@ -19,8 +19,8 @@ headless = _headless_gateway.headless
_local_owner = _headless_gateway._local_owner
-def _squatter(db, sid, *, column, value):
- db.create_session(sid, 'tui')
+def _squatter(db, sid, *, column, value, source='tui'):
+ db.create_session(sid, source)
db.set_session_title(sid, 'Bot Chat')
if column:
db._execute_write(lambda conn: conn.execute(f'UPDATE sessions SET {column}=? WHERE id=?', (value, sid)))
@@ -61,18 +61,51 @@ async def test_an_unadoptable_local_bot_chat_is_moved_aside_and_the_delivery_lan
assert len(db._read_all('SELECT id FROM sessions')) == 2
[email protected]('column, value', [(None, None), ('user_id', 'github:alice')])
[email protected]('stamp', ['github:alice', 'nous:[email protected]'])
@pytest.mark.asyncio
-async def test_kept_local_bot_chat_history_is_never_moved_aside(headless, column, value):
- # A plain pre-upgrade TUI Bot Chat is adopted in place; a row a login stamped as its own stays
- # that login's (refused, never renamed). Neither loses its title.
+async def test_an_unbound_stamped_bot_chat_is_moved_aside_and_the_delivery_lands(headless, stamp):
+ # A ``user_id`` on a row with no binding at all names nobody the delivery can reach: the door's
+ # principal is submit-only (no native transport) and refuses a foreign stamp, so the row held
+ # the title and refused every delivery for good (#143715). Moving it loses nothing.
+ from tests.waiting import async_wait_until
+ db = headless.authority.db
+ old = _squatter(db, '20260924_173840_909979', column='user_id', value=stamp)
+ db.append_message(old, 'user', 'earlier')
+ first = await _deliver(headless.authority, 'd')
+ new = first['session_id']
+ assert new != old and str(db.get_session(new)['chat_id']).startswith('local-'), first
+ assert db.get_session_by_title('Bot Chat')['id'] == new
+ assert db.get_session(old)['title'] == f'Bot Chat (tui {old})' and db.get_session(old)['user_id'] == stamp
+ second = await _deliver(headless.authority, 'e')
+ assert second['session_id'] == new
+ await async_wait_until(lambda: len(headless.executed) == 2, timeout=10, interval=.05)
+ assert len(db._read_all('SELECT id FROM sessions')) == 2
+
+
[email protected]('source, bound', [
+ ('tui', True), # a binding names a route the row answers on
+ ('desktop', False), # a served login's own Desktop row (#140183)
+])
+async def test_a_stamped_bot_chat_that_names_its_owner_is_never_moved_aside(headless, source, bound):
+ db = headless.authority.db
+ old = _squatter(db, 'tui_bot_chat', column='user_id', value='github:alice', source=source)
+ if bound:
+ db._execute_write(lambda conn: conn.execute(
+ "UPDATE sessions SET session_key='agent:main:tui:dm:bound' WHERE id=?", (old,)))
+ db.append_message(old, 'user', 'earlier')
+ with pytest.raises(RuntimeStoreError):
+ await _deliver(headless.authority, 'f')
+ assert db.get_session_by_title('Bot Chat')['id'] == old, 'an owned row keeps its title'
+ assert len(db._read_all('SELECT id FROM sessions')) == 1
+
+
+async def test_kept_local_bot_chat_history_is_never_moved_aside(headless):
+ # A plain pre-upgrade TUI Bot Chat is adopted in place, never renamed.
db = headless.authority.db
- old = _squatter(db, 'tui_bot_chat', column=column, value=value)
+ old = _squatter(db, 'tui_bot_chat', column=None, value=None)
db.append_message(old, 'user', 'earlier')
- try:
- record = await _deliver(headless.authority, 'c')
- except RuntimeStoreError as exc:
- assert column == 'user_id' and exc.reason == 'not_found'
- else:
- assert column is None and record['session_id'] == old
+ record = await _deliver(headless.authority, 'c')
+ assert record['session_id'] == old
assert db.get_session_by_title('Bot Chat')['id'] == old and len(db._read_all('SELECT id FROM sessions')) == 1