Skip to content

The agent that grows with you

fix: expose authenticated group metadata and profile discovery

Teknium3e896bc3909a
diff --git a/gateway/session_group_controls.py b/gateway/session_group_controls.py
new file mode 100644
index 00000000000..f45926b4dab
--- /dev/null
+++ b/gateway/session_group_controls.py
@@ -0,0 +1,172 @@
+"""Profile-owned non-inference room controls and roster discovery.
+
+The legacy local room driver creates TUI agents, so it is not an execution
+capability of this transport. Room metadata still uses the existing durable
+room protocol, on the authority's database rather than the install-wide default.
+"""
+import asyncio
+from pathlib import Path
+
+from hermes_state_runtime import RuntimeStoreError
+
+
+GROUP_METHODS = {
+ 'groups.capabilities': 'session:read',
+ 'groups.list': 'session:read',
+ 'groups.state': 'session:read',
+ 'groups.log': 'session:read',
+ 'groups.create': 'session:control',
+ 'groups.rename': 'session:control',
+ 'groups.disband': 'session:control',
+}
+_FIELDS = {
+ 'groups.capabilities': set(),
+ 'groups.list': {'limit', 'offset', 'include_disbanded'},
+ 'groups.state': {'room_id', 'include_disbanded'},
+ 'groups.log': {'room_id', 'since_seq', 'limit', 'include_disbanded'},
+ 'groups.create': {'room_id', 'name', 'members'},
+ 'groups.rename': {'room_id', 'event_id', 'name'},
+ 'groups.disband': {'room_id'},
+ 'profiles.list': {'include_sessions'},
+}
+
+
+async def dispatch_group_control(connection, method, params):
+ authority, actor = connection.authority, connection.actor
+ capability = GROUP_METHODS.get(method, 'session:read')
+ if capability not in actor.capabilities:
+ raise RuntimeStoreError('permission_denied')
+ if actor.profile_id != authority.profile_id:
+ raise RuntimeStoreError('profile_mismatch')
+ if method not in _FIELDS or not isinstance(params, dict) or set(params) - (_FIELDS[method] | {'profile'}):
+ raise RuntimeStoreError('invalid_params')
+ home = Path(authority.profile_id)
+ if Path(authority.db.db_path).resolve().parent != home.resolve():
+ raise RuntimeStoreError('profile_mismatch')
+ from hermes_cli.profiles import profile_matches_home
+ profile = params.get('profile')
+ if profile is not None and not isinstance(profile, str):
+ raise RuntimeStoreError('invalid_params')
+ if profile and not profile_matches_home(profile, home):
+ raise RuntimeStoreError('profile_mismatch')
+ supplied = {key: value for key, value in params.items() if key != 'profile'}
+
+ def invoke():
+ from gateway.run import _profile_runtime_scope
+ from gateway.hosted_rooms import HostedRoomError
+ with _profile_runtime_scope(home):
+ if method == 'profiles.list':
+ return _profiles(authority, actor, home, supplied)
+ try:
+ return _group(authority, home, method, supplied)
+ except HostedRoomError as exc:
+ raise RuntimeStoreError(getattr(exc, 'reason', None) or 'invalid_params') from exc
+ except (ValueError, TypeError) as exc:
+ raise RuntimeStoreError('invalid_params') from exc
+ return await asyncio.to_thread(invoke)
+
+
+def _group(authority, home, method, params):
+ from gateway import hosted_rooms as rooms
+ db_path = authority.db.db_path
+ gateway_id = rooms.local_authority_gateway_id()
+
+ def capabilities():
+ return {'protocol_version': rooms.PROTOCOL_VERSION, 'driver': False,
+ 'persistent_process': True, 'authority_gateway_id': gateway_id,
+ 'room_link': {'enabled': False, 'reason': 'canonical_driver_required'},
+ 'features': ['room_identity', 'monotonic_log', 'replayable_disband'],
+ 'methods': list(GROUP_METHODS), 'max_log_limit': rooms.MAX_LOG_LIMIT}
+
+ def listing():
+ limit, offset = params.get('limit', rooms.MAX_ROOM_LIST_LIMIT), params.get('offset', 0)
+ result = rooms.list_rooms(db_path, **params)
+ return {'rooms': result, 'next_offset': offset + limit if len(result) == limit else None}
+
+ def create():
+ from gateway.hosted_room_discussion import validate_roster
+ name = home.name if home.parent.name == 'profiles' else 'default'
+ profiles = {name}
+ if name == 'default' and (home / 'profiles').is_dir():
+ profiles.update(path.name for path in (home / 'profiles').iterdir() if path.is_dir())
+ members = validate_roster(params.get('members'), local_profiles=profiles)
+ normalized = [{'member_id': m.member_id, 'profile': m.profile, 'handle': m.handle,
+ 'target': dict(m.target or {}),
+ **({'display_name': m.display_name} if m.display_name else {})} for m in members]
+ return {'room': rooms.create_room(db_path, room_id=params.get('room_id'),
+ name=params.get('name'), members=normalized, authority_gateway_id=gateway_id)}
+
+ def disband():
+ from gateway.hosted_room_driver import list_tasks
+ # Metadata control must not destroy an active execution or bypass Stop.
+ if any(list_tasks(db_path, room_id=params.get('room_id'), status=status)
+ for status in ('queued', 'running', 'stopping', 'indeterminate', 'deferred')):
+ raise RuntimeStoreError('runtime_coordination_required')
+ state = rooms.room_state(db_path, room_id=params.get('room_id'), include_disbanded=True)
+ return {'tombstone': rooms.disband_room(db_path, room_id=params.get('room_id'),
+ expected_gateway_id=gateway_id, expected_epoch=state['authority_epoch'])}
+
+ handlers = {
+ 'groups.capabilities': capabilities,
+ 'groups.list': listing,
+ 'groups.create': create,
+ 'groups.state': lambda: {'room': rooms.room_state(db_path, **params)},
+ 'groups.log': lambda: rooms.read_events(db_path, **params),
+ 'groups.rename': lambda: {'room': rooms.rename_room(db_path, **params)},
+ 'groups.disband': disband,
+ }
+ return handlers[method]()
+
+
+def _profiles(authority, actor, home, params):
+ from hermes_cli.profiles import _profile_info, read_profile_meta
+ import yaml
+ include_sessions = params.get('include_sessions', True)
+ if type(include_sessions) is not bool:
+ raise RuntimeStoreError('invalid_params')
+ name = home.name if home.parent.name == 'profiles' else 'default'
+ profile = _profile_info(name, home, is_default=name == 'default')
+ row = {'name': name, 'path': str(home), 'is_default': profile.is_default,
+ 'model': profile.model, 'provider': profile.provider,
+ 'description': profile.description or '', 'display_name': profile.display_name or '',
+ 'skill_count': profile.skill_count or 0}
+ path = home / 'profile.yaml'
+ meta = yaml.safe_load(path.read_text(encoding='utf-8')) if path.is_file() else {}
+ meta = meta if isinstance(meta, dict) else {}
+ revisions = meta.get('_ui_meta_revisions')
+ row['ui_meta_revisions'] = {str(k): max(0, v) for k, v in revisions.items()
+ if type(v) is int} if isinstance(revisions, dict) else {}
+ if isinstance(meta.get('ui_meta'), dict):
+ row['ui_meta'] = meta['ui_meta']
+ row['has_avatar'] = any((home / 'assets' / f'avatar.{ext}').is_file() for ext in ('png', 'jpg', 'webp'))
+ if include_sessions:
+ row.update(last_session=None, worker_session=None, canonical_session=None)
+ # Discovery is read-only: no legacy restore/unarchive or new SessionDB handle.
+ def summary(session):
+ tip = authority.db.get_compression_tip(session['id']) or session['id']
+ target = authority.db.get_session(tip) or session
+ with authority.db._lock:
+ message = authority.db._conn.execute(
+ "SELECT content FROM messages WHERE session_id=? AND active=1 "
+ "AND role IN ('user','assistant') AND TRIM(COALESCE(content,''))!='' "
+ "ORDER BY id DESC LIMIT 1", (tip,)).fetchone()
+ text = ' '.join(str(message[0] or '').split()) if message else ''
+ return {'id': session['id'], 'resolved_id': tip, 'title': target.get('title') or '',
+ 'root_title': session.get('title') or '',
+ 'preview': text[:80] + '...' if len(text) > 80 else text,
+ 'started_at': target.get('started_at') or 0,
+ 'last_active': target.get('last_activity_at') or target.get('started_at') or 0,
+ 'message_count': target.get('message_count') or 0}
+
+ # The named registry is not a recency window and canonical chats are hidden.
+ canonical = authority.db.get_session_by_title('Bot Chat')
+ if (canonical and canonical.get('user_id') == actor.subject
+ and str(canonical.get('chat_id') or '').startswith('local-') and not canonical.get('archived')):
+ row['canonical_session'] = summary(canonical)
+ with authority.db._lock:
+ latest = authority.db._conn.execute(
+ "SELECT id FROM sessions WHERE user_id=? AND chat_id LIKE 'local-%' AND archived=0 "
+ "ORDER BY COALESCE(last_activity_at,started_at) DESC LIMIT 1", (actor.subject,)).fetchone()
+ if latest:
+ row['last_session'] = summary(authority.db.get_session(latest[0]))
+ return {'profiles': [row], 'bot_mode_protocol': True}
diff --git a/tests/gateway/test_session_group_controls.py b/tests/gateway/test_session_group_controls.py
new file mode 100644
index 00000000000..ec7aeb32260
--- /dev/null
+++ b/tests/gateway/test_session_group_controls.py
@@ -0,0 +1,110 @@
+"""Canonical group management is profile-bound and never starts inference."""
+import asyncio
+import json
+from pathlib import Path
+from types import SimpleNamespace
+
+import pytest
+from websockets.asyncio.client import connect
+from websockets.exceptions import InvalidStatus
+
+from gateway.session_controls import AuthorityConnection
+from hermes_state import SessionDB
+from tests.gateway.fixtures.local_recovery_probe import child_env, daemon, rpc, websocket
+
+
+def test_group_management_on_ordinary_authenticated_socket(tmp_path):
+ root = Path(__file__).resolve().parents[2]
+ home, user = tmp_path / 'state', tmp_path / 'user'
+ home.mkdir(mode=0o700)
+ user.mkdir()
+ (home / 'profiles' / 'helper').mkdir(parents=True)
+ (home / 'config.yaml').write_text(json.dumps({
+ 'gateway': {'multiplex_profiles': False},
+ 'model': {'provider': 'custom', 'default': 'no-inference',
+ 'base_url': 'http://127.0.0.1:1/v1'},
+ 'auxiliary': {'title_generation': {'enabled': False}},
+ }))
+ env = child_env() | dict(HOME=str(user), USERPROFILE=str(user), HERMES_HOME=str(home),
+ PYTHONPATH=str(root), PYTHONUNBUFFERED='1')
+
+ async def probe(desc):
+ with pytest.raises(InvalidStatus):
+ async with connect(desc['api_origin'].replace('http:', 'ws:') + '/api/ws'):
+ pass
+ async with websocket(home, desc) as ws:
+ capabilities = await rpc(ws, 'groups.capabilities')
+ assert 'result' in capabilities, capabilities
+ assert 'groups.create' in capabilities['result']['methods']
+ members = [{'member_id': 'one', 'profile': 'default', 'handle': 'one'},
+ {'member_id': 'two', 'profile': 'helper', 'handle': 'two'}]
+ created = await rpc(ws, 'groups.create', room_id='room-fixture', name='First', members=members)
+ assert 'result' in created, created
+ room = created['result']['room']
+ retry = (await rpc(ws, 'groups.create', room_id='room-fixture', name='First', members=members))['result']['room']
+ assert retry['room_id'] == room['room_id'] and retry['idempotent']
+ renamed = await rpc(ws, 'groups.rename', room_id='room-fixture', event_id='rename-fixture', name='Renamed')
+ assert renamed['result']['room']['name'] == 'Renamed', renamed
+ listed = await rpc(ws, 'groups.list')
+ assert [r['name'] for r in listed['result']['rooms']] == ['Renamed'], listed
+ state = await rpc(ws, 'groups.state', room_id='room-fixture')
+ log = await rpc(ws, 'groups.log', room_id='room-fixture')
+ assert state['result']['room']['latest_seq'] == log['result']['cursor']
+ denied = await rpc(ws, 'groups.rename', profile='foreign', room_id='room-fixture', event_id='forbidden', name='Wrong')
+ assert denied['error']['message'] == 'profile_mismatch', denied
+ profiles = await rpc(ws, 'profiles.list', include_sessions=False)
+ assert [p['path'] for p in profiles['result']['profiles']] == [str(home)], profiles
+ disbanded = await rpc(ws, 'groups.disband', room_id='room-fixture')
+ assert 'result' in disbanded, disbanded
+ assert (await rpc(ws, 'groups.list'))['result']['rooms'] == []
+ assert (await rpc(ws, 'session.list'))['result']['sessions'] == []
+ print(json.dumps({'group_management': 'passed', 'profiles': profiles['result'], 'capabilities': capabilities['result']}))
+
+ with daemon(root, home, env, barrier=False) as (_, desc):
+ asyncio.run(probe(desc))
+
+
+def test_profile_discovery_and_group_controls_enforce_actor_scope(tmp_path, monkeypatch):
+ home = tmp_path / 'state'
+ home.mkdir()
+ monkeypatch.setenv('HERMES_HOME', str(home))
+ monkeypatch.setenv('HOME', str(tmp_path / 'user'))
+ (home / 'config.yaml').write_text('model:\n default: fixture-model\n provider: custom\n')
+ (home / 'profile.yaml').write_text('display_name: Fixture\nui_meta:\n hermes-bots:\n color: blue\n')
+ with SessionDB(home / 'state.db') as db:
+ db.create_session('private', source='cli', user_id='another')
+ db.set_session_title('private', 'Bot Chat')
+ authority = SimpleNamespace(profile_id=str(home), instance_id='owner', db=db, events={}, sessions={})
+
+ async def probe():
+ async def call(method, params=None, **identity):
+ connection = AuthorityConnection(authority, object(), {'user_id': 'reader', **identity})
+ return await connection.dispatch({'id': 'request', 'method': method, 'params': params or {}})
+ for method in ('profiles.list', 'groups.list', 'groups.create'):
+ for identity in ({'user_id': ''}, {'capabilities': []}, {'profile_id': 'foreign'}):
+ result = await call(method, **identity)
+ assert result['error']['message'] in {'permission_denied', 'profile_mismatch'}, result
+ denied = await call('groups.create', capabilities=['session:read'])
+ assert denied['error']['message'] == 'permission_denied', denied
+ result = await call('profiles.list', capabilities=['session:read'])
+ assert 'result' in result, result
+ row, = result['result']['profiles']
+ assert row['model'] == 'fixture-model'
+ assert row['display_name'] == 'Fixture'
+ assert row['ui_meta']['hermes-bots']['color'] == 'blue'
+ assert row['canonical_session'] is None
+ assert row['last_session'] is None
+ assert db.get_session('private')['title'] == 'Bot Chat'
+ db.set_session_title('private', 'Other Bot Chat')
+ db.create_session('mine', source='gui', user_id='reader', chat_id='local-mine')
+ db.set_session_title('mine', 'Bot Chat')
+ db.set_session_hidden('mine', True)
+ db.append_message('mine', 'user', 'owned preview')
+ own = (await call('profiles.list', capabilities=['session:read']))['result']['profiles'][0]
+ assert own['canonical_session']['id'] == 'mine'
+ assert own['canonical_session']['preview'] == 'owned preview'
+ for method in ('profiles.list', 'groups.list'):
+ denied = await call(method, {'profile': 'foreign'})
+ assert denied['error']['message'] == 'profile_mismatch', denied
+ assert (await call('profiles.delete'))['error']['message'] == 'invalid_params'
+ asyncio.run(probe())
diff --git a/website/docs/developer-guide/gateway-internals.md b/website/docs/developer-guide/gateway-internals.md
index 9af71c076bc..22a1ebe074e 100644
--- a/website/docs/developer-guide/gateway-internals.md
+++ b/website/docs/developer-guide/gateway-internals.md
@@ -8,6 +8,28 @@ description: "How the messaging gateway boots, authorizes users, routes sessions
The messaging gateway is the long-running process that connects Hermes to 20+ external messaging platforms through a unified architecture.
+## Canonical group metadata and profile discovery
+
+The authenticated canonical WebSocket exposes `groups.capabilities`, `groups.list`,
+`groups.state`, and `groups.log` with `session:read`; `groups.create`,
+`groups.rename`, and `groups.disband` require `session:control`. These reuse the
+hosted-room metadata protocol in the owning authority's database. An explicit
+`profile` selector must resolve to that authority's home. Disband refuses rooms
+with unsettled driver work rather than bypassing Stop.
+
+This is **metadata management, not hosted-room inference**. The existing local
+hosted-room driver still uses legacy TUI session handlers; the canonical surface
+therefore reports `driver: false` and does not advertise send, retry, approval,
+or peer-grant operations. It never imports that driver to execute a group turn.
+
+`profiles.list` requires `session:read` and returns only the authenticated
+profile, including its display metadata and avatar presence. `include_sessions`
+defaults to true: previews are limited to that actor's canonical local bindings,
+and `canonical_session` resolves the exact hidden `Bot Chat` title, not the most
+recent conversation. Discovery does not create, restore, or unarchive sessions,
+open sibling profile databases, or run inference. Cross-profile discovery requires
+a separately authenticated connection to the relevant owner.
+
## Shared authority pending-input snapshots
`session.resume` returns each pending admission with its original `input_id` and