session_live_listing.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """The live session feeds (``session.list`` live scope, ``session.active_list``) read off the loop.
  2. Both feeds cover every session in ``authority.sessions``, which can hold thousands of rows (mostly
  3. finished cron runs; boot restores only open sessions, an ended one comes back on first touch).
  4. Walking it one row at a time on the loop cost three full hydrations plus a receipt JSON parse per
  5. session: the loop stalled past 10 s, WS writes and the Telegram adapter's ``updater.stop()`` missed
  6. their deadlines (#141236).
  7. Here the loop does only in-memory work (authorization and liveness from ``LiveSession``); the store
  8. facts for a batch of sessions are ONE worker-thread read: picker columns, runtime revision and
  9. generation, pending admissions, and the local receipt target's ``ended_at``.
  10. """
  11. from __future__ import annotations
  12. import asyncio
  13. import json
  14. from dataclasses import dataclass
  15. from gateway.session_contract import SessionHandle, SessionRef
  16. from hermes_state_runtime import RuntimeStoreError
  17. # Sessions per worker-thread read; each batch is one await, so the loop serves other work between them.
  18. LISTING_BATCH = 256
  19. _SKIPPED_REASONS = frozenset({'permission_denied', 'profile_mismatch', 'not_found'})
  20. @dataclass(frozen=True)
  21. class LiveEntry:
  22. session_id: str
  23. live: object
  24. handle: SessionHandle
  25. row: dict
  26. pending: list
  27. closed: bool
  28. def _store_facts(db, session_ids):
  29. """Worker thread: ``{sid: row}`` and ``{sid: [pending admission]}`` for *session_ids*, in two reads.
  30. ``target_ended_at`` is the local receipt target's ``ended_at`` (``closed_and_unviewed``'s read):
  31. SQLite extracts the target from the receipt, so no receipt is parsed in Python. A malformed or
  32. non-local receipt reads as no target, as ``_local_target`` does."""
  33. from hermes_state_local import POLICY_PREFIX
  34. ids = json.dumps(list(session_ids))
  35. with db._read_ctx() as conn:
  36. rows = conn.execute(
  37. "SELECT s.id, s.source, s.title, s.started_at, s.ended_at, s.message_count, s.model, s.hidden, "
  38. "s.archived, s.runtime_revision, s.runtime_generation, "
  39. "(SELECT t.ended_at FROM state_meta m JOIN sessions t ON t.id = CASE WHEN json_valid(m.value) "
  40. "THEN json_extract(m.value, '$.entry.session_id') END WHERE m.key = ? || s.id) AS target_ended_at "
  41. "FROM sessions s WHERE s.id IN (SELECT value FROM json_each(?))", (POLICY_PREFIX, ids)).fetchall()
  42. pending = conn.execute(
  43. "SELECT target_session_id, admission_id, seq, status FROM session_admissions "
  44. "WHERE target_session_id IN (SELECT value FROM json_each(?)) "
  45. "AND status IN ('queued','started','unknown') ORDER BY seq", (ids,)).fetchall()
  46. by_session: dict[str, list] = {}
  47. for row in pending:
  48. by_session.setdefault(row['target_session_id'], []).append(dict(row))
  49. return {row['id']: dict(row) for row in rows}, by_session
  50. def _masked_pending(live, rows):
  51. """``SessionAuthority._pending_rows``: a settlement not yet published still reads as its started claim."""
  52. if not live.settling:
  53. return rows
  54. merged = {row['admission_id']: row for row in rows}
  55. merged.update(live.settling)
  56. return sorted(merged.values(), key=lambda row: row['seq'])
  57. def _execution_state(pending) -> str:
  58. statuses = {row['status'] for row in pending}
  59. return 'unknown' if 'unknown' in statuses else ('running' if 'started' in statuses else 'idle')
  60. def _readable(authority, actor, session_id) -> bool:
  61. if session_id not in authority.sessions:
  62. return False # retired since the snapshot: a listing never restores a session
  63. from gateway.session_retirement import LISTING_READ
  64. token = LISTING_READ.set(True)
  65. try:
  66. authority.authorize(actor, SessionRef(actor.profile_id, session_id), 'session:read')
  67. except RuntimeStoreError as exc:
  68. if exc.reason not in _SKIPPED_REASONS:
  69. raise
  70. return False
  71. finally:
  72. LISTING_READ.reset(token)
  73. return True
  74. async def live_entries(authority, actor) -> list[LiveEntry]:
  75. """Every live session *actor* may read, with its handle, picker row and closed-and-unviewed verdict."""
  76. from gateway.session_local_close import closed_idle_unviewed
  77. entries = []
  78. snapshot = tuple(authority.sessions)
  79. for start in range(0, len(snapshot), LISTING_BATCH):
  80. batch = [sid for sid in snapshot[start:start + LISTING_BATCH] if _readable(authority, actor, sid)]
  81. if not batch:
  82. continue
  83. rows, pending = await asyncio.to_thread(_store_facts, authority.db, batch)
  84. if authority.retiring or authority.retired:
  85. return [] # parked or unserved during the read: nothing here is this profile's to list any more
  86. for sid in batch:
  87. live, row = authority.sessions.get(sid), rows.get(sid)
  88. if live is None or row is None:
  89. continue # retired during the read, or its row was deleted (``_handle``'s not_found)
  90. masked = _masked_pending(live, pending.get(sid, []))
  91. state = _execution_state(masked)
  92. handle = SessionHandle(SessionRef(authority.profile_id, sid), authority.instance_id, authority.epoch,
  93. row['runtime_revision'], row['runtime_generation'], state)
  94. entries.append(LiveEntry(sid, live, handle, row, masked,
  95. closed_idle_unviewed(live, state, row['target_ended_at'])))
  96. return entries