The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): keep the preclaim refusal inside the storage retry
unsupportedpastels6309003fce00
diff --git a/gateway/session_authority.py b/gateway/session_authority.pyindex 0fe4295d43b..28398b7ef34 100644--- a/gateway/session_authority.py+++ b/gateway/session_authority.py@@ -1132,30 +1132,33 @@ class SessionAuthority:route; None when the drain must exit (paused, or stopping with committed rows left queued)."""backoff, conflicted = 0.0, Nonewhile True:+ # The refusal handling's own reads and writes share the claim's storage retry: the retry's+ # claim conflicts on the same head again (``conflicted`` kept) and refuses that exact row.try:- row, first = await self._claim_next(ref, live)- except RuntimeStoreError as exc:- if exc.reason == 'unknown_execution':- # An automation turn nobody is waiting on settles interrupted (GW-10); only a- # person's lost turn keeps the FIFO paused for them to resolve.- from gateway.session_unattended_unknown import settle_unattended_unknown- if await settle_unattended_unknown(self, ref):- continue- if exc.reason == 'admission_conflict':- # A verdict on the head alone (its route or binding moved under it) that every drain- # repeats: pausing behind it strands the FIFO for good. Refused only once the same head- # conflicted twice, read after each: never a successor a cancel advanced the FIFO to.- head = next((row['admission_id'] for row in list_session_admissions(- self.db, session_id=ref.session_id) if row['status'] == 'queued'), None)- if head is not None and head != conflicted:- conflicted = head- continue- if head is not None and await self._refuse_head(ref, head, exc.reason):- continue- import logging- logging.getLogger(__name__).warning('Session %s paused: %s', ref.session_id, exc.reason)- self._pause(ref, exc.reason)- return None+ try:+ row, first = await self._claim_next(ref, live)+ except RuntimeStoreError as exc:+ if exc.reason == 'unknown_execution':+ # An automation turn nobody is waiting on settles interrupted (GW-10); only a+ # person's lost turn keeps the FIFO paused for them to resolve.+ from gateway.session_unattended_unknown import settle_unattended_unknown+ if await settle_unattended_unknown(self, ref):+ continue+ if exc.reason == 'admission_conflict':+ # A verdict on the head alone (its route or binding moved under it) that every drain+ # repeats: pausing behind it strands the FIFO for good. Refused only once the same+ # head conflicted twice, read after each: never a successor a cancel advanced to.+ head = next((row['admission_id'] for row in list_session_admissions(+ self.db, session_id=ref.session_id) if row['status'] == 'queued'), None)+ if head is not None and head != conflicted:+ conflicted = head+ continue+ if head is not None and await self._refuse_head(ref, head, exc.reason):+ continue+ import logging+ logging.getLogger(__name__).warning('Session %s paused: %s', ref.session_id, exc.reason)+ self._pause(ref, exc.reason)+ return Noneexcept (OSError, sqlite3.OperationalError) as exc:# A writer held past its patience or a full disk is transient, and every _schedule# caller is event-driven: a dead drain would strand the committed head and hang itsdiff --git a/tests/gateway/test_preclaim_conflict_settles_head.py b/tests/gateway/test_preclaim_conflict_settles_head.pyindex 144a43beac9..574c6aac876 100644--- a/tests/gateway/test_preclaim_conflict_settles_head.py+++ b/tests/gateway/test_preclaim_conflict_settles_head.py@@ -39,3 +39,50 @@ async def test_conflicted_head_settles_with_its_refusal_and_the_successor_runs(tfor r in list_session_admissions(db, session_id='s', pending_only=False)}assert statuses == {'stale': ('terminal', 'cancelled'), 'follower': ('terminal', 'completed')}assert waiter.exception().reason == 'admission_conflict'+++async def test_refusal_write_storage_error_retries_and_still_settles_that_head(tmp_path, monkeypatch):+ import sqlite3++ from gateway import session_api_turn, session_authority, session_finite+ from gateway.config import Platform++ db, authority = _authority(tmp_path, monkeypatch, platform=Platform.API_SERVER)+ monkeypatch.setattr(session_authority, '_CLAIM_RETRY_MIN_S', 0.0)+ executed, cancels = [], []++ def check_api_turn(authority, ref, payload):+ if payload['text'] == 'stale':+ raise RuntimeStoreError('admission_conflict')+ monkeypatch.setattr(session_api_turn, 'check_api_turn', check_api_turn)++ async def execute(authority, ref, row):+ executed.append(row['payload']['text'])+ return 'done'+ monkeypatch.setattr(session_finite, 'execute_finite_admission', execute)++ cancel = session_authority.cancel_session_input++ def locked_once(db, **kwargs):+ cancels.append(kwargs['admission_id'])+ if len(cancels) == 1:+ raise sqlite3.OperationalError('database is locked')+ return cancel(db, **kwargs)+ monkeypatch.setattr(session_authority, 'cancel_session_input', locked_once)++ with db:+ authority._schedule = lambda ref: None+ head = await _submit(authority, 'stale')+ await _submit(authority, 'follower')+ waiter = authority.waiters.setdefault(head.admission_id, asyncio.get_running_loop().create_future())+ del authority._schedule+ authority._schedule(REF)+ await asyncio.wait_for(authority.sessions['s'].task, 5)++ assert cancels == [head.admission_id, head.admission_id]+ assert executed == ['follower']+ statuses = {r['request_id']: (r['status'], r['outcome'])+ for r in list_session_admissions(db, session_id='s', pending_only=False)}+ assert statuses == {'stale': ('terminal', 'cancelled'), 'follower': ('terminal', 'completed')}+ assert waiter.exception().reason == 'admission_conflict'