session_local_close.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. """The picker's view of a canonical local session its last viewer closed, as main listed it.
  2. Main's sidecar stamped ``tui_close`` (``/new``, a resume switch) and ``tui_shutdown`` (exit) on the
  3. session row it owned; pickers then listed it as resumable. The stamp itself is the owner's
  4. ``session.detach {end_reason}`` (``session_acp_lifecycle.end_idle_viewer_session``); this module only
  5. decides which live rows read as closed.
  6. """
  7. from gateway.session_history_mount import is_history_mount
  8. from hermes_state_local import local_receipt
  9. from hermes_state_runtime import RuntimeStoreError
  10. def _local_target(authority, session_id):
  11. try:
  12. return local_receipt(authority.db, session_id)['entry']['session_id']
  13. except (RuntimeStoreError, KeyError, TypeError):
  14. return None # not a local session (API binding, malformed receipt): nothing to read as closed
  15. def closed_idle_unviewed(live, execution_state, target_ended_at):
  16. """The verdict on facts already read: the live listings read ``target_ended_at`` for a whole batch
  17. in one off-loop query (``gateway/session_live_listing``)."""
  18. if live is None or live.subscribers or execution_state != 'idle':
  19. return False
  20. if live.detached or is_history_mount(live):
  21. return True # an old messaging conversation or a stored one: history once no one views it
  22. return target_ended_at is not None
  23. def closed_and_unviewed(authority, session_id, handle):
  24. """A live-feed row main would list as resumable: closed, idle and with no viewer left."""
  25. live = authority.sessions.get(session_id)
  26. if live is None or live.subscribers or handle.execution_state != 'idle':
  27. return False
  28. if live.detached or is_history_mount(live):
  29. return True
  30. target = _local_target(authority, session_id)
  31. row = authority.db.get_session_list_row(target) if target else None
  32. return closed_idle_unviewed(live, handle.execution_state, row and row.get('ended_at'))