The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a late tick of a settled batch sibling is still stale
Teknium4a5753f3d24f
diff --git a/gateway/session_automation.py b/gateway/session_automation.pyindex cff6e714d09..3aa5419bc90 100644--- a/gateway/session_automation.py+++ b/gateway/session_automation.py@@ -120,8 +120,8 @@ def _heartbeat_lock(runner):def _process_completed(runner, authority, owner, event):"""Whether the completion of tick ``event``'s process is admitted into the session ``owner``: by- its exact identity (``started_at`` names the process incarnation), or as a sibling in a pending- batch."""+ its exact identity (``started_at`` names the process incarnation), or as a sibling in a batch,+ pending or settled: only the batch's primary is its ``request_id``."""completion = runner._completion_delivery_identity({'type': 'completion', 'session_id': event.get('session_id'), 'started_at': event.get('started_at')})if completion is None:@@ -131,7 +131,7 @@ def _process_completed(runner, authority, owner, event):if conn.execute('SELECT 1 FROM session_admissions WHERE target_session_id=? AND request_id=?',(owner, identity)).fetchone():return True- for row in list_session_admissions(authority.db, session_id=owner):+ for row in list_session_admissions(authority.db, session_id=owner, pending_only=False):descriptor = row['payload'].get('local_automation_v1') or row['payload'].get('native_text_v1', {}).get('automation', {})if identity in (descriptor.get('identities') or ()):return Truediff --git a/tests/gateway/test_local_producer_admission.py b/tests/gateway/test_local_producer_admission.pyindex 70816f1bec4..aeb6a1ecc7c 100644--- a/tests/gateway/test_local_producer_admission.py+++ b/tests/gateway/test_local_producer_admission.py@@ -266,6 +266,38 @@ async def test_a_messaging_chat_gets_its_ticks_folded_and_none_after_the_completassert _tick_output(folded) == 'proc_job/1\nproc_job/2\n'+async def _drain_one(runner, event):+ waiting = queue.Queue()+ waiting.put(event)+ await runner._drain_watch_notifications(waiting)+ return waiting.qsize()+++def _automation_rows(messaging):+ rows = list_session_admissions(messaging.authority.db, session_id=messaging.owner, pending_only=False)+ return {row['request_id']: row for row in rows if row['principal_id'] == 'automation:' + messaging.route}+++async def test_a_late_tick_of_a_settled_batch_member_is_dropped(messaging):+ """A completion batch names only its primary in ``request_id``; the siblings live in the payload.+ Once the batch turn has finished, a late tick of a sibling process is still stale."""+ from gateway.session_automation import producer_identity+ batch = [messaging.event('completion', session_id=process, exit_code=0, output='done')+ for process in ('proc_a', 'proc_b')]+ primary = dict(batch[0], _automation_identities=[producer_identity(messaging.runner, e) for e in batch])+ assert await messaging.runner._inject_watch_notification('[2 finished]', primary) is True+ with messaging.authority.db._lock:+ messaging.authority.db._conn.execute(+ "UPDATE session_admissions SET status='terminal',outcome='completed' WHERE request_id=?",+ ('["completion","proc_a",1.0]',))+ messaging.authority.db._conn.commit()++ assert await _drain_one(messaging.runner, messaging.event(+ 'heartbeat', session_id='proc_b', **_tick_fields('proc_b', 1))) == 0+ assert list(_automation_rows(messaging)) == ['["completion","proc_a",1.0]'], 'the late tick was admitted'++@pytest.mark.asyncioasync def test_a_served_profiles_bot_chat_takes_every_dm_with_no_client_attached(gateway):sender = gateway.connect(gateway.secondary)