session_prune.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """``session.prune``: the owner runs `hermes sessions prune` for its own store.
  2. The CLI computes the filters and shows the preview from a read-only view; the delete itself is
  3. this owner's write, through the same ``prune_sessions`` / ``retire_prunable`` path housekeeping
  4. uses (busy sessions and the logical owner of a retained reset child are skipped). A CLI writing
  5. the store under a live gateway would be a second writer, so it never does.
  6. """
  7. import asyncio
  8. from pathlib import Path
  9. from hermes_state_runtime import RuntimeStoreError
  10. _SCALARS = (type(None), bool, int, float, str)
  11. def _filters(raw):
  12. from hermes_state_maintenance import _PRUNE_FILTER_NAMES
  13. if (not isinstance(raw, dict) or set(raw) - (_PRUNE_FILTER_NAMES | {'older_than_days'})
  14. or not all(isinstance(value, _SCALARS) for value in raw.values())):
  15. raise RuntimeStoreError('invalid_params')
  16. return dict(raw)
  17. async def prune(connection, ref, params):
  18. authority, actor = connection.authority, connection.actor
  19. # Store-wide deletion across every principal's rows: the local operator only (a ticket from
  20. # this host's private control socket), never a remote or browser grant.
  21. if not connection.native_owner or 'session:operator' not in actor.capabilities:
  22. raise RuntimeStoreError('permission_denied')
  23. if actor.profile_id != authority.profile_id:
  24. raise RuntimeStoreError('profile_mismatch')
  25. never_active = params.get('never_active_days')
  26. if ('filters' in params) == (never_active is not None) or (
  27. never_active is not None and (isinstance(never_active, bool) or not isinstance(never_active, (int, float))
  28. or never_active < 0)):
  29. raise RuntimeStoreError('invalid_params')
  30. filters = _filters(params['filters']) if 'filters' in params else None
  31. authority._require_admission_open()
  32. from gateway.session_runtime_workers import track_mutation
  33. task = track_mutation(authority, _apply(authority, filters, never_active))
  34. return await asyncio.shield(task)
  35. async def _apply(authority, filters, never_active):
  36. from hermes_constants import get_hermes_home
  37. from gateway.session_mutations import retire_live_sessions
  38. # Same transcript dir the CLI's local prune unlinks under (dispatch runs in the owner's scope).
  39. sessions_dir = Path(get_hermes_home()) / 'sessions'
  40. deleted_ids = []
  41. if filters is None:
  42. deleted, routing, skipped = await asyncio.to_thread(
  43. authority.db.prune_never_active_keyed_sessions, older_than_days=float(never_active),
  44. sessions_dir=sessions_dir, deleted_ids=deleted_ids)
  45. result = {'deleted': deleted, 'routing_deleted': routing, 'skipped': skipped}
  46. else:
  47. deleted = await asyncio.to_thread(
  48. authority.db.prune_sessions, sessions_dir=sessions_dir, exclude_active_write_guards=True,
  49. deleted_ids=deleted_ids, **filters)
  50. result = {'deleted': deleted}
  51. retire_live_sessions(authority, deleted_ids)
  52. return result