session_active_list.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """``session.active_list`` on the session authority: the live state of the turns it owns.
  2. Desktop's silent-turn check (``session-states.ts`` ``onEventSilence``) and its sidebar poll read
  3. this verb. Unregistered here, the WS fell back to the legacy sidecar's handler, which lists only
  4. the sidecar's own ``_sessions`` and never an authority-owned session: a foreground tool quiet for
  5. 45 s read as a reaped turn, and Desktop painted "Hermes ended this turn without a reply" over a
  6. turn that was still running. Rows keep ``server._session_live_item``'s shape; ``id`` and
  7. ``session_key`` are both the session id, as ``session.resume`` reports them.
  8. """
  9. from __future__ import annotations
  10. from hermes_state_runtime import RuntimeStoreError
  11. def _status(live, handle, pending) -> str:
  12. if handle.execution_state == 'running':
  13. from gateway.session_server_requests import open_requests
  14. sid = handle.ref.session_id
  15. # A question holds the turn: a shared control (approval) or a viewer request (clarify batch,
  16. # sudo, secret, vault) still open on the stream.
  17. asked = live.controls.snapshot(sid, handle.execution_generation) or open_requests(live.event_stream, sid)
  18. return 'waiting' if asked else 'working'
  19. # Admitted, not yet claimed by the drain: the turn exists, its agent has not started.
  20. return 'starting' if any(row['status'] == 'queued' for row in pending) else 'idle'
  21. async def active_list(connection, ref, params):
  22. if set(params) - {'profile', 'current_session_id'}:
  23. raise RuntimeStoreError('invalid_params')
  24. if 'session:read' not in connection.actor.capabilities:
  25. raise RuntimeStoreError('permission_denied')
  26. from gateway.session_live_listing import live_entries
  27. from hermes_state_sessions import INTERNAL_LISTING_SOURCES
  28. current = str(params.get('current_session_id') or '')
  29. rows = []
  30. for entry in await live_entries(connection.authority, connection.actor):
  31. row = entry.row
  32. # The same population as the live ``session.list`` feed, hidden chats included: a hidden
  33. # chat open in a pane still needs its turn's liveness.
  34. if row.get('source') in INTERNAL_LISTING_SOURCES or entry.closed:
  35. continue
  36. sid, started = entry.session_id, float(row.get('started_at') or 0)
  37. rows.append({'current': sid == current, 'id': sid, 'session_key': sid,
  38. 'last_active': float(row.get('ended_at') or started), 'started_at': started,
  39. 'message_count': int(row.get('message_count') or 0),
  40. 'model': str(row.get('model') or ''), 'preview': '', 'title': row.get('title') or '',
  41. 'source': row.get('source'), 'status': _status(entry.live, entry.handle, entry.pending)})
  42. return {'sessions': rows}