session_group_controls.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. """Profile-owned room controls over the canonical hosted service."""
  2. import asyncio
  3. import logging
  4. import os
  5. from pathlib import Path
  6. import uuid
  7. import weakref
  8. from hermes_state_runtime import RuntimeStoreError
  9. logger = logging.getLogger(__name__)
  10. GROUP_METHODS = {
  11. 'groups.capabilities': 'session:read',
  12. 'groups.list': 'session:read',
  13. 'groups.state': 'session:read',
  14. 'groups.log': 'session:read',
  15. 'groups.create': 'session:control',
  16. 'groups.rename': 'session:control',
  17. 'groups.context': 'session:control',
  18. 'groups.disband': 'session:control',
  19. 'groups.send': 'session:submit',
  20. 'groups.attachment.upload': 'session:submit',
  21. 'groups.attachment.download': 'session:read',
  22. 'groups.stop': 'session:control',
  23. 'groups.continue': 'session:control',
  24. 'groups.retry': 'session:control',
  25. 'groups.discard': 'session:control',
  26. 'groups.approve': 'session:approve',
  27. }
  28. _FIELDS = {
  29. 'groups.capabilities': set(),
  30. 'groups.list': {'limit', 'offset', 'include_disbanded'},
  31. 'groups.state': {'room_id', 'include_disbanded'},
  32. 'groups.log': {'room_id', 'since_seq', 'limit', 'include_disbanded'},
  33. 'groups.create': {'room_id', 'name', 'members'},
  34. 'groups.rename': {'room_id', 'event_id', 'name'},
  35. 'groups.context': {'room_id', 'event_id', 'purpose', 'notes', 'routing', 'lead_member_id'},
  36. 'groups.disband': {'room_id', 'cancel_id'},
  37. 'groups.send': {'room_id', 'event_id', 'payload'},
  38. 'groups.attachment.upload': {'room_id', 'upload_id', 'kind', 'name', 'mime', 'data_base64'},
  39. 'groups.attachment.download': {'room_id', 'event_id', 'attachment_id'},
  40. 'groups.stop': {'room_id', 'cancel_id'},
  41. 'groups.continue': {'room_id', 'discussion_event_id'},
  42. 'groups.retry': {'room_id', 'member_id', 'task_id', 'execution_generation'},
  43. 'groups.discard': {'room_id', 'member_id', 'task_id', 'execution_generation'},
  44. 'groups.approve': {'room_id', 'member_id', 'task_id', 'execution_generation',
  45. 'choice', 'request_id'},
  46. 'profiles.list': {'include_sessions'},
  47. }
  48. async def dispatch_group_control(connection, method, params):
  49. authority, actor = connection.authority, connection.actor
  50. capability = GROUP_METHODS.get(method, 'session:read')
  51. if capability not in actor.capabilities:
  52. raise RuntimeStoreError('permission_denied')
  53. if actor.profile_id != authority.profile_id:
  54. raise RuntimeStoreError('profile_mismatch')
  55. if method not in _FIELDS or not isinstance(params, dict) or set(params) - (_FIELDS[method] | {'profile'}):
  56. raise RuntimeStoreError('invalid_params')
  57. home = Path(authority.profile_id)
  58. db_path = await asyncio.to_thread(Path(authority.db.db_path).resolve)
  59. if db_path.parent != await asyncio.to_thread(home.resolve):
  60. raise RuntimeStoreError('profile_mismatch')
  61. from hermes_cli.profiles import profile_matches_home
  62. profile = params.get('profile')
  63. if profile is not None and not isinstance(profile, str):
  64. raise RuntimeStoreError('invalid_params')
  65. if profile and not profile_matches_home(profile, home):
  66. raise RuntimeStoreError('profile_mismatch')
  67. supplied = {key: value for key, value in params.items() if key != 'profile'}
  68. siblings = _roster_siblings(connection, supplied) if method == 'profiles.list' else ()
  69. def invoke():
  70. from gateway.run import _profile_runtime_scope
  71. from gateway.hosted_rooms import HostedRoomError
  72. with _profile_runtime_scope(home):
  73. if method == 'profiles.list':
  74. return _profiles(authority, actor, home, supplied, siblings)
  75. try:
  76. return _group(authority, actor, method, supplied, host_scope=connection.host_scoped)
  77. except RuntimeStoreError:
  78. raise
  79. except HostedRoomError as exc:
  80. raise _refusal(getattr(exc, 'reason', None) or 'invalid_params',
  81. admitted=getattr(exc, 'admitted', None)) from exc
  82. except (ValueError, TypeError) as exc:
  83. raise _refusal('invalid_params', admitted=getattr(exc, 'admitted', None)) from exc
  84. return await asyncio.to_thread(invoke)
  85. def _refusal(reason, *, admitted=None):
  86. """The wire refusal; ``admitted=False`` (a groups.send refused before its user event
  87. committed: ``HostedRoomService.send``, or no driver to take it) rides along as error data."""
  88. refusal = RuntimeStoreError(reason)
  89. if admitted is False:
  90. refusal.admitted = False
  91. return refusal
  92. def _group(authority, actor, method, params, *, host_scope=False):
  93. from gateway import hosted_rooms as rooms
  94. from gateway.hosted_room_context import room_context, set_room_context
  95. db_path = authority.db.db_path
  96. gateway_id = rooms.local_authority_gateway_id()
  97. service = getattr(authority, 'hosted_room_service', None)
  98. room_authorizer = getattr(service, 'authorize_room', None)
  99. if service is not None:
  100. if Path(service.db_path).resolve() != Path(db_path).resolve():
  101. raise RuntimeStoreError('profile_mismatch')
  102. status = service.runtime.status()
  103. if not status.get('running') or status.get('stopping'):
  104. service = None
  105. execution_methods = {'groups.send', 'groups.stop', 'groups.continue', 'groups.retry', 'groups.discard',
  106. 'groups.approve'}
  107. admit = None
  108. if getattr(authority, 'hosted_room_service', None) is not None and 'room_id' in params:
  109. if room_authorizer is None:
  110. raise RuntimeStoreError('permission_denied')
  111. if method == 'groups.create':
  112. # Claim ownership inside the room insert's transaction: a create rejected by
  113. # validation or storage must not leave an owner-only reservation behind.
  114. def admit(conn):
  115. room_authorizer(actor.subject, params['room_id'], create=True, conn=conn)
  116. else:
  117. room_authorizer(actor.subject, params['room_id'])
  118. if method in {'groups.attachment.upload', 'groups.attachment.download'}:
  119. if service is None:
  120. raise RuntimeStoreError('runtime_coordination_required')
  121. from gateway.session_hosted_attachments import upload, download
  122. handler = upload if method == 'groups.attachment.upload' else download
  123. return handler(service, actor, params)
  124. if method in execution_methods:
  125. if service is None:
  126. if method == 'groups.send':
  127. raise _refusal('runtime_coordination_required', admitted=False)
  128. raise RuntimeStoreError('runtime_coordination_required')
  129. if not params.get('room_id'):
  130. raise RuntimeStoreError('invalid_params')
  131. return _execution_control(service, method, params)
  132. def capabilities():
  133. from gateway.session_hosted_members import member_roster
  134. # The roster groups.create will accept from THIS caller, so a picker never offers a bot
  135. # (parked, retiring, a sibling a profile-bound socket cannot seat) that create refuses.
  136. homes, refused = member_roster(authority, siblings=host_scope)
  137. return {'protocol_version': rooms.PROTOCOL_VERSION, 'driver': service is not None,
  138. 'persistent_process': True, 'authority_gateway_id': gateway_id,
  139. 'room_link': {'enabled': False, 'reason': 'canonical_driver_required'},
  140. 'features': ['room_identity', 'monotonic_log', 'replayable_disband'],
  141. 'methods': list(GROUP_METHODS), 'max_log_limit': rooms.MAX_LOG_LIMIT,
  142. 'seatable_profiles': sorted(name for name in homes if name not in refused)}
  143. def listing():
  144. limit, offset = params.get('limit', rooms.MAX_ROOM_LIST_LIMIT), params.get('offset', 0)
  145. if getattr(authority, 'hosted_room_service', None) is None:
  146. result = rooms.list_rooms(db_path, **params)
  147. return {'rooms': result, 'next_offset': offset + limit if len(result) == limit else None}
  148. authorize = room_authorizer
  149. if authorize is None:
  150. raise RuntimeStoreError('permission_denied')
  151. def visible(room):
  152. try:
  153. authorize(actor.subject, room['room_id'])
  154. except RuntimeStoreError as exc:
  155. if exc.reason != 'permission_denied':
  156. raise
  157. return False
  158. return True
  159. # Page the actor-visible collection, not the room table: scan the underlying cursor until
  160. # the page holds ``limit`` visible rooms, and continue at the next VISIBLE room (or None),
  161. # so a page is never short or empty while visible rooms remain after it. ``offset`` is
  162. # that underlying continuation cursor, as returned in ``next_offset``.
  163. found, cursor, next_offset = [], offset, None
  164. while next_offset is None:
  165. batch = rooms.list_rooms(db_path, **{**params, 'offset': cursor})
  166. for room in batch:
  167. if visible(room):
  168. if len(found) == limit:
  169. next_offset = cursor
  170. break
  171. found.append(room)
  172. cursor += 1
  173. if len(batch) < limit:
  174. break
  175. return {'rooms': found, 'next_offset': next_offset}
  176. def create():
  177. from gateway.hosted_room_discussion import DiscussionValidationError, validate_roster
  178. from gateway.session_hosted_members import member_roster, refuse_unseatable
  179. # Only a host-scoped grant, the one that may address siblings directly
  180. # (``AuthorityConnection._sibling_for``), seats served siblings with no entry; a
  181. # profile-bound one seats its own profile plus its hosted_rooms.profiles entries, as
  182. # before. Both create paths validate here, by the driver's rule.
  183. homes, refused = member_roster(authority, siblings=host_scope)
  184. refuse_unseatable(params.get('members'), refused)
  185. try:
  186. members = validate_roster(params.get('members'), local_profiles=homes)
  187. if service is not None:
  188. return {'room': service.create_room(**params, admit=admit)}
  189. except DiscussionValidationError as exc:
  190. # Name the refused member; the bare code left Desktop and gateway.log silent (#139002).
  191. raise RuntimeStoreError('invalid_params', field='members', detail=str(exc)) from exc
  192. normalized = [{'member_id': m.member_id, 'profile': m.profile, 'handle': m.handle,
  193. 'target': dict(m.target or {}),
  194. **({'display_name': m.display_name} if m.display_name else {})} for m in members]
  195. return {'room': rooms.create_room(db_path, room_id=params.get('room_id'),
  196. name=params.get('name'), members=normalized, authority_gateway_id=gateway_id,
  197. admit=admit)}
  198. def disband():
  199. from gateway.hosted_room_driver import list_tasks
  200. state = rooms.room_state(db_path, room_id=params.get('room_id'), include_disbanded=True)
  201. if service is not None and state.get('disbanded_at') is None:
  202. service.stop_room(params.get('room_id'),
  203. cancel_id=params.get('cancel_id') or 'room-disbanded',
  204. require_acknowledged=True)
  205. service.revoke_room_routes(params.get('room_id'))
  206. # Metadata control must not destroy an active execution or bypass Stop.
  207. if service is None and any(list_tasks(db_path, room_id=params.get('room_id'), status=status)
  208. for status in ('queued', 'running', 'stopping', 'indeterminate', 'deferred')):
  209. raise RuntimeStoreError('runtime_coordination_required')
  210. state = rooms.room_state(db_path, room_id=params.get('room_id'), include_disbanded=True)
  211. return {'tombstone': rooms.disband_room(db_path, room_id=params.get('room_id'),
  212. expected_gateway_id=gateway_id, expected_epoch=state['authority_epoch'])}
  213. def state():
  214. room = rooms.room_state(db_path, **params)
  215. result = {'room': room}
  216. if room.get('disbanded_at') is None:
  217. result['context'] = room_context(db_path, room_id=room['room_id'])
  218. if service is not None:
  219. result['driver_status'] = service.status(room['room_id'])
  220. return result
  221. handlers = {
  222. 'groups.capabilities': capabilities,
  223. 'groups.list': listing,
  224. 'groups.create': create,
  225. 'groups.state': state,
  226. 'groups.log': lambda: rooms.read_events(db_path, **params),
  227. 'groups.rename': lambda: {'room': rooms.rename_room(db_path, **params)},
  228. 'groups.context': lambda: set_room_context(db_path, **params),
  229. 'groups.disband': disband,
  230. }
  231. return handlers[method]()
  232. def _execution_control(service, method, params):
  233. def send():
  234. from gateway.hosted_rooms import user_event_id
  235. event = service.send(room_id=params.get('room_id'),
  236. event_id=user_event_id(params.get('event_id')),
  237. payload=params.get('payload'))
  238. return {'event': event, 'client_event_id': params.get('event_id'),
  239. 'accepted': True, 'driver_started': True}
  240. def attempt_control():
  241. if (type(params.get('execution_generation')) is not int
  242. or params['execution_generation'] < 1
  243. or not params.get('member_id') or not params.get('task_id')):
  244. raise RuntimeStoreError('invalid_params')
  245. operation = service.discard_room_task if method == 'groups.discard' else service.retry_room_task
  246. task = operation(**params)
  247. identity = task['identity']
  248. receipt = {field: getattr(identity, field) for field in
  249. ('room_id', 'task_id', 'thread_id', 'turn_id')}
  250. receipt.update({field: task[field] for field in
  251. ('status', 'execution_generation', 'cancel_generation')})
  252. return {'discarded' if method == 'groups.discard' else 'retried': True, 'task': receipt}
  253. def approve():
  254. if (type(params.get('execution_generation')) is not int
  255. or params['execution_generation'] < 1
  256. or params.get('choice') not in {'once', 'deny'}
  257. or not isinstance(params.get('request_id'), str) or not params['request_id']):
  258. raise RuntimeStoreError('invalid_params')
  259. return {'approved': True, 'result': service.approve_room_task(**params)}
  260. handlers = {
  261. 'groups.send': send,
  262. 'groups.stop': lambda: {'cancelled': service.stop_room(
  263. params.get('room_id'), cancel_id=params.get('cancel_id') or uuid.uuid4().hex)},
  264. 'groups.continue': lambda: {'continued': True, 'event': service.continue_room(
  265. params.get('room_id'), discussion_event_id=str(params.get('discussion_event_id') or ''))},
  266. 'groups.retry': attempt_control,
  267. 'groups.discard': attempt_control,
  268. 'groups.approve': approve,
  269. }
  270. return handlers[method]()
  271. # A local chat: minted by this owner (``local-*``), or a pre-upgrade chat it adopted, which keeps its
  272. # historical id as chat_id under a ``gateway.local_legacy.v1:`` binding. These are the chats
  273. # ``resolve_local_target`` resolves by chat_id without adopting anything (its policy-receipt arm adds
  274. # none: a receipt-bound chat_id that is not ``local-*`` is an adopted one). A messaging or API
  275. # conversation with the same title is neither.
  276. _LOCAL_CHAT = "(chat_id LIKE 'local-%' OR EXISTS (SELECT 1 FROM state_meta WHERE key = ? || sessions.chat_id))"
  277. # Sibling authorities whose last roster read failed: warned once per outage, then logged at debug.
  278. # Weak, so an unserved profile drops out with its authority and a re-served one (a new authority)
  279. # is warned again.
  280. _DEGRADED = weakref.WeakSet()
  281. def _roster_siblings(connection, params):
  282. """The other local bots this process serves (``session_authorities.local_bots``, the set group
  283. seating uses too), as ``[(bot, reader)]``, snapshotted on the owner loop, where serve and
  284. unserve mutate the registry; an unbuilt slot is skipped. ``reader`` is the principal that row's
  285. session fields are read as, or None for a metadata-only row: only a host-scoped socket routes
  286. session verbs to a sibling (``AuthorityConnection._sibling_for``), and only a bot addressable
  287. by its own name (no ``problem``) is the one a click on its row opens. It carries this socket's
  288. subject and capabilities, which is what the routed connection would carry; none is built, so
  289. no poll (routed or not) adds connections."""
  290. from gateway.session_authorities import local_bots
  291. from gateway.session_contract import SessionActor
  292. authority, actor = connection.authority, connection.actor
  293. if getattr(getattr(authority, 'runner', None), 'session_authorities', None) is None:
  294. return []
  295. readable = connection.host_scoped and params.get('include_sessions', True) is True
  296. return [(bot, SessionActor(actor.subject, bot.authority.profile_id, actor.capabilities, actor.transport_id)
  297. if readable and bot.problem is None else None)
  298. for bot in local_bots(authority.runner) if bot.authority is not authority]
  299. def _bot_sessions(authority, actor):
  300. """A roster row's session fields, read from *authority*'s own store as *actor*: the exact
  301. "Bot Chat" title (the status dot's session and the row's age) and the newest visible local
  302. conversation, each only when the actor owns it or holds operator access. Reused while the
  303. store has not moved, as the pre-unified roster did (#117257): every connection polls every 5 s.
  304. Discovery is read-only: no legacy restore/unarchive or new SessionDB handle."""
  305. from tui_gateway.profile_roster_cache import cached_reader_session_fields
  306. operator = 'session:operator' in actor.capabilities
  307. return cached_reader_session_fields(Path(authority.db.db_path).parent, (actor.subject, operator),
  308. lambda: _read_bot_sessions(authority.db, actor.subject, operator))
  309. def _read_bot_sessions(db, subject, operator):
  310. from hermes_state_local_migration import LEGACY_PREFIX
  311. from hermes_state_sessions import INTERNAL_LISTING_SOURCES
  312. hidden_sources = ','.join('?' * len(INTERNAL_LISTING_SOURCES))
  313. owner_filter, owner_params = ('', ()) if operator else ('user_id=? AND ', (subject,))
  314. # One read connection for the whole row. A store closed meanwhile (its profile unserved
  315. # mid-poll) refuses rather than reopening a writer into a home that is being deleted.
  316. with db._read_ctx(reopen=False) as conn:
  317. def summary(root):
  318. tip = db._compression_tip_on(conn, root['id'])
  319. target = conn.execute('SELECT * FROM sessions WHERE id=?', (tip,)).fetchone() or root
  320. message = conn.execute(
  321. "SELECT content FROM messages WHERE session_id=? AND active=1 "
  322. "AND role IN ('user','assistant') AND TRIM(COALESCE(content,''))!='' "
  323. "ORDER BY id DESC LIMIT 1", (tip,)).fetchone()
  324. text = ' '.join(str(message[0] or '').split()) if message else ''
  325. return {'id': root['id'], 'resolved_id': tip, 'title': target['title'] or '',
  326. 'root_title': root['title'] or '',
  327. 'preview': text[:80] + '...' if len(text) > 80 else text,
  328. 'started_at': target['started_at'] or 0,
  329. 'last_active': target['last_activity_at'] or target['started_at'] or 0,
  330. 'message_count': target['message_count'] or 0}
  331. fields: dict = {'last_session': None, 'worker_session': None, 'canonical_session': None}
  332. # The named registry is not a recency window and canonical chats are hidden.
  333. canonical = conn.execute(f'SELECT * FROM sessions WHERE title=? AND {_LOCAL_CHAT}',
  334. ('Bot Chat', LEGACY_PREFIX)).fetchone()
  335. from gateway.owner_identity import same_owner
  336. if canonical and (same_owner(canonical['user_id'], subject) or operator) and not canonical['archived']:
  337. fields['canonical_session'] = summary(canonical)
  338. # The newest conversation a picker shows: a hidden row (a Bot Chat, group member plumbing)
  339. # never wins, as in the pre-unified roster's listing.
  340. latest = conn.execute(
  341. f"SELECT * FROM sessions WHERE {owner_filter}{_LOCAL_CHAT} AND archived=0 AND hidden=0 "
  342. f"AND COALESCE(source,'') NOT IN ({hidden_sources}) "
  343. "ORDER BY COALESCE(last_activity_at,started_at) DESC LIMIT 1",
  344. (*owner_params, LEGACY_PREFIX, *INTERNAL_LISTING_SOURCES)).fetchone()
  345. if latest and fields['canonical_session'] and latest['id'] == canonical['id']:
  346. fields['last_session'] = dict(fields['canonical_session'])
  347. elif latest:
  348. fields['last_session'] = summary(latest)
  349. # The profile's freshest worker (#90268, #140977): workers are deny-listed out of every
  350. # conversation listing, so a roster row that only carried conversation fields read "idle"
  351. # while a kanban/tool run was heartbeating (#72016). Newest DENIED source wins, never an
  352. # archived or hidden row; no owner filter -- a worker's ``user_id`` is the dispatch identity
  353. # (a bot, the kanban dispatcher), not this reader, and the legacy roster read it the same way.
  354. worker = conn.execute(
  355. f"SELECT id, source, title, COALESCE(last_activity_at, started_at) AS last_active FROM sessions "
  356. f"WHERE COALESCE(source,'') IN ({hidden_sources}) AND archived=0 AND hidden=0 "
  357. "ORDER BY last_active DESC LIMIT 1", INTERNAL_LISTING_SOURCES).fetchone()
  358. if worker:
  359. fields['worker_session'] = {'id': worker['id'], 'source': (worker['source'] or '').strip().lower(),
  360. 'title': worker['title'] or '', 'last_active': worker['last_active'] or 0}
  361. return fields
  362. def _sibling_sessions(authority, reader):
  363. """A sibling row's session fields. A retiring sibling is not read: unserve marks it before it
  364. closes the store, and this runs right before the read (a close that still lands mid-read is
  365. refused by ``reopen=False``). Any failure leaves only that row metadata-only, as the
  366. pre-unified roster degraded per profile, instead of failing the whole roster."""
  367. if authority.retiring or authority.retired:
  368. return {}
  369. try:
  370. fields = _bot_sessions(authority, reader)
  371. except Exception as exc: # health: allow BLE001 -- one bot's store must never fail the whole roster
  372. log = logger.debug if authority in _DEGRADED else logger.warning
  373. _DEGRADED.add(authority)
  374. log('Bots roster: %s is metadata-only, its session store did not answer (%s)', authority.profile_id, exc)
  375. return {}
  376. _DEGRADED.discard(authority)
  377. return fields
  378. def _profiles(authority, actor, home, params, siblings=()):
  379. """The Bots roster: this profile's row, then every profile it can name. ``siblings`` is
  380. ``_roster_siblings``' snapshot; a sibling with a reader carries the same session fields."""
  381. from hermes_cli.profiles import _profile_info
  382. import hermes_yaml as yaml
  383. include_sessions = params.get('include_sessions', True)
  384. if type(include_sessions) is not bool:
  385. raise RuntimeStoreError('invalid_params')
  386. from gateway.session_authorities import served_profile_name
  387. name = served_profile_name(home)
  388. profile = _profile_info(name, home, is_default=name == 'default')
  389. row = {'name': name, 'path': str(home), 'is_default': profile.is_default,
  390. 'model': profile.model, 'provider': profile.provider,
  391. 'description': profile.description or '', 'display_name': profile.display_name or '',
  392. 'skill_count': profile.skill_count or 0}
  393. path = home / 'profile.yaml'
  394. meta = yaml.safe_load(path.read_text(encoding='utf-8-sig')) if path.is_file() else {}
  395. meta = meta if isinstance(meta, dict) else {}
  396. row['ui_meta_revisions'] = _ui_meta_revisions(meta)
  397. if isinstance(meta.get('ui_meta'), dict):
  398. row['ui_meta'] = meta['ui_meta']
  399. row['has_avatar'] = _has_avatar(home)
  400. if include_sessions:
  401. row.update(_bot_sessions(authority, actor))
  402. profiles = [row]
  403. seen = {home.resolve()}
  404. def discovered(configured, target, reach=None):
  405. # Discovery alone is never a licence to read another owner's state/configuration: only a
  406. # sibling this connection already routes to (``reach``) adds its session fields.
  407. if target.resolve() in seen:
  408. return
  409. seen.add(target.resolve())
  410. entry = {'name': configured, 'path': str(target), 'is_default': configured == 'default',
  411. 'model': '', 'provider': '', 'description': '', 'display_name': '',
  412. 'skill_count': 0, 'has_avatar': _has_avatar(target), 'ui_meta_revisions': {}}
  413. if include_sessions and reach is not None:
  414. entry.update(_sibling_sessions(*reach))
  415. meta = _discovered_profile_meta(configured, target / 'profile.yaml')
  416. if isinstance(meta, dict):
  417. # The profile's own metadata, not a placeholder: Desktop seeds the Edit profile dialog
  418. # from these fields and skips the avatar push when ``has_avatar`` is set.
  419. entry['description'] = str(meta.get('description') or '').strip()
  420. entry['display_name'] = str(meta.get('display_name') or '')
  421. entry['ui_meta_revisions'] = _ui_meta_revisions(meta)
  422. if isinstance(meta.get('ui_meta'), dict):
  423. entry['ui_meta'] = meta['ui_meta']
  424. profiles.append(entry)
  425. # Sibling profiles this process multiplexes: the roster lists them with the same
  426. # standing as a hosted-room destination, and the client routes their sessions by name.
  427. for bot, reader in siblings:
  428. # The row keeps the name its home resolves to (the client routes by it); a bot that name
  429. # does not reach (a linked ``profiles/<name>`` reads as ``default``) has no reader, so its
  430. # row stays metadata-only.
  431. discovered(served_profile_name(bot.home), bot.home,
  432. (bot.authority, reader) if reader is not None else None)
  433. service = getattr(authority, 'hosted_room_service', None)
  434. if service is not None:
  435. for configured, target in service.profile_homes().items():
  436. discovered(configured, target)
  437. # Every other profile under this root, as the pre-authority ``profiles.list`` named them
  438. # (RT-38): a host running one gateway per profile serves no siblings, and Desktop drops every
  439. # union agent the active source leaves out, so a default-only answer hid the other bots.
  440. # Metadata only, like any discovery above.
  441. from hermes_cli.profiles import _get_default_hermes_home
  442. root = _get_default_hermes_home()
  443. if root.is_dir():
  444. discovered('default', root)
  445. for target in _named_profile_dirs():
  446. discovered(target.name, target)
  447. # ``install_id`` (same value as /api/status) lets a multi-connection client prove which
  448. # machine answered; the roster trace logged ``answered=-`` without it. It already returns
  449. # None for an unreadable root.
  450. from hermes_cli.install_identity import get_install_id
  451. return {'profiles': profiles, 'bot_mode_protocol': True, 'install_id': get_install_id() or ''}
  452. # The Bots pane polls ``profiles.list`` every 5 s. Re-listing ``profiles/`` and re-parsing every
  453. # sibling ``profile.yaml`` each time cost ~60 ms at 50 profiles, so both are cached, and neither
  454. # cache trusts a timestamp to notice a change. Linux stamps mtime AND ctime from the coarse clock
  455. # (one jiffy, 1-10 ms), FAT/SMB/HFS+ tick coarser still, and a directory's size and inode survive
  456. # a file appearing and vanishing inside it; so a create-then-delete inside one tick leaves every
  457. # stat field of the profile dir as it was. The listing is therefore keyed on what
  458. # ``_iter_named_profile_dirs`` actually decides by (each entry's identity markers and tombstone,
  459. # present or not), and a parsed ``profile.yaml`` on the file's bytes: the read is cheap, the
  460. # YAML parse is the cost the cache avoids.
  461. _NAMED_DIRS_CACHE: dict[str, tuple[tuple, list]] = {}
  462. _PROFILE_META_CACHE: dict[str, tuple[bytes, object]] = {}
  463. def _stat_key(stat):
  464. return (stat.st_mtime_ns, stat.st_ctime_ns, stat.st_size, stat.st_ino)
  465. def _lstat_key(path):
  466. try:
  467. stat = os.lstat(path)
  468. except OSError:
  469. return None
  470. return (stat.st_mode, *_stat_key(stat))
  471. def _named_profile_dirs():
  472. from hermes_cli.profiles import _get_profiles_root, _iter_named_profile_dirs
  473. from hermes_constants import _PROFILE_IDENTITY_MARKERS, profile_tombstone_path
  474. profiles_root = _get_profiles_root()
  475. try:
  476. with os.scandir(profiles_root) as entries:
  477. rows = []
  478. for entry in entries:
  479. # Presence (not just timestamps) of every marker ``named_profile_has_identity``
  480. # checks, and of the tombstone ``named_profile_is_deleted`` checks: one lstat each.
  481. markers = tuple(_lstat_key(os.path.join(entry.path, marker))
  482. for marker in _PROFILE_IDENTITY_MARKERS)
  483. tombstone = os.path.exists(profile_tombstone_path(Path(entry.path)))
  484. rows.append((entry.name, _stat_key(entry.stat()), markers, tombstone))
  485. signature = (_stat_key(os.stat(profiles_root)), tuple(sorted(rows)))
  486. except OSError:
  487. signature = None
  488. cached = _NAMED_DIRS_CACHE.get(str(profiles_root))
  489. if signature is not None and cached and cached[0] == signature:
  490. return cached[1]
  491. try:
  492. targets = _iter_named_profile_dirs()
  493. except OSError as exc:
  494. # An unreadable ``profiles/`` drops the siblings, never the authority's own roster.
  495. logger.warning('profiles.list: cannot list %s: %s', profiles_root, exc)
  496. return []
  497. if signature is not None:
  498. _NAMED_DIRS_CACHE[str(profiles_root)] = (signature, targets)
  499. return targets
  500. _ASSET_EXTS = ('png', 'jpg', 'webp')
  501. def _has_avatar(home):
  502. """Cheap existence flag so a roster paint skips a ``get_asset`` probe per profile."""
  503. return any((home / 'assets' / f'avatar.{ext}').is_file() for ext in _ASSET_EXTS)
  504. def _ui_meta_revisions(meta):
  505. """``_ui_meta_revisions`` from a parsed profile.yaml: str keys, non-bool ints, floor 0.
  506. Always present (``{}`` when the profile never wrote one) so a client can feature-detect
  507. gateway-owned CAS instead of reading a missing field as revision zero.
  508. """
  509. revisions = meta.get('_ui_meta_revisions') if isinstance(meta, dict) else None
  510. return {str(k): max(0, v) for k, v in revisions.items()
  511. if type(v) is int} if isinstance(revisions, dict) else {}
  512. def _discovered_profile_meta(name, meta_path):
  513. """A sibling's parsed ``profile.yaml``, or ``{}``. One unreadable file (a legacy code page,
  514. bad YAML, a permission error) degrades that row to basic metadata; it never fails the roster."""
  515. import hermes_yaml as yaml
  516. try:
  517. raw = meta_path.read_bytes()
  518. except (FileNotFoundError, NotADirectoryError, IsADirectoryError):
  519. return {}
  520. except OSError as exc:
  521. logger.warning('profiles.list: skipping unreadable profile.yaml for %s (%s): %s', name, meta_path, exc)
  522. return {}
  523. cached = _PROFILE_META_CACHE.get(str(meta_path))
  524. if cached and cached[0] == raw:
  525. return cached[1]
  526. try:
  527. meta = yaml.safe_load(raw.decode('utf-8-sig'))
  528. except (ValueError, yaml.YAMLError) as exc:
  529. logger.warning('profiles.list: skipping unreadable profile.yaml for %s (%s): %s', name, meta_path, exc)
  530. meta = {}
  531. _PROFILE_META_CACHE[str(meta_path)] = (raw, meta)
  532. return meta