Skip to content

The agent that grows with you

refactor(gateway): one local_bots() answer for the Bots roster and group seating

Tekniume6a659557d73
diff --git a/gateway/session_authorities.py b/gateway/session_authorities.py
index d858c860afe..54e713ef48a 100644
--- a/gateway/session_authorities.py
+++ b/gateway/session_authorities.py
@@ -11,7 +11,9 @@ scoped or not, exactly as before.
"""
from __future__ import annotations
+from dataclasses import dataclass
from pathlib import Path
+from typing import Any
from hermes_constants import get_hermes_home, get_hermes_home_override, hermes_home_key
from hermes_state_runtime import RuntimeStoreError
@@ -101,14 +103,6 @@ class SessionAuthorities:
# A snapshot: hosted-room coordinator threads read the served set while the loop hot-serves.
return [a for a in list(self._by_key.values()) if a is not None and not getattr(a, 'retiring', False)]
- def named_serving(self) -> list[tuple[str, object]]:
- """``(name, authority)`` for each of ``serving()``. A secondary goes by the name it was
- reserved under: re-deriving it from the resolved home misnames a ``profiles/<name>`` entry
- that is a link (it resolves outside ``profiles/`` and reads as ``default``), and a group
- must refuse such a profile by its own name rather than bind ``default`` to its home. The
- launch profile keeps ``served_profile_name``, the label its sessions and clients use."""
- return [(self.profile_name(a) or served_profile_name(a.profile_id), a) for a in self.serving()]
-
def served_profiles(self) -> list[dict]:
return [{'profile_id': a.profile_id, 'home': a.profile_id} for a in self.serving()]
@@ -116,6 +110,47 @@ class SessionAuthorities:
return frozenset(a.profile_id for a in self.serving())
+@dataclass(frozen=True)
+class LocalBot:
+ """One local bot this process serves: its profile ``name``, ``home`` and owning ``authority``.
+ ``problem`` says why a client cannot address it by that name (None when it can); ``retiring``
+ marks one being unserved, which still owns its rooms but takes no new work."""
+ name: str
+ home: Path
+ authority: Any
+ problem: str | None = None
+ retiring: bool = False
+
+
+def local_bots(runner) -> list[LocalBot]:
+ """The local bots this gateway serves, one per built authority, by the name the registry
+ reserved each under (the launch profile keeps ``served_profile_name``). The one answer to
+ "which bots are local here" for both the Bots roster (``profiles.list``) and group seating
+ (``session_hosted_members``). A name two homes answer to, or one that is not its home's own
+ name (a linked ``profiles/<name>`` resolves outside ``profiles/`` and reads as ``default``), is
+ flagged rather than bound: a client routes, and the member transport names, a bot by name."""
+ from collections import Counter
+ registry = getattr(runner, 'session_authorities', None)
+ if registry is None:
+ authority = getattr(runner, 'session_authority', None)
+ pairs = [(served_profile_name(authority.profile_id), authority)] if authority is not None else []
+ else:
+ # A snapshot: hosted-room coordinator threads read the served set while the loop hot-serves.
+ pairs = [(registry.profile_name(a) or served_profile_name(a.profile_id), a)
+ for a in list(registry._by_key.values()) if a is not None]
+ counts = Counter(name for name, _ in pairs)
+ bots = []
+ for name, authority in pairs:
+ problem = None
+ if counts[name] > 1:
+ problem = 'two served homes answer to that name'
+ elif name != served_profile_name(authority.profile_id):
+ problem = 'a linked profile home is not supported yet'
+ bots.append(LocalBot(name, Path(authority.profile_id), authority, problem,
+ bool(getattr(authority, 'retiring', False))))
+ return bots
+
+
def served_profile_name(home) -> str:
"""Canonical profile id of a served home: ``default`` for the root, the directory name for
``profiles/<name>`` (via ``hermes_constants.profile_name_for_home``, the resolver main uses).
diff --git a/gateway/session_group_controls.py b/gateway/session_group_controls.py
index 2a1c83db7e5..3cbe9f90944 100644
--- a/gateway/session_group_controls.py
+++ b/gateway/session_group_controls.py
@@ -277,21 +277,23 @@ _DEGRADED = weakref.WeakSet()
def _roster_siblings(connection, params):
- """The other profiles this process serves, as ``[(authority, reader)]``, snapshotted on the owner
- loop, where serve and unserve mutate the registry; an unbuilt slot is skipped. ``reader`` is the
- principal that row's session fields are read as, or None for a metadata-only row: only a
- host-scoped socket routes session verbs to a sibling (``AuthorityConnection._sibling_for``). It
- carries this socket's subject and capabilities, which is what the routed connection would carry;
- none is built, so no poll (routed or not) adds connections."""
+ """The other local bots this process serves (``session_authorities.local_bots``, the set group
+ seating uses too), as ``[(bot, reader)]``, snapshotted on the owner loop, where serve and
+ unserve mutate the registry; an unbuilt slot is skipped. ``reader`` is the principal that row's
+ session fields are read as, or None for a metadata-only row: only a host-scoped socket routes
+ session verbs to a sibling (``AuthorityConnection._sibling_for``), and only a bot addressable
+ by its own name (no ``problem``) is the one a click on its row opens. It carries this socket's
+ subject and capabilities, which is what the routed connection would carry; none is built, so
+ no poll (routed or not) adds connections."""
+ from gateway.session_authorities import local_bots
from gateway.session_contract import Principal
authority, actor = connection.authority, connection.actor
- registry = getattr(getattr(authority, 'runner', None), 'session_authorities', None)
- if registry is None:
+ if getattr(getattr(authority, 'runner', None), 'session_authorities', None) is None:
return []
readable = connection.host_scoped and params.get('include_sessions', True) is True
- return [(sibling, Principal(actor.subject, sibling.profile_id, actor.capabilities, actor.transport_id)
- if readable else None)
- for sibling in list(registry) if sibling is not None and sibling is not authority]
+ return [(bot, Principal(actor.subject, bot.authority.profile_id, actor.capabilities, actor.transport_id)
+ if readable and bot.problem is None else None)
+ for bot in local_bots(authority.runner) if bot.authority is not authority]
def _bot_sessions(authority, actor):
@@ -370,7 +372,7 @@ def _sibling_sessions(authority, reader):
def _profiles(authority, actor, home, params, siblings=()):
"""The Bots roster: this profile's row, then every profile it can name. ``siblings`` is
``_roster_siblings``' snapshot; a sibling with a reader carries the same session fields."""
- from hermes_cli.profiles import _profile_info, profile_matches_home
+ from hermes_cli.profiles import _profile_info
import hermes_yaml as yaml
include_sessions = params.get('include_sessions', True)
if type(include_sessions) is not bool:
@@ -420,13 +422,12 @@ def _profiles(authority, actor, home, params, siblings=()):
# Sibling profiles this process multiplexes: the roster lists them with the same
# standing as a hosted-room destination, and the client routes their sessions by name.
- for sibling, reader in siblings:
- configured, target = served_profile_name(Path(sibling.profile_id)), Path(sibling.profile_id)
- # A row's fields come from the authority a click on that row opens: the client routes by
- # this name. A linked ``profiles/<name>`` resolves outside ``profiles/`` and reads as
- # ``default`` here, which routes to the launch profile, so its row stays metadata-only.
- reach = (sibling, reader) if reader is not None and profile_matches_home(configured, target) else None
- discovered(configured, target, reach)
+ for bot, reader in siblings:
+ # The row keeps the name its home resolves to (the client routes by it); a bot that name
+ # does not reach (a linked ``profiles/<name>`` reads as ``default``) has no reader, so its
+ # row stays metadata-only.
+ discovered(served_profile_name(bot.home), bot.home,
+ (bot.authority, reader) if reader is not None else None)
service = getattr(authority, 'hosted_room_service', None)
if service is not None:
for configured, target in service.profile_homes().items():
diff --git a/gateway/session_hosted_members.py b/gateway/session_hosted_members.py
index c7fdfb49424..bf4a38a6281 100644
--- a/gateway/session_hosted_members.py
+++ b/gateway/session_hosted_members.py
@@ -7,7 +7,6 @@ profile-bound caller seats its own profile plus the profiles its entries name, a
answers to one name. An entry that would break a rule is ignored and logged once, and only a new
member naming it is refused, so one bad entry never takes the rest of the roster down.
"""
-from collections import Counter
import logging
from pathlib import Path
@@ -77,27 +76,25 @@ def refuse_unseatable(members, refused):
def _served(authority):
- """``(own name, name -> home, refused)`` over the profiles this process serves, by the names
- the registry reserved them under. A name two homes answer to is refused, never bound."""
- from gateway.session_authorities import served_profile_name
- registry = getattr(getattr(authority, 'runner', None), 'session_authorities', None)
- pairs = registry.named_serving() if registry is not None else []
- own = next((name for name, served in pairs if served is authority), None)
+ """``(own name, name -> home, refused)`` over the local bots this process serves
+ (``session_authorities.local_bots``). A bot its clients cannot address by name is refused,
+ never bound; a retiring one is not seated in a new group."""
+ from gateway.session_authorities import local_bots, served_profile_name
+ bots = local_bots(getattr(authority, 'runner', None))
+ own = next((bot.name for bot in bots if bot.authority is authority), None)
if own is None:
- # A bare authority (no registry), or a retiring one: it still owns its own rooms.
+ # A bare authority (no registry): it still owns its own rooms.
own = served_profile_name(authority.profile_id)
- pairs = [(own, authority), *pairs]
- counts = Counter(name for name, _ in pairs)
served, refused = {}, {}
- for name, served_authority in pairs:
- if counts[name] > 1:
- refused[name] = ('invalid_params', 'members', 'two served homes answer to that name')
- elif name != served_profile_name(served_authority.profile_id):
- # A linked profiles/<name> resolves outside profiles/; the member transport names a
- # destination by its resolved home, so every turn would fail on the name.
- refused[name] = ('invalid_params', 'members', 'a linked profile home is not supported in groups yet')
+ for bot in bots:
+ if bot.problem is not None:
+ refused[bot.name] = ('invalid_params', 'members', bot.problem.replace(' yet', ' in groups yet'))
+ elif bot.retiring and bot.authority is not authority:
+ continue
else:
- served[name] = Path(served_authority.profile_id)
+ served[bot.name] = bot.home
+ if own not in refused:
+ served.setdefault(own, Path(authority.profile_id))
return own, served, refused
diff --git a/tests/gateway/test_bot_roster_fleet.py b/tests/gateway/test_bot_roster_fleet.py
index 90d606afec5..474ab3213e9 100644
--- a/tests/gateway/test_bot_roster_fleet.py
+++ b/tests/gateway/test_bot_roster_fleet.py
@@ -207,3 +207,21 @@ async def test_a_row_whose_name_routes_elsewhere_carries_no_bot_chat(fleet):
row, = [row for row in result['profiles'] if row['path'] == str(fleet.homes['delta'])]
assert row['name'] == 'default'
assert not {'canonical_session', 'last_session'} & set(row), row
+
+
[email protected]('fleet', [('delta',)], indirect=True)
+async def test_the_roster_and_group_seating_read_one_set_of_local_bots(fleet):
+ """RT-24 and HR-3 ask the same question, "which bots are local here": both read
+ ``session_authorities.local_bots``. The roster reads a bot's chats only where group seating
+ would seat it by name; a linked home is flagged for both."""
+ from gateway.session_authorities import authority_for_home, local_bots
+ from gateway.session_hosted_members import member_roster
+ bots = {bot.name: bot for bot in local_bots(fleet.runner)}
+ assert set(bots) == {'default', 'alpha', 'beta', 'gamma', 'delta'}
+ assert bots['delta'].problem and not any(bots[n].problem for n in ('default', 'alpha', 'beta', 'gamma'))
+ homes, refused = member_roster(authority_for_home(fleet.runner, fleet.homes['default']))
+ assert set(homes) == {name for name, bot in bots.items() if bot.problem is None}
+ assert set(refused) == {'delta'}
+ rows = (await fleet.call(fleet.connect('default', scope='host'), 'profiles.list'))['profiles']
+ assert {row['path'] for row in rows} >= {str(bot.home) for bot in bots.values()}