session_events.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """Authority-local subscription ordering over the existing bounded transports/replay."""
  2. from copy import deepcopy
  3. import logging
  4. import threading
  5. import time
  6. import uuid
  7. import weakref
  8. from tui_gateway import event_replay
  9. from tui_gateway.transport import FanoutTransport
  10. class SessionEvents:
  11. def __init__(self):
  12. self.lock = threading.RLock()
  13. self.fanout = FanoutTransport()
  14. # A private lifetime key prevents two profiles (or legacy TUI owners)
  15. # with the same persisted ID from reading each other's replay.
  16. self._key = uuid.uuid4().hex
  17. # Nothing but this object can name the key, so its ring and counters go when it does (a
  18. # retired or replaced LiveSession), instead of waiting for the shared cache's FIFO cap.
  19. weakref.finalize(self, event_replay.release_key, self._key, blocking=False).atexit = False
  20. self.epoch = uuid.uuid4().hex
  21. self.sequence = 0
  22. self._public_id = None
  23. self.execution = {}
  24. # Synchronous same-thread recipients (API run projections): unlike fanout peers they
  25. # cannot lose a frame to a detach that races the writer thread.
  26. self.observers = set()
  27. # Set by the authority that attaches transports: retires the subscription of a
  28. # peer whose bounded backlog overflowed, so it cannot linger looking subscribed.
  29. self.on_overflow = None
  30. # The running execution's open tool calls and its last frame time; a hosted room's info
  31. # poll reports them so the room shows which tool a member is in (``progress_for``).
  32. self._progress = None
  33. def note_progress(self, generation, event_type, payload):
  34. """Record one execution frame (the caller holds ``lock`` and passed the generation fence)."""
  35. progress = self._progress
  36. if progress is None or progress['generation'] != generation:
  37. progress = self._progress = {'generation': generation, 'tools': {}}
  38. payload = payload if isinstance(payload, dict) else {}
  39. tool_id = str(payload.get('tool_id') or '')
  40. if event_type == 'tool.start':
  41. progress['tools'][tool_id] = str(payload.get('name') or '') or 'tool'
  42. elif event_type == 'tool.complete':
  43. progress['tools'].pop(tool_id, None)
  44. progress['at'] = time.time()
  45. def progress_for(self, generation):
  46. """``{"tool", "progress_at"}`` for *generation*'s execution: the newest open tool call
  47. (absent between tools) and its last frame time; ``{}`` before its first frame."""
  48. with self.lock:
  49. progress = self._progress
  50. if progress is None or progress['generation'] != generation:
  51. return {}
  52. tools = list(progress['tools'].values())
  53. return {'progress_at': progress['at'], **({'tool': tools[-1]} if tools else {})}
  54. def release(self):
  55. """The session ended with no viewer left (it may stay resumable): free its replay frames
  56. now. A later publish or attach finds them gone and starts a new epoch, as after eviction."""
  57. with self.lock:
  58. event_replay.release_key(self._key)
  59. def watermark(self):
  60. with self.lock:
  61. if self.sequence and not event_replay.latest_seq(self._key):
  62. # The shared bounded cache evicted this session. Never reuse a
  63. # sequence in the old epoch, even if publication resumes later.
  64. self.epoch = uuid.uuid4().hex
  65. self.sequence = 0
  66. return self.epoch, self.sequence
  67. def publish(self, session_id, payload, *, event_type="message.complete", execution=None):
  68. # ``execution`` stamps this frame alone; the shared stamp is read without the lock elsewhere.
  69. with self.lock:
  70. self.watermark()
  71. frame = {'jsonrpc': '2.0', 'method': 'event', 'params': {
  72. **(self.execution if execution is None else execution), 'type': event_type, 'session_id': self._key,
  73. 'payload': deepcopy(payload), 'replay_epoch': self.epoch}}
  74. event_replay._stamp_event(frame)
  75. if frame['params']['seq'] <= self.sequence:
  76. # Another publisher may evict us after watermark's lookup.
  77. self.epoch = uuid.uuid4().hex
  78. frame['params']['replay_epoch'] = self.epoch
  79. # The ring froze the frame under its private lookup key; live recipients see the
  80. # canonical ID here and ``since`` restores it on the thawed replay copies.
  81. frame['params']['session_id'] = session_id
  82. self._public_id = session_id
  83. self.sequence = frame['params']['seq']
  84. def overflow(transport):
  85. # The peer's socket is healthy; only its event subscription ended. Same
  86. # shape as the client's own synthetic gap so one consumer handles both.
  87. if self.on_overflow is not None:
  88. self.on_overflow(transport)
  89. return {'jsonrpc': '2.0', 'method': 'event', 'params': {
  90. 'type': 'session.replay_gap', 'session_id': session_id,
  91. 'payload': {'replay_epoch': self.epoch, 'latest_seq': self.sequence}}}
  92. delivered = self.fanout.write(frame, overflow=overflow)
  93. for observer in tuple(self.observers):
  94. try:
  95. observer(frame)
  96. delivered = True
  97. except Exception:
  98. # A request-owned projection cannot undo committed publication or starve peers.
  99. logging.getLogger(__name__).warning('Session event observer failed for %s', session_id,
  100. exc_info=True)
  101. # True when a live viewer or observer took the frame; the replay ring alone is not
  102. # delivery (a detached finite viewer reads nothing until it reattaches).
  103. return delivered
  104. def catch_up(self, transport, epoch, sequence):
  105. """Caller holds ``lock``, before *transport* joins ``fanout``: write it the frames after the
  106. cursor its connection's replay answered. A reconnect asks ``session.events.since`` and then
  107. ``session.resume``; whatever the session published in between went to no live peer of this
  108. socket, and the replay was already over, so a reply's tail and ``message.complete`` were lost
  109. and the view stayed on its first chunk. Under the lock nothing can publish ahead of them."""
  110. replay = self.since(epoch, sequence)
  111. if replay['snapshot_required']:
  112. return
  113. for frame in replay['events']:
  114. transport.write({'jsonrpc': '2.0', 'method': 'event', 'params': frame})
  115. def since(self, epoch, sequence):
  116. with self.lock:
  117. current_epoch, latest = self.watermark()
  118. frames = event_replay.events_since(self._key, sequence)
  119. missing = (epoch != current_epoch or sequence > latest
  120. or event_replay.is_truncated(self._key, sequence)
  121. or event_replay.latest_seq(self._key) != latest)
  122. events = [] if missing else [
  123. {**frame, 'session_id': self._public_id} if frame.get('session_id') == self._key else frame
  124. for frame in frames]
  125. return {'events': events, 'latest_seq': latest, 'last_sequence': latest,
  126. 'epoch': current_epoch, 'replay_epoch': current_epoch,
  127. 'truncated': missing, 'snapshot_required': missing, 'count': len(events)}