Skip to content

The agent that grows with you

fix(acp): editor sessions left mid-turn end once their work settles

Teknium6479bca1101c
diff --git a/gateway/session_authority.py b/gateway/session_authority.py
index 5d5b10e3c7f..ff10cbffe07 100644
--- a/gateway/session_authority.py
+++ b/gateway/session_authority.py
@@ -506,6 +506,9 @@ class SessionAuthority:
# The FIFO is moving again (or empty): the next pause is a new episode.
live.pause_notified = False
if row is None:
+ # A viewer that left while this work ran could not end the ACP session then.
+ from gateway.session_acp_lifecycle import end_idle_acp_session
+ end_idle_acp_session(self, ref.session_id)
return
admission_id = row['admission_id']
with live.event_stream.lock:
diff --git a/tests/acp_adapter/test_gateway_acp_session_end.py b/tests/acp_adapter/test_gateway_acp_session_end.py
index ec2738be3d9..37251be7927 100644
--- a/tests/acp_adapter/test_gateway_acp_session_end.py
+++ b/tests/acp_adapter/test_gateway_acp_session_end.py
@@ -74,3 +74,53 @@ async def test_last_viewer_or_shutdown_ends_only_idle_acp_sessions(tmp_path, mon
assert ended(busy_row) == (False, None) and ended(gui_row) == (False, None)
finally:
store.close_all_db_handles()
+
+
+async def test_acp_session_left_mid_turn_ends_once_its_fifo_drains(tmp_path, monkeypatch):
+ """The last viewer leaving during a turn cannot end the session; the drain going idle must."""
+ import asyncio
+ from gateway import run
+ from gateway.config import GatewayConfig
+ from gateway.session import SessionStore
+ from gateway.session_authority import initialize_session_authority
+ from gateway.session_contract import Submission
+ from gateway.session_controls import AuthorityConnection
+ from gateway.session_local import create_local_session
+ from hermes_state_local import local_receipt
+
+ release = asyncio.Event()
+
+ async def execute(authority, ref, row): # the model turn; drain + settlement stay real
+ await release.wait()
+ return 'done'
+
+ monkeypatch.setattr('gateway.session_finite.execute_finite_admission', execute)
+ monkeypatch.setattr(run, '_load_gateway_config', lambda: {'platform_toolsets': {'acp': [], 'gui': []}})
+ store = SessionStore(tmp_path / 'sessions', GatewayConfig())
+ runner = SimpleNamespace(session_store=store, _session_db=store._db, adapters={}, _draining=False,
+ _cached_agent_for=lambda route: None)
+ runner._adapter_for_source = lambda source: runner.adapters.get(source.platform)
+ authority = await initialize_session_authority(runner, profile_id='fixture', instance_id='owner')
+ try:
+ rows = {}
+ for source in ('acp', 'gui'):
+ viewer = AuthorityConnection(authority, object(), {'user_id': 'human', 'profile_id': 'fixture'})
+ ref = create_local_session(authority, viewer.actor, {'request_id': source, 'source': source,
+ 'cwd': str(tmp_path), 'model': 'frozen', 'toolsets': []})
+ target = local_receipt(authority.db, ref.session_id)['entry']['session_id']
+ reply = await viewer.dispatch({'id': 1, 'method': 'session.resume', 'params': {'session_id': ref.session_id}})
+ assert 'result' in reply, reply
+ await authority.submit(viewer.actor, Submission(source + '-turn', ref, {'text': 'work'}, 'queue'))
+ await asyncio.sleep(0.1)
+ await viewer.close()
+ assert authority.db.get_session(target)['ended_at'] is None, 'running work keeps the session open'
+ rows[source] = ref, target
+ release.set()
+ for ref, _ in rows.values():
+ await authority.sessions[ref.session_id].task
+ acp_row = authority.db.get_session(rows['acp'][1])
+ assert (acp_row['ended_at'] is not None, acp_row['end_reason']) == (True, 'acp_disconnect')
+ assert authority.db.get_session(rows['gui'][1])['ended_at'] is None, 'only ACP sessions end on idle'
+ finally:
+ store.close_all_db_handles()