Skip to content

The agent that grows with you

fix(gateway): a still-retiring profile leaves the published served set

Tekniumceae2aa8d887
diff --git a/gateway/run_runtime.py b/gateway/run_runtime.py
index 59374ceecf0..f66c13baef6 100644
--- a/gateway/run_runtime.py
+++ b/gateway/run_runtime.py
@@ -258,6 +258,8 @@ async def _retire_profile_authority(authority, timeout=None):
from gateway.session_runtime_workers import join_authority_work, stop_authority_work
timeout = TURN_SETTLE_SECONDS if timeout is None else timeout
authority.retiring = True
+ if getattr(authority.runner, 'session_ticket_store', None) is not None:
+ _publish_served_set(authority.runner) # discovery and tickets stop naming it now
stop_authority_work(authority)
service = getattr(authority, 'hosted_room_service', None)
rooms = None if service is None else asyncio.ensure_future(asyncio.to_thread(service.stop, timeout=timeout))
diff --git a/gateway/session_authorities.py b/gateway/session_authorities.py
index 65e1a370205..209b6474fd7 100644
--- a/gateway/session_authorities.py
+++ b/gateway/session_authorities.py
@@ -95,11 +95,16 @@ class SessionAuthorities:
return self._by_key[self.launch_key]
return self._by_key.get(hermes_home_key(get_hermes_home()))
+ def serving(self) -> list:
+ """Authorities that accept work: not a reserved-but-unbuilt slot, not one retiring (a
+ failed serve or an unserve whose turn outlived Stop keeps its slot but refuses admissions)."""
+ return [a for a in self._by_key.values() if a is not None and not getattr(a, 'retiring', False)]
+
def served_profiles(self) -> list[dict]:
- return [{'profile_id': a.profile_id, 'home': a.profile_id} for a in self._by_key.values()]
+ return [{'profile_id': a.profile_id, 'home': a.profile_id} for a in self.serving()]
def profile_ids(self) -> frozenset[str]:
- return frozenset(a.profile_id for a in self._by_key.values())
+ return frozenset(a.profile_id for a in self.serving())
def served_profile_name(home) -> str:
diff --git a/hermes_cli/dashboard_auth/native_http.py b/hermes_cli/dashboard_auth/native_http.py
index da6c7eab909..2c481c4aef9 100644
--- a/hermes_cli/dashboard_auth/native_http.py
+++ b/hermes_cli/dashboard_auth/native_http.py
@@ -121,7 +121,8 @@ async def authenticate_native_http(request):
or store.instance_id != descriptor.get('instance_id')
or any(a.instance_id != store.instance_id for a in authorities)
or descriptor.get('served_profiles') != [
- {'profile_id': a.profile_id, 'home': a.profile_id} for a in authorities]):
+ {'profile_id': a.profile_id, 'home': a.profile_id} for a in authorities
+ if not getattr(a, 'retiring', False)]):
return JSONResponse({'detail': 'Unauthorized'}, status_code=401)
try:
# The ticket names the served profile it was minted for; that home's authority owns it.
diff --git a/tests/gateway/test_parked_profile_served_set.py b/tests/gateway/test_parked_profile_served_set.py
new file mode 100644
index 00000000000..a6925ad5b6c
--- /dev/null
+++ b/tests/gateway/test_parked_profile_served_set.py
@@ -0,0 +1,44 @@
+"""A profile whose retirement misses its deadline keeps its slot (its writer may still run) but
+refuses every admission: it must leave the published served set and the ticket store at once,
+so discovery stops reporting it ready (and reads its parked verdict) instead of handing clients
+a profile whose every submit answers runtime_draining."""
+import asyncio
+from types import SimpleNamespace
+
+import pytest
+
+from gateway import run_runtime
+from gateway.runtime_bootstrap import TicketStore
+from gateway.session_authorities import SessionAuthorities
+from gateway.session_contract import Submission
+from tests.gateway.test_profile_retire_stops_turns import ACTOR, REF, _Agent, _authority
+
+
+async def test_still_retiring_profile_leaves_the_served_set(tmp_path, monkeypatch):
+ from gateway import session_finite
+ agent = _Agent(honours_stop=False)
+ db, authority, execute, _returned = _authority(tmp_path, agent)
+ monkeypatch.setattr(session_finite, 'execute_finite_admission', execute)
+ monkeypatch.setattr('gateway.session_cron.unbind_owner', lambda authority: None)
+ launch = SimpleNamespace(profile_id=str(tmp_path / 'launch'), retiring=False)
+ registry = SessionAuthorities(launch.profile_id)
+ registry.add(launch.profile_id, launch)
+ registry.add(authority.profile_id, authority, name='owned')
+ runner = authority.runner
+ runner.session_authorities = registry
+ runner.session_runtime_descriptor = {'served_profiles': registry.served_profiles()}
+ runner.session_ticket_store = TicketStore('owner', registry.profile_ids())
+ try:
+ with db:
+ await authority.submit(ACTOR, Submission(request_id='running', ref=REF, payload={'text': 'a'}, intent='queue'))
+ await asyncio.wait_for(asyncio.to_thread(agent.started.wait, 5), 6)
+ assert await run_runtime._retire_profile_authority(authority, timeout=0.2) is False
+ assert runner.session_runtime_descriptor['served_profiles'] == [
+ {'profile_id': launch.profile_id, 'home': launch.profile_id}]
+ with pytest.raises(PermissionError):
+ runner.session_ticket_store.mint(profile_id='owned', subject='u', purpose='interactive')
+ finally:
+ agent.release.set()
+ await asyncio.sleep(.05)
+ db.close()
diff --git a/tests/gateway/test_runtime_worker_shutdown.py b/tests/gateway/test_runtime_worker_shutdown.py
index 0230fafc956..e9e74781213 100644
--- a/tests/gateway/test_runtime_worker_shutdown.py
+++ b/tests/gateway/test_runtime_worker_shutdown.py
@@ -74,7 +74,8 @@ async def test_profile_timeout_keeps_authority_until_physical_worker_exits(tmp_p
assert stopped.is_set() and not done.is_set()
assert registry.for_home(beta) is authority
assert not unbound
- assert str(beta) in runner.session_ticket_store.profile_ids
+ # Kept for its live writer, but it refuses all work: no longer published or ticketed.
+ assert str(beta) not in runner.session_ticket_store.profile_ids
release.set()
await asyncio.wait_for(task, 5)
await run_runtime.unserve_profile_runtime(runner, beta)