The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): stamp a viewer close only once it is claimed
unsupportedpastels70149168a88c
diff --git a/gateway/session_viewer_close.py b/gateway/session_viewer_close.pyindex 9e1fdf3f3eb..4e6708cd10b 100644--- a/gateway/session_viewer_close.py+++ b/gateway/session_viewer_close.py@@ -56,22 +56,21 @@ async def settle_viewer_close(authority, session_id):if close is None or live is None or live.subscribers:authority.pending_closes.pop(session_id, None)return- from gateway.session_acp_lifecycle import end_idle_viewer_session, reopen_if_reattached- from gateway.session_runtime_workers import tracked_write- await tracked_write(authority, partial(end_idle_viewer_session, authority, session_id, close.reason),- then=partial(reopen_if_reattached, authority, session_id))target = _physical_target(authority, session_id)# Every await below can let a viewer reattach (attach drops the pending close) or another# settler run, so the close is re-checked after each one.- if not _still_pending(authority, session_id, live, close):- returnif await asyncio.to_thread(_busy, authority, session_id, target):return # the drain's next idle point completes itrow = await asyncio.to_thread(authority.db.get_session, target)if not _still_pending(authority, session_id, live, close):return- # Claimed with no await since the check: exactly one settler runs the lifecycle.+ # Claimed with no await since the check: exactly one settler stamps the row and runs the+ # lifecycle, and a close cancelled before its claim never wrote ``ended_at``.del authority.pending_closes[session_id]+ from gateway.session_acp_lifecycle import end_idle_viewer_session, reopen_if_reattached+ from gateway.session_runtime_workers import tracked_write+ await tracked_write(authority, partial(end_idle_viewer_session, authority, session_id, close.reason),+ then=partial(reopen_if_reattached, authority, session_id))platform = (row or {}).get('source') or (live.source.platform.value if live.source else '')await _run_close_lifecycle(authority, live, target, platform, close.reason)diff --git a/tests/gateway/test_viewer_close_lifecycle.py b/tests/gateway/test_viewer_close_lifecycle.pyindex f99a33097e7..121f9d02a67 100644--- a/tests/gateway/test_viewer_close_lifecycle.py+++ b/tests/gateway/test_viewer_close_lifecycle.py@@ -132,8 +132,30 @@ async def test_a_viewer_reattaching_during_the_idle_check_cancels_the_close(tmp_release.set()await closing- # The conversation is being watched again: its agent stays cached and nothing is finalized.- assert calls == []+ # The conversation is being watched again: its agent stays cached, nothing is finalized and+ # the row is not left ended under the attached viewer.+ assert calls == [] and authority.pending_closes == {}+ row = db.get_session('chat')+ assert row['ended_at'] is None and row['end_reason'] is None+++async def test_a_close_stamps_the_row_only_once_it_is_claimed(tmp_path, monkeypatch):+ db, authority, calls = await _owner(tmp_path, monkeypatch)+ viewer, subscription = await _open(db, authority, tmp_path)+ entered, release = _gate_busy_check(monkeypatch)++ closing = asyncio.create_task(_call(viewer, 'session.detach', session_id='chat',+ subscription_id=subscription, end_reason='cli_close'))+ await async_wait_until(lambda: len(entered) == 1)+ # Still cancellable: a viewer may yet reattach, so nothing is stamped.+ assert db.get_session('chat')['ended_at'] is None+ release.set()+ await closing++ row = db.get_session('chat')+ assert row['ended_at'] is not None and row['end_reason'] == 'cli_close'+ assert [name for name, _ in calls] == ['commit', 'finalize']@pytest.mark.asyncio