Skip to content

The agent that grows with you

fix(gateway): a moved local conversation goes read-only whenever its lane is taken over

Teknium8b13bb23663c
diff --git a/gateway/session_authority.py b/gateway/session_authority.py
index 5113dcf8d80..be501c7bb42 100644
--- a/gateway/session_authority.py
+++ b/gateway/session_authority.py
@@ -80,9 +80,10 @@ class LiveSession:
# Mounted by that cold path (not published by its lane): the lane publishing ANOTHER conversation
# on the same route later supersedes it too.
history_mount: bool = False
- # A local conversation a messaging lane took over (``/handoff``): its viewers follow it onto the
- # lane, and once that lane publishes another conversation (``/new``) it is superseded like a
- # history mount, whatever the lane published since (#143286).
+ # A local conversation a messaging lane took over (``/handoff``, a platform ``/resume``): its
+ # viewers follow it onto the lane, and once that lane publishes another conversation (``/new``,
+ # another handoff) it is superseded like a history mount, whatever the lane published since.
+ # Set from the row (``moved_local``) on every mount of it, however constructed (#143286).
handed_off: bool = False
# The background-review guard of a cold ``/refine`` (gateway/session_refine.py): its side agent is
# per call, so the run token lives here; a second /refine joins it and the next turn cancels it.
@@ -278,7 +279,8 @@ class SessionAuthority:
# *sid* takes the route over: new here, moved onto it, or back after another conversation had it.
took_over = live is None or live.route != route or live.superseded
if live is None:
- live = self.sessions[sid] = LiveSession(source, route)
+ from gateway.session_route_recovery import moved_local
+ live = self.sessions[sid] = LiveSession(source, route, handed_off=moved_local(self, sid, source))
elif (live.source is not None and live.source.platform == Platform.LOCAL
and source.platform != Platform.LOCAL):
# A local conversation a messaging lane took over (``/handoff``): its repoint retired the
diff --git a/gateway/session_route_recovery.py b/gateway/session_route_recovery.py
index 7e99cb06688..8452378fc80 100644
--- a/gateway/session_route_recovery.py
+++ b/gateway/session_route_recovery.py
@@ -121,10 +121,22 @@ def restore_routed_session(authority, sid):
# Re-pin the persisted receiving bot so delivery after a restart goes through it or nowhere,
# never the default bot by heuristic (gateway/AGENTS.md § Identity survives the process).
source = authority.runner._restored_source(entry)
- authority.sessions.setdefault(sid, LiveSession(source, route, superseded=superseded, history_mount=True))
+ authority.sessions.setdefault(sid, LiveSession(source, route, superseded=superseded, history_mount=True,
+ handed_off=moved_local(authority, sid, source)))
return SessionRef(authority.profile_id, sid)
+def moved_local(authority, sid, source):
+ """*sid* is a local conversation (created by a local surface) published on a messaging lane:
+ ``/handoff`` or a platform ``/resume`` moved it there. That provenance is the row's, not the
+ path that mounts it (the Desktop that followed it, the lane's inbound, a cold open), so every
+ mount of it goes read-only once the lane takes up another conversation (#143286)."""
+ from hermes_state_local_migration import LEGACY_LOCAL_SOURCES
+ if source is None or source.platform == Platform.LOCAL:
+ return False
+ return (authority.db.get_session(sid) or {}).get('created_source') in LEGACY_LOCAL_SOURCES
+
+
def mountable(authority, sid):
"""``restore_routed_session`` would mount *sid* (this profile's messaging lineage root)."""
return _mount_plan(authority, sid) is not None
diff --git a/tests/gateway/test_handoff_local_receipt.py b/tests/gateway/test_handoff_local_receipt.py
index 4204bd83868..97c54f59458 100644
--- a/tests/gateway/test_handoff_local_receipt.py
+++ b/tests/gateway/test_handoff_local_receipt.py
@@ -385,6 +385,69 @@ async def test_a_platform_resume_of_a_local_conversation_continues_its_current_h
assert cold.sessions[head.session_id].route == route
[email protected]('opened', ['followed by the Desktop', 'a viewer after a restart', 'the lane after a restart'])
[email protected]('takeover', ['another chat handed off', "the platform's /new"])
[email protected]('door', ['/handoff', 'platform /resume'])
[email protected]('history', ['plain', 'reset'])
+async def test_a_moved_conversation_is_read_only_once_its_lane_is_taken_over(tmp_path, history, door, takeover,
+ opened):
+ """Bot Chat X (``reset``: a local ``/new`` moved it to X2) moves onto a Telegram lane, which
+ continues its head, writable there. Once that lane is taken over (another chat handed off to
+ it, or the platform's ``/new`` and its next inbound) the head is the lane's history, however it
+ was mounted: the Desktop that followed it, a viewer or the lane's inbound after a restart. Its
+ only route is the lane's, so a turn or a Stop through it would act on the new conversation:
+ read stays open, submit and control are refused, warm and after a restart."""
+ from gateway.platforms.event import MessageEvent
+ from gateway.session_authority import initialize_session_authority
+ from gateway.session_contract import Principal, SessionRef
+ from gateway.session_local import create_local_session
+ from hermes_state_local import local_receipt
+ from hermes_state_runtime import RuntimeStoreError
+ runner = _Runner(tmp_path)
+ warm = await initialize_session_authority(runner, profile_id='fixture', instance_id='warm')
+ owner = Principal('owner', 'fixture', frozenset({'session:create', 'session:read', 'session:control',
+ 'session:submit'}), 'socket')
+ ref = create_local_session(warm, owner, {'request_id': 'bot-chat', 'source': 'gui', 'cwd': str(tmp_path),
+ 'model': 'frozen', 'toolsets': []})
+ warm.db.set_session_title(ref.session_id, 'Bot Chat')
+ if history == 'reset':
+ runner.session_store.reset_session(warm.sessions[ref.session_id].route)
+ head = SessionRef('fixture', local_receipt(warm.db, ref.session_id)['entry']['session_id'])
+ telegram = _source()
+ if door == '/handoff':
+ route = await _handoff(runner, warm, ref.session_id, telegram)
+ else:
+ await _resume_gateway(runner, warm)._handle_resume_command(
+ MessageEvent(text='/resume --all Bot Chat', source=telegram))
+ route = runner.session_store._generate_session_key(telegram)
+ assert runner.session_store.lookup_by_session_key(route).session_id == head.session_id
+ if opened != 'followed by the Desktop':
+ runner = _Runner(tmp_path)
+ warm = await initialize_session_authority(runner, profile_id='fixture', instance_id='restarted')
+ if opened == 'a viewer after a restart':
+ warm.authorize(owner, head, 'session:read')
+ warm.register(telegram) # the lane's next inbound
+ warm.authorize(owner, head, 'session:submit') # the lane's conversation: writable there
+
+ if takeover == 'another chat handed off':
+ other = create_local_session(warm, owner, {'request_id': 'other-chat', 'source': 'gui',
+ 'cwd': str(tmp_path), 'model': 'frozen', 'toolsets': []})
+ await _handoff(runner, warm, other.session_id, telegram)
+ else:
+ runner.session_store.reset_session(route)
+ warm.register(telegram)
+ assert warm.logical_owner(runner.session_store.lookup_by_session_key(route).session_id) != head.session_id
+
+ cold = await initialize_session_authority(_Runner(tmp_path), profile_id='fixture', instance_id='cold')
+ for authority in (warm, cold):
+ authority.authorize(owner, head, 'session:read')
+ for capability in ('session:submit', 'session:control'):
+ with pytest.raises(RuntimeStoreError, match='session_superseded'):
+ authority.authorize(owner, head, capability)
+ assert authority.agent(head) is None # never the lane's running agent
+
+
@pytest.mark.asyncio
async def test_a_moved_receipt_with_a_malformed_lineage_never_stops_the_boot(tmp_path, monkeypatch):
from gateway.session_authority import initialize_session_authority