session_retirement.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. """Idle conversations leave the authority's memory, as a restart leaves them.
  2. A ``LiveSession`` (event stream, replay ring key, locks, pending controls) is only needed while a
  3. viewer watches the conversation or input runs in it. A local conversation its last viewer closed,
  4. and an API conversation between requests, otherwise stayed in ``authority.sessions`` until the
  5. process exited. Every entry point already handles an absent session (that is the state after a
  6. restart): attach and admission restore it from its receipt, an API request rebinds it, and input
  7. committed for it is restored by ``_schedule``/``_publish_pending`` before the FIFO runs it.
  8. """
  9. import asyncio
  10. import contextvars
  11. from gateway.config import Platform
  12. _RETIRABLE = frozenset({Platform.LOCAL, Platform.API_SERVER})
  13. # How long a retired conversation an RPC brought back stays after its last authorize: longer than any
  14. # handler's unpinned await between authorize and its ``authority.sessions`` read.
  15. RESTORED_IDLE_S = 30.0
  16. # A session listing authorizes every session in memory on each poll (Desktop/TUI every 1.5 s):
  17. # that is not use, so it must never push an armed re-retire timer back.
  18. LISTING_READ = contextvars.ContextVar("retirement_listing_read", default=False)
  19. def _pinned(authority, session_id, live):
  20. """In-memory state that still needs this exact ``LiveSession``."""
  21. from gateway.session_history_mount import is_history_mount
  22. task = live.task
  23. return (authority.sessions.get(session_id) is not live
  24. or live.source is None or live.source.platform not in _RETIRABLE
  25. or live.subscribers or live.event_stream.observers or live.rescan or live.settling
  26. or live.detached or live.detaching or is_history_mount(live)
  27. or live.mutation_lock.locked() or live.claim_gate.locked()
  28. or (task is not None and not task.done() and task is not asyncio.current_task())
  29. or session_id in authority.executing or session_id in authority.pending_closes
  30. or session_id in authority.orphan_closes or _reviewing(live))
  31. def _reviewing(live):
  32. """A cold ``/refine`` review still running: its run token lives on this ``LiveSession``, so a
  33. second /refine joins it and the next turn cancels it (``_execute_claimed``)."""
  34. run = getattr(live.review, '_background_review_run', None)
  35. return run is not None and not run.request_done.is_set()
  36. def retire_idle(authority, session_id, live, target):
  37. """Drop *live* when nothing pins it. Called with no await since the last check before it, and
  38. with none inside: the FIFO read below and the removal are one step on the loop. Input committed
  39. before that read shows as a queued row (or a worker as registered) and keeps the session; input
  40. committing after it is followed by a loop-side ``_schedule``/``_publish_pending``, which
  41. restores the conversation (``restore_retired``) before anything indexes it."""
  42. if _pinned(authority, session_id, live):
  43. return False
  44. from gateway.session_viewer_close import _busy
  45. if _busy(authority, session_id, target):
  46. return False
  47. del authority.sessions[session_id]
  48. authority.launch_sources.pop(session_id, None) # a cache: re-read from the row on demand
  49. authority.retired_routes[session_id] = live.route
  50. if live.source.platform == Platform.LOCAL:
  51. authority.retired_local.add(session_id)
  52. return True
  53. def retire_closed(authority, session_id, live, target, idle_stamp):
  54. """A closed local conversation nobody views and nothing runs in. A turn claimed since the close
  55. found the FIFO idle (*idle_stamp* replaced) reopened its row: it stays. A close whose retirement a
  56. running drain pinned is retired by that drain's idle exit (``retire_at_idle``)."""
  57. live.closed_stamp = (idle_stamp,)
  58. if live.event_stream.execution is not idle_stamp:
  59. return False
  60. return retire_idle(authority, session_id, live, target)
  61. def retire_at_idle(authority, ref, live):
  62. """The drain's idle exit: an API conversation between requests, or a closed local one whose
  63. close ran while this drain was alive."""
  64. platform = live.source.platform if live.source is not None else None
  65. if platform == Platform.API_SERVER:
  66. return retire_idle(authority, ref.session_id, live, authority.physical_target(ref))
  67. stamp = live.closed_stamp
  68. if platform == Platform.LOCAL and stamp is not None and stamp[0] is live.event_stream.execution:
  69. from gateway.session_viewer_close import _physical_target
  70. return retire_idle(authority, ref.session_id, live, _physical_target(authority, ref.session_id))
  71. return False
  72. def retire_after_review(authority, ref, live):
  73. """A cold ``/refine`` review ended (loop side): a close it pinned retires the conversation now."""
  74. if authority.sessions.get(ref.session_id) is live and live.closed_stamp is not None:
  75. retire_at_idle(authority, ref, live)
  76. def restore_retired(authority, session_id):
  77. """Bring back a conversation ``retire_idle`` let go, from its receipt. A local one keeps its
  78. frozen policy on the local adapter until it is deleted; an API one keeps its binding receipt.
  79. Anything else (deleted, never bound) stays absent, as before retirement existed."""
  80. if session_id in authority.sessions:
  81. return
  82. if session_id in authority.retired_local:
  83. from gateway.session_local_recovery import restore_local_session
  84. restore_local_session(authority, session_id)
  85. else:
  86. from gateway.session_api import restore_api_session
  87. from hermes_state_runtime import RuntimeStoreError
  88. try:
  89. restore_api_session(authority, session_id)
  90. except RuntimeStoreError:
  91. return # no binding: the caller's lookup fails exactly as it did before retirement
  92. touch_restored(authority, session_id)
  93. def touch_restored(authority, session_id):
  94. """A conversation retirement let go is back in memory (a rename, a read, a wake): it retires
  95. again once ``RESTORED_IDLE_S`` pass with no authorize on it and nothing pinning it. A viewer's
  96. close retires a watched one instead (``retire_closed``)."""
  97. live = authority.sessions.get(session_id)
  98. if live is None or session_id not in getattr(authority, 'retired_routes', ()):
  99. return
  100. try:
  101. loop = asyncio.get_running_loop()
  102. except RuntimeError:
  103. return # an off-loop authorize: the next on-loop one arms it
  104. if live.retire_timer is not None:
  105. if LISTING_READ.get():
  106. return
  107. live.retire_timer.cancel()
  108. live.retire_timer = loop.call_later(RESTORED_IDLE_S, _retire_restored, authority, session_id, live)
  109. def _retire_restored(authority, session_id, live):
  110. live.retire_timer = None
  111. if authority.sessions.get(session_id) is not live or live.subscribers or live.source is None:
  112. return # deleted or restored anew, or watched: that viewer's close retires it
  113. from gateway.session_contract import SessionRef
  114. if live.source.platform == Platform.API_SERVER:
  115. target = authority.physical_target(SessionRef(authority.profile_id, session_id))
  116. else:
  117. from gateway.session_viewer_close import _physical_target
  118. target = _physical_target(authority, session_id)
  119. if not retire_idle(authority, session_id, live, target):
  120. touch_restored(authority, session_id) # running work pins it: check again later
  121. def forget_retired(authority, session_id):
  122. """A deleted conversation retirement let go: its admission source and frozen policy on the local
  123. adapter, and its cached agent (``forget_live_session`` for one still in memory)."""
  124. local_retired = session_id in authority.retired_local
  125. authority.retired_local.discard(session_id)
  126. route = authority.retired_routes.pop(session_id, None)
  127. if route is None:
  128. return
  129. evict = getattr(authority.runner, '_evict_cached_agent', None)
  130. if callable(evict):
  131. evict(route)
  132. if not local_retired:
  133. return
  134. from gateway.session_local_recovery import local_adapter_map
  135. local = local_adapter_map(authority).get(Platform.LOCAL)
  136. saved = local.sources.get(session_id) if local is not None else None
  137. if saved is not None:
  138. local.forget(saved[0], route)