Skip to content

The agent that grows with you

fix(gateway): an admitted completion carries main's ownership proof into its turn

Teknium461932d44158
diff --git a/gateway/session_automation.py b/gateway/session_automation.py
index 1f1d74b24ab..96e0d3b1eb4 100644
--- a/gateway/session_automation.py
+++ b/gateway/session_automation.py
@@ -83,9 +83,9 @@ def automation_display_metadata(metadata):
# persisted; notification_category="diagnostic" rides the snapshot so the replayed wake still mutes,
# and display_kind/display_text so the persisted row is the card the producer named.
# gateway_proven_session_id / gateway_completion_ids are main's delivery-time ownership proof for the
-# classic resolver (802fd5782f3, fd4e277c849). Here _owner() proves the route inside the admission
-# itself and the durable claim settles only on that commit, so they are accepted and not persisted:
-# carrying a sibling set in the fingerprint would make the same completion's retry a new identity.
+# classic resolver (802fd5782f3, fd4e277c849). They are accepted and not persisted (a sibling set in the
+# fingerprint would make the same completion's retry a new identity); execute_admission re-stamps the
+# proof at execution from the target the pre-claim check validated, and the turn's resolver reads it.
_AUTOMATION_METADATA = frozenset({'gateway_session_key', 'gateway_session_id', 'automation_identities',
'turn_author', 'notification_origin', 'original_trigger_message_id', 'notification_category',
'display_kind', 'display_text', 'gateway_proven_session_id', 'gateway_completion_ids'})
diff --git a/gateway/session_ingress.py b/gateway/session_ingress.py
index f5486055504..6f83483d1d7 100644
--- a/gateway/session_ingress.py
+++ b/gateway/session_ingress.py
@@ -209,6 +209,11 @@ async def execute_admission(authority, ref, row):
if 'local_automation_v1' in row['payload']:
from gateway.session_automation import restore_local_automation
event = restore_local_automation(authority, ref, row)
+ if event.internal and 'gateway_session_id' in event.metadata:
+ # Main's ownership proof (802fd5782f3): the session the pre-claim check just validated, so the
+ # turn's resolver never re-reads (and on a transient failure drops) an admitted completion.
+ event.metadata['gateway_proven_session_id'] = (
+ authority.physical_target(ref) if 'native_text_v1' in row['payload'] else event.metadata['gateway_session_id'])
provenance = row['payload'].get('native_text_v1', {}).get('provenance')
home = None
if provenance is not None:
diff --git a/tests/gateway/test_completion_ownership_proof.py b/tests/gateway/test_completion_ownership_proof.py
new file mode 100644
index 00000000000..34f9a668c8a
--- /dev/null
+++ b/tests/gateway/test_completion_ownership_proof.py
@@ -0,0 +1,44 @@
+"""An admitted completion keeps main's post-compression guarantee: the executed event carries the
+ownership proof, so one transient owner read in the turn's resolver never drops it (N77)."""
+import asyncio
+
+import pytest
+
+from tests.gateway.test_automation_compression_media import _admit_human, _discord_owner
+
+
+async def test_admitted_completion_survives_a_transient_owner_read_after_compression(tmp_path, monkeypatch):
+ from gateway.platforms.event import MessageEvent
+ from gateway.session_contract import SessionRef
+ from gateway.session_ingress import execute_admission
+ from hermes_state_runtime import list_session_admissions
+ from tests.gateway.test_completion_admission import pending
+
+ runner, _, authority, source = await _discord_owner(tmp_path, monkeypatch)
+ receipt = await _admit_human(runner, authority, MessageEvent(text='human', source=source, message_id='human'))
+ key = runner.session_store._generate_session_key(source)
+ sid = receipt.ref.session_id
+ event = dict(pending(key, 'ci-exec'), parent_session_id=sid)
+ assert await runner._deliver_async_delegation_group([event]) is True
+ row = list_session_admissions(authority.db, session_id=sid, pending_only=False)[-1]
+ db = authority.db
+ db.append_message(sid, 'user', 'human')
+ child = sid + '-child' # the owner compresses after admission
+ db.publish_compression_child(parent_session_id=sid, child_session_id=child, source='discord',
+ messages=[{'role': 'user', 'content': 'summary'}], require_compression_lease=False)
+ assert runner.session_store.advance_compression_session(key, sid, child) is not None
+ resolved = []
+
+ async def turn(ev):
+ resolved.append(await runner._hmwa_resolve_session(ev, ev.source))
+ return 'ok'
+
+ async def transient(*a, **k):
+ return 'retry', a[0], None
+ monkeypatch.setattr(runner, '_handle_message', turn)
+ monkeypatch.setattr(runner, '_lookup_completion_owner', transient)
+ await execute_admission(authority, SessionRef(authority.profile_id, sid), row)
+ assert resolved and resolved[0] is not None, 'admitted completion dropped before its turn'
+ assert resolved[0][1].session_id == child
+ await asyncio.sleep(0)