Skip to content

The agent that grows with you

fix(gateway): an image retry re-staged under a new filename replays instead of conflicting

Teknium3c2026869e37
diff --git a/gateway/session_ingress_media.py b/gateway/session_ingress_media.py
index 7c9a4fc60b5..bfc748e1fe4 100644
--- a/gateway/session_ingress_media.py
+++ b/gateway/session_ingress_media.py
@@ -60,25 +60,36 @@ def admit_attachments(attachments, *, admitted=None):
paths = [Path(item['path']) for item in attachments]
if any(not path.is_absolute() or path.resolve().parent != staging for path in paths):
raise RuntimeStoreError('invalid_params')
+ mimes = [item['mime'] for item in attachments]
+ row = admitted() if admitted is not None else None
+ committed = row['payload'].get('attachments_v1') if row is not None else None
# A hardlink placed in staging is a second name for a file outside it (~/.ssh/id_rsa).
try:
if any(path.lstat().st_nlink != 1 for path in paths):
raise RuntimeStoreError('invalid_params')
+ # Retry identity is the committed bytes, not the disposable staging name: a client that
+ # re-staged the same image under a new name is retrying, not sending different work.
+ retry = (committed is not None and committed['media_types'] == mimes
+ and [r['sha256'] for r in committed['media']] == [_sha256(path) for path in paths])
except FileNotFoundError as exc:
- row = admitted() if admitted is not None else None
- committed = row['payload'].get('attachments_v1') if row is not None else None
- if (row is None or committed is None or [Path(r['path']).name for r in committed['media']] != [p.name for p in paths]
- or committed['media_types'] != [item['mime'] for item in attachments]):
+ if (committed is None or [Path(r['path']).name for r in committed['media']] != [p.name for p in paths]
+ or committed['media_types'] != mimes):
raise RuntimeStoreError('invalid_params') from exc
+ retry = True
+ except (OSError, ValueError) as exc:
+ raise RuntimeStoreError('invalid_params') from exc
+ if retry:
# A live row still executes these bytes, so they must verify; a terminal row is
# exact-retry evidence by digest only. ``admit_session_input`` still checks the digest.
if row['status'] != 'terminal':
restore_native_media(committed['media'])
return {'attachments_v1': committed}
- except OSError as exc:
- raise RuntimeStoreError('invalid_params') from exc
- return {'attachments_v1': {'media': capture_native_media(paths),
- 'media_types': [item['mime'] for item in attachments]}}
+ return {'attachments_v1': {'media': capture_native_media(paths), 'media_types': mimes}}
+
+
+def _sha256(path):
+ with _open_regular(path) as source:
+ return hashlib.file_digest(source, 'sha256').hexdigest()
def restore_attachments(payload):
diff --git a/tests/gateway/test_prompt_attachments.py b/tests/gateway/test_prompt_attachments.py
index 18199a35b86..06e89fc686c 100644
--- a/tests/gateway/test_prompt_attachments.py
+++ b/tests/gateway/test_prompt_attachments.py
@@ -99,3 +99,30 @@ async def test_lost_ack_retry_reconciles_a_queued_image_after_staging_is_gone(tm
with pytest.raises(RuntimeStoreError, match='invalid_params'):
await submit('r-fresh')
assert [row['request_id'] for row in list_session_admissions(authority.db, session_id='s')] == ['r-lost']
+
+
+async def test_retry_with_the_same_image_restaged_under_a_new_name_replays(tmp_path, monkeypatch):
+ """Retry identity is the committed bytes, not the client's disposable staging filename: the
+ same request re-staged under a new name replays the admission instead of conflicting, and
+ different bytes under the same request id still conflict."""
+ from gateway.platforms.base import cache_image_from_bytes
+ from gateway.session_contract import Principal, SessionRef, Submission
+ from gateway.session_authority import SessionAuthority
+ 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(path):
+ return authority.submit(actor, Submission('r-same', SessionRef('p', 's'),
+ {'text': 'look', 'attachments': [{'path': path, 'mime': 'image/png'}]}, 'queue'))
+ first = await submit(cache_image_from_bytes(_ONE_PX_PNG, '.png'))
+ restaged = cache_image_from_bytes(_ONE_PX_PNG, '.png')
+ assert (await submit(restaged)).admission_id == first.admission_id
+ with pytest.raises(RuntimeStoreError, match='admission_conflict'):
+ await submit(cache_image_from_bytes(_ONE_PX_PNG + b'\0', '.png'))
+ assert len(list_session_admissions(authority.db, session_id='s')) == 1