Skip to content

The agent that grows with you

fix(gateway): the unknown-turn barrier resolves a reset tip to its admission owner

Tekniumb7511e257b4f
diff --git a/gateway/run_runtime.py b/gateway/run_runtime.py
index 7a5a16a1f4f..59374ceecf0 100644
--- a/gateway/run_runtime.py
+++ b/gateway/run_runtime.py
@@ -134,7 +134,7 @@ def release_unknown_turn_markers(runner):
admission recovered ``unknown`` (the owner died before its terminal commit) owes no reply, so
its marker is cleared rather than ledgered as a delivery the FIFO never committed. A terminal
admission keeps its marker: its persisted reply is ledgered and sent once, never re-run."""
- from gateway.session_authorities import owner_scope
+ from gateway.session_authorities import admission_owner, owner_scope
store = getattr(runner, 'session_store', None)
if store is None:
return
@@ -145,7 +145,7 @@ def release_unknown_turn_markers(runner):
if not unknown:
continue
for entry in store.list_sessions():
- if entry.active_turn_token and authority.logical_owner(entry.session_id) in unknown:
+ if entry.active_turn_token and admission_owner(authority, entry.session_id) in unknown:
store.clear_turn_active(entry.session_key, entry.active_turn_token)
diff --git a/gateway/run_startup.py b/gateway/run_startup.py
index 6ce9f7da7b3..b05ef89bc67 100644
--- a/gateway/run_startup.py
+++ b/gateway/run_startup.py
@@ -611,10 +611,11 @@ class GatewayStartupMixin:
for entry in candidates:
# Canonical admissions own restart decisions, including unknown pauses.
# Legacy synthetic resume must not race their restored FIFO.
- from gateway.session_authorities import all_authorities
+ from gateway.session_authorities import admission_owner, all_authorities
if any(authority.db._read_one(
- 'SELECT 1 FROM session_admissions WHERE target_session_id=? LIMIT 1',
- (entry.session_id,)) for authority in all_authorities(self)):
+ 'SELECT 1 FROM session_admissions WHERE target_session_id IN (?,?) LIMIT 1',
+ (entry.session_id, admission_owner(authority, entry.session_id)))
+ for authority in all_authorities(self)):
continue
# Epoch math: the marker was stamped naive-local by the previous process, possibly
# on the other side of a DST change; wall-clock subtraction is off by the shift.
diff --git a/gateway/session_authorities.py b/gateway/session_authorities.py
index f35ad63b407..65e1a370205 100644
--- a/gateway/session_authorities.py
+++ b/gateway/session_authorities.py
@@ -143,6 +143,18 @@ def all_authorities(runner):
return [authority] if authority is not None else []
+def admission_owner(authority, session_id):
+ """The admission identity a physical store row answers to: its compression root, then the
+ local creation id a canonical reset forks under (a reset child is not a compression child, so
+ ``logical_owner`` alone stops at the child and misses the conversation's admissions)."""
+ from hermes_state_local import local_lineage_owner
+ sid = authority.logical_owner(session_id)
+ if not sid:
+ return sid
+ with authority.db._read_ctx() as conn:
+ return local_lineage_owner(conn, sid)
+
+
def owner_scope(authority, *, hydrate_secrets=False):
"""Runtime scope of the profile that owns *authority* — under multiplex only.
diff --git a/tests/gateway/test_reset_tip_unknown_barrier.py b/tests/gateway/test_reset_tip_unknown_barrier.py
new file mode 100644
index 00000000000..c292f717e31
--- /dev/null
+++ b/tests/gateway/test_reset_tip_unknown_barrier.py
@@ -0,0 +1,65 @@
+"""A local conversation reset before a crash keeps its admissions on the creation id while the
+store routes the physical reset child. The unknown-turn barrier must resolve the child to that
+owner: neither the crash marker release nor legacy auto-resume may treat the child as admission-
+free and silently re-run a turn whose canonical admission is ``unknown``."""
+import asyncio
+from contextlib import closing
+from datetime import datetime
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+
+from gateway.config import Platform
+from gateway.session import SessionEntry
+from gateway.session_authority import SessionAuthority
+from hermes_state import SessionDB
+import hermes_state_runtime as rt
+from tests.gateway.restart_test_helpers import make_restart_runner, make_restart_source
+from tests.gateway.test_unknown_resolution_physical_worker import _local_reset
+
+
+def _unknown_after_reset(tmp_path, runner):
+ db = SessionDB(tmp_path / 'state.db')
+ epoch = rt.begin_runtime_epoch(db, instance_id='crashed')
+ logical = _local_reset(db, epoch, 'logical', 'physical')
+ rt.admit_session_input(db, epoch=epoch, principal_id='human', session_id=logical,
+ request_id='turn', payload={'text': 'turn'})
+ rt.claim_session_input(db, epoch=epoch, session_id=logical)
+ epoch = rt.begin_runtime_epoch(db, instance_id='restarted')
+ rt.recover_session_inputs(db, epoch=epoch) # the crashed turn is now 'unknown'
+ return db, SessionAuthority(runner, profile_id='profile', instance_id='restarted', db=db, epoch=epoch)
+
+
+def _entry(**extra):
+ return SessionEntry(session_key='agent:main:telegram:dm:c', session_id='physical', created_at=datetime.now(),
+ updated_at=datetime.now(), origin=make_restart_source(chat_id='c'),
+ platform=Platform.TELEGRAM, chat_type='dm', **extra)
+
+
+def test_crash_marker_on_a_reset_tip_is_released_for_its_unknown_admission(tmp_path):
+ from gateway.run_runtime import release_unknown_turn_markers
+ entry = _entry(active_turn_token='tok')
+ cleared = []
+ store = SimpleNamespace(list_sessions=lambda: [entry],
+ clear_turn_active=lambda key, token: cleared.append((key, token)))
+ runner = SimpleNamespace(session_store=store, config=SimpleNamespace(multiplex_profiles=False))
+ db, authority = _unknown_after_reset(tmp_path, runner)
+ with closing(db):
+ runner.session_authorities = [authority]
+ release_unknown_turn_markers(runner)
+ assert cleared == [(entry.session_key, 'tok')]
+
+
+async def test_legacy_resume_skips_a_reset_tip_whose_admission_is_unknown(tmp_path):
+ runner, adapter = make_restart_runner()
+ db, authority = _unknown_after_reset(tmp_path, runner)
+ with closing(db):
+ runner.session_authorities = [authority]
+ entry = _entry(resume_pending=True, resume_reason='restart_interrupted', last_resume_marked_at=datetime.now())
+ runner.session_store._entries = {entry.session_key: entry}
+ adapter.handle_message = AsyncMock()
+ assert runner._schedule_resume_pending_sessions() == 0
+ await asyncio.sleep(0)
+ adapter.handle_message.assert_not_called()