Skip to content

The agent that grows with you

fix(gateway): serving_authorities is the registry's serving set

unsupportedpastels49ed2d580ade
diff --git a/gateway/platforms/webhook_delivery.py b/gateway/platforms/webhook_delivery.py
index 263b086551e..a14fafe975b 100644
--- a/gateway/platforms/webhook_delivery.py
+++ b/gateway/platforms/webhook_delivery.py
@@ -72,13 +72,14 @@ def retained_destination(adapter, chat_id):
"""Readback also checks today's connector and authorization; retained data is not a grant."""
from gateway.session_envelope import restore_native, _validate_native
runner = adapter.gateway_runner or getattr(adapter._message_handler, '__self__', None)
- from gateway.session_authorities import active_authority, all_authorities
+ from gateway.session_authorities import active_authority, all_authorities, serving_authorities
if not all_authorities(runner):
return validate_destination(adapter._delivery_info.get(chat_id))
# The reply leg runs in the routed profile's scope; that ledger holds the destination. A
- # delivery id is unique per route, so an unscoped caller may search every served ledger.
+ # delivery id is unique per route, so an unscoped caller may search every served ledger (a
+ # parked profile's retained store is not served: reading it could fail every other profile's).
scoped = active_authority(runner)
- candidates = [scoped] if scoped is not None else all_authorities(runner)
+ candidates = [scoped] if scoped is not None else serving_authorities(runner)
# Only the signed ingress admission carries the destination; later automation rows
# (process/watch completions) on the same chat share its source but never compete.
query = """SELECT payload_json FROM session_admissions
diff --git a/gateway/session_authorities.py b/gateway/session_authorities.py
index 69976f62499..6d4843d10ee 100644
--- a/gateway/session_authorities.py
+++ b/gateway/session_authorities.py
@@ -193,9 +193,14 @@ def all_authorities(runner):
def serving_authorities(runner):
- """``all_authorities`` minus retiring ones: what startup recovery and service creation cover.
- Shutdown keeps ``all_authorities``, so a retained authority's live writer is still settled."""
- return [a for a in all_authorities(runner) if not getattr(a, 'retiring', False)]
+ """The authorities that accept work (``SessionAuthorities.serving``: no reserved-but-unbuilt slot,
+ none retiring): what startup recovery, service creation and cross-ledger reads cover. Shutdown
+ keeps ``all_authorities``, so a retained authority's live writer is still settled."""
+ registry = getattr(runner, 'session_authorities', None)
+ if registry is not None:
+ return registry.serving()
+ authority = getattr(runner, 'session_authority', None)
+ return [authority] if authority is not None and not getattr(authority, 'retiring', False) else []
def require_serving(authority):
diff --git a/tests/gateway/test_serving_authorities.py b/tests/gateway/test_serving_authorities.py
new file mode 100644
index 00000000000..b29a1bad4fd
--- /dev/null
+++ b/tests/gateway/test_serving_authorities.py
@@ -0,0 +1,52 @@
+"""Readers of every served ledger skip a slot that serves nothing.
+
+``serving_authorities`` is the registry's own ``SessionAuthorities.serving()``: a reserved-but-unbuilt
+(``None``) slot and a parked profile's retained (retiring) authority are not served, so startup
+recovery and a webhook's cross-ledger readback never read them.
+"""
+import sqlite3
+from types import SimpleNamespace
+
+import pytest
+
+from gateway.session_authorities import SessionAuthorities, serving_authorities
+from hermes_state_runtime import RuntimeStoreError
+
+
+class _Ledger:
+ def __init__(self, rows=(), broken=False):
+ self.rows, self.broken = list(rows), broken
+
+ def _read_all(self, *_args):
+ if self.broken:
+ raise sqlite3.OperationalError('disk I/O error')
+ return self.rows
+
+
+def _registry(tmp_path):
+ homes = [tmp_path / name for name in ('root', 'beta', 'gamma')]
+ for home in homes:
+ home.mkdir()
+ launch = SimpleNamespace(profile_id=str(homes[0]), db=_Ledger())
+ parked = SimpleNamespace(profile_id=str(homes[1]), db=_Ledger(broken=True), retiring=True)
+ registry = SessionAuthorities(homes[0], multiplexed=True)
+ registry.add(homes[0], launch)
+ registry.add(homes[1], parked, name='beta')
+ registry.add(homes[2], None, name='gamma') # reserved, not built yet
+ return registry, launch
+
+
+def test_serving_authorities_skips_unbuilt_and_retiring_slots(tmp_path):
+ registry, launch = _registry(tmp_path)
+ assert serving_authorities(SimpleNamespace(session_authorities=registry)) == [launch]
+
+
+def test_a_webhook_readback_never_reads_a_parked_store(tmp_path, monkeypatch):
+ from gateway.platforms.webhook_delivery import retained_destination
+ registry, _launch = _registry(tmp_path)
+ runner = SimpleNamespace(session_authorities=registry)
+ # A reply leg scoped to a profile this process does not serve searches every served ledger.
+ monkeypatch.setattr('gateway.session_authorities.active_authority', lambda _runner: None)
+ adapter = SimpleNamespace(gateway_runner=runner, _message_handler=None, _delivery_info={})
+ with pytest.raises(RuntimeStoreError, match='not_found'):
+ retained_destination(adapter, 'chat-1')