Skip to content

The agent that grows with you

fix(gateway): a lost automation worker settles on the exit that fenced it

Tekniume399f60942b6
diff --git a/gateway/session_authority.py b/gateway/session_authority.py
index a2494eaa8ca..ffff6db6593 100644
--- a/gateway/session_authority.py
+++ b/gateway/session_authority.py
@@ -1120,7 +1120,8 @@ class SessionAuthority:
async def _execute_claimed(self, ref, live, row):
"""Run the claimed admission: ``(response, outcome)``. The executing stamp clears the moment
- execution returns; a lost managed worker pauses the FIFO and re-raises ManagedExecutionUnknown."""
+ execution returns; a lost managed worker settles an automation turn (``settled_unknown``) or pauses
+ the FIFO and re-raises ManagedExecutionUnknown."""
from gateway.session_finite import execute_finite_admission
from gateway.session_managed_worker import ManagedExecutionUnknown
try:
@@ -1135,6 +1136,11 @@ class SessionAuthority:
self.pending_stops.pop(ref.session_id, None)
self.pending_steers.pop(ref.session_id, None)
self.adopted.pop(ref.session_id, None)
+ # An automation turn nobody waits on settles here, at the exit that fenced it (GW-10),
+ # so the followers already queued run in this drain; a person's turn still pauses.
+ from gateway.session_unattended_unknown import settle_unattended_unknown
+ if await settle_unattended_unknown(self, ref):
+ return None, 'settled_unknown'
self._pause(ref, 'unknown_execution')
raise
except Exception:
@@ -1175,6 +1181,9 @@ class SessionAuthority:
admission_id = row['admission_id']
try:
response, outcome = await self._execute_claimed(ref, live, row)
+ if outcome == 'settled_unknown':
+ self.deferred_resets.pop(live.route, None) # no settlement follows, as below
+ continue
except BaseException as exc:
# No settlement follows (a lost managed worker, a cancelled drain): the turn is never
# replayed, so its deferred reset is dropped (the next turn re-detects the overflow),
diff --git a/tests/gateway/test_unknown_automation_settles.py b/tests/gateway/test_unknown_automation_settles.py
index b800978b85c..4c69e08e5b1 100644
--- a/tests/gateway/test_unknown_automation_settles.py
+++ b/tests/gateway/test_unknown_automation_settles.py
@@ -225,3 +225,42 @@ async def test_captured_completed_automation_result_gets_no_restart_notice(tmp_p
assert not [m for m in db.get_messages('s') if m.get('display_kind') == 'failed_turn']
finally:
store.close_all_db_handles()
+
+
+async def test_lost_automation_worker_settles_on_its_own_exit_and_runs_the_queued_follower(tmp_path, monkeypatch):
+ """Review #141357 F2: a managed worker lost mid-turn fences its automation head ``unknown`` and
+ raises ManagedExecutionUnknown. That same drain settles it and runs the follower already queued,
+ without waiting for an unrelated wake."""
+ from gateway.session_managed_worker import ManagedExecutionUnknown
+ from gateway.session_settlement_recovery import recover_failed_settlement
+ store, runner = _runner(tmp_path, monkeypatch, [])
+ db = store._db
+ db.create_session('s', source='telegram')
+ authority = await initialize_session_authority(runner, profile_id='p', instance_id='owner-1')
+ authority.sessions['s'] = LiveSession(SessionSource(platform=Platform.TELEGRAM, chat_id='gw10'), 's')
+ ref = SessionRef('p', 's')
+ actor = AuthorityConnection(authority, Peer(), {'user_id': 'human'}).actor
+ ran = []
+ try:
+ authority._schedule = lambda ref: None
+ head_id = await _automation_head(authority, actor, ref)
+ follower = await authority.submit(actor, Submission('follower', ref, {'text': 'FOLLOWER'}, 'queue'))
+
+ async def execute(owner, ref, row):
+ if row['admission_id'] == head_id:
+ await recover_failed_settlement(owner, row)
+ raise ManagedExecutionUnknown('managed_worker_unknown')
+ ran.append(row['payload']['text'])
+ return 'ACK'
+ monkeypatch.setattr('gateway.session_finite.execute_finite_admission', execute)
+ del authority._schedule
+ authority._schedule(ref)
+ live = authority.sessions['s']
+ await async_wait_until(lambda: live.task is not None and live.task.done(), what='drain finished')
+ assert ran == ['FOLLOWER']
+ statuses = _statuses(db)
+ assert statuses[head_id] == ('terminal', 'interrupted')
+ assert statuses[follower.admission_id][0] == 'terminal'
+ finally:
+ store.close_all_db_handles()