Skip to content

The agent that grows with you

fix: keep the refusal and a durable collection obligation for refused local images

Teknium5b4eb69b4d43
diff --git a/gateway/session_authority.py b/gateway/session_authority.py
index a664a215eb4..3d9986193c4 100644
--- a/gateway/session_authority.py
+++ b/gateway/session_authority.py
@@ -367,13 +367,29 @@ class SessionAuthority:
payload=payload, intent=request.intent,
_authorize_write=_authorize_write)
except Exception:
- from gateway.session_ingress_media import release_unheld_media
- release_unheld_media(self.db, payload.get('attachments_v1', {}).get('media', []))
+ self._release_refused_capture(request, payload)
raise
self._publish_pending(request.ref)
self._schedule(request.ref)
return self._receipt(row)
+ def _release_refused_capture(self, request, payload):
+ """A refused submission's captured bytes have no owner unless another admission holds the
+ same bytes. They become durable retirement candidates (collected again by delete/prune if
+ this pass fails), then a holder-aware release runs. Its failure is logged and never replaces
+ the refusal the client must see (e.g. ``admission_conflict``)."""
+ media = payload.get('attachments_v1', {}).get('media', [])
+ if not media:
+ return
+ from hermes_state_media import collect_retired_media, retire_media
+ try:
+ self.db._execute_write(lambda conn: retire_media(conn, {'attachments_v1': {'media': media}}))
+ collect_retired_media(self.db)
+ except Exception:
+ import logging
+ logging.getLogger(__name__).exception(
+ 'Releasing refused attachments of request %s failed', request.request_id)
+
async def receipt(self, actor, ref, admission_id):
self.authorize(actor, ref, 'session:submit')
row = get_session_admission(self.db, admission_id=admission_id)
diff --git a/tests/gateway/test_prompt_attachments.py b/tests/gateway/test_prompt_attachments.py
index 82e4b48e63e..a9081f60482 100644
--- a/tests/gateway/test_prompt_attachments.py
+++ b/tests/gateway/test_prompt_attachments.py
@@ -155,3 +155,44 @@ async def test_discarding_a_lost_image_turn_releases_its_committed_bytes(tmp_pat
await authority.resolve_unknown(actor, SessionRef('p', 's'), receipt.admission_id, row['generation'])
assert rt.get_session_admission(authority.db, admission_id=receipt.admission_id)['status'] == 'terminal'
assert not os.path.exists(committed)
+
+
+async def test_refused_image_capture_is_released_without_masking_the_refusal(tmp_path, monkeypatch):
+ """A local submit captures image bytes before admission can refuse them. The refused capture is
+ released holder-aware (the accepted admission's bytes stay), and a collection failure never
+ replaces the refusal the client must see."""
+ from gateway import session_ingress_media
+ from gateway.platforms.base import cache_image_from_bytes
+ from gateway.session_authority import SessionAuthority
+ from gateway.session_contract import Principal, SessionRef, Submission
+ from hermes_state_runtime import RuntimeStoreError, list_session_admissions
+
+ async def answer(event):
+ return 'ok'
+ authority = await _authority(tmp_path, monkeypatch, answer)
+ monkeypatch.setattr(SessionAuthority, '_schedule', lambda self, ref: None)
+ actor = Principal('human', 'p', frozenset({'session:submit'}), 't')
+
+ def submit(data):
+ return authority.submit(actor, Submission('r-conflict', SessionRef('p', 's'), {'text': 'look', 'attachments': [
+ {'path': cache_image_from_bytes(data, '.png'), 'mime': 'image/png'}]}, 'queue'))
+ await submit(_ONE_PX_PNG)
+ retained = lambda: sorted(p.parent.name for p in session_ingress_media._media_root().glob('*/*'))
+ accepted = retained()
+ with pytest.raises(RuntimeStoreError, match='admission_conflict'):
+ await submit(_ONE_PX_PNG + b'B')
+ assert retained() == accepted, 'the refused capture must be released, the accepted one kept'
+
+ release = session_ingress_media.release_unheld_media
+ def collection_fails(*args, **kwargs):
+ raise OSError('collection write failed')
+ monkeypatch.setattr(session_ingress_media, 'release_unheld_media', collection_fails)
+ with pytest.raises(RuntimeStoreError, match='admission_conflict'):
+ await submit(_ONE_PX_PNG + b'C')
+ assert len(list_session_admissions(authority.db, session_id='s')) == 1
+ # The failed collection stays a durable obligation the next retirement sweep honours.
+ monkeypatch.setattr(session_ingress_media, 'release_unheld_media', release)
+ from hermes_state_media import collect_retired_media
+ collect_retired_media(authority.db)
+ assert retained() == accepted