run_housekeeping_disk.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. """Gateway housekeeping chores that keep the data disk healthy: stale media-cache cleanup and the
  2. home-channel prompt for the low-disk auto-prune decision (see ``hermes_cli/prune_advisor.py``).
  3. A headless gateway on a small VPS is where a disk fills up, and its owner may never open the CLI,
  4. so the question goes to each served profile's home channel; Desktop and Ink TUI viewers of local
  5. sessions get it as a notice. It is posted at most once per
  6. snooze window per profile: posting records the snooze, and ``/autoprune on|off`` ends it for good.
  7. Runs from the housekeeping thread; the send is scheduled onto the gateway loop.
  8. """
  9. from __future__ import annotations
  10. import contextlib
  11. import logging
  12. from pathlib import Path
  13. logger = logging.getLogger(__name__)
  14. def housekeeping_media_caches() -> None:
  15. """Hourly media-cache prune (24h max age); spillover (None) keeps the session retention window."""
  16. from gateway.platforms.base import (
  17. cleanup_audio_cache, cleanup_document_cache, cleanup_image_cache, cleanup_screenshot_cache,
  18. cleanup_video_cache)
  19. from tools.tool_result_storage import cleanup_spillover_cache
  20. from tools.environments.local import cleanup_terminal_temp_cache
  21. from tools.bot_mode_dm import cleanup_bot_dm_cache
  22. from tools.bot_relay import cleanup_bot_relay_artifacts
  23. from agent.provider_media import MEDIA_CACHE_MAX_AGE_HOURS
  24. from gateway.run import _housekeeping_chore
  25. for cache_name, cleanup_fn in (
  26. ("Image", cleanup_image_cache), ("Document", cleanup_document_cache),
  27. ("Audio", cleanup_audio_cache), ("Video", cleanup_video_cache),
  28. ("Screenshot", cleanup_screenshot_cache), ("Spillover", cleanup_spillover_cache),
  29. ("Terminal temp", cleanup_terminal_temp_cache), ("Bot DM", cleanup_bot_dm_cache),
  30. ("Bot relay", cleanup_bot_relay_artifacts)):
  31. def _one(name=cache_name, fn=cleanup_fn):
  32. removed = fn(max_age_hours=None if fn is cleanup_spillover_cache else MEDIA_CACHE_MAX_AGE_HOURS)
  33. if removed:
  34. logger.info("%s cache cleanup: removed %d stale file(s)", name, removed)
  35. _housekeeping_chore(f"{cache_name} cache cleanup", _one)
  36. def maybe_post_auto_prune_prompt(runner, loop) -> None:
  37. """One housekeeping tick: post the prompt to every served profile that has a home channel, a
  38. low disk and no decision yet. Cheap when nothing is due (two syscalls + a YAML read per
  39. profile); the database estimate runs only for a profile that can actually receive it."""
  40. if runner is None or loop is None:
  41. return
  42. from agent.async_utils import safe_schedule_threadsafe
  43. from hermes_cli import prune_advisor
  44. from hermes_constants import get_routing_process_hermes_home
  45. reachable = {profile for profile, *_rest in runner._served_home_channel_transports()}
  46. homes: dict = {None: get_routing_process_hermes_home()}
  47. homes.update(getattr(runner, "_served_profile_homes", None) or {})
  48. for profile, home in homes.items():
  49. if profile not in reachable or prune_advisor.should_ask(Path(home)) is None:
  50. continue
  51. advice = prune_advisor.advise(Path(home))
  52. if advice is None:
  53. continue
  54. fut = safe_schedule_threadsafe(_post(runner, profile, home, advice), loop, logger=logger,
  55. log_message="Auto-prune prompt scheduling error")
  56. # Snooze only on a delivered post: a send that failed must not swallow the question.
  57. if fut is not None and fut.result(timeout=60):
  58. prune_advisor.snooze(Path(home))
  59. _LOCAL_NOTIFIED: dict[str, float] = {}
  60. async def publish_local_auto_prune_notice(authority, ref) -> None:
  61. """Desktop / Ink TUI sessions (``AuthorityConnection.resume``): one sticky ``notification.show``
  62. per profile per snooze window while the disk is low and auto-prune is undecided. The classic
  63. CLI asks at its own startup, so its sessions are skipped here."""
  64. import asyncio
  65. import time
  66. from gateway.config import Platform
  67. from gateway.session_policy import policy_for_source
  68. from hermes_cli import prune_advisor
  69. from hermes_state_runtime import RuntimeStoreError
  70. live = authority.sessions.get(ref.session_id)
  71. source = getattr(live, "source", None)
  72. if source is None or source.platform != Platform.LOCAL:
  73. return
  74. try:
  75. policy = policy_for_source(authority.runner, source)
  76. except RuntimeStoreError: # policy not restored yet: this viewer gets no notice, the next one may
  77. return
  78. if policy is None or policy.source not in {"tui", "gui"}:
  79. return
  80. home = Path(authority.db.db_path).parent
  81. if not prune_advisor.notice_due(home, _LOCAL_NOTIFIED):
  82. return
  83. _LOCAL_NOTIFIED[str(home)] = time.time() # in-flight claim: one estimate per home at a time
  84. advice = await asyncio.to_thread(prune_advisor.advise, home)
  85. # Answered (here or in another window) during the estimate: publishing would re-ask it.
  86. delivered = (advice is not None and prune_advisor.should_ask(home) is not None
  87. and live.event_stream.publish(ref.session_id, prune_advisor.notice_payload(advice),
  88. event_type="notification.show"))
  89. if not delivered:
  90. _LOCAL_NOTIFIED.pop(str(home), None) # nobody saw it: the next window may ask
  91. def clear_local_auto_prune_notice(runner, source) -> None:
  92. """After /autoprune is answered on a local session, drop its sticky notice for that viewer."""
  93. from gateway.config import Platform
  94. from gateway.session_authorities import active_authority
  95. from hermes_cli.prune_advisor import NOTICE_KEY
  96. if getattr(source, "platform", None) != Platform.LOCAL:
  97. return
  98. authority = active_authority(runner)
  99. live = authority.sessions.get(source.chat_id) if authority is not None else None
  100. if live is not None:
  101. live.event_stream.publish(source.chat_id, {"key": NOTICE_KEY}, event_type="notification.clear")
  102. async def _post(runner, profile, home, advice) -> bool:
  103. """Send to the profile's home channels; True when at least one send was delivered."""
  104. from gateway.run import _async_profile_runtime_scope
  105. from gateway.warning_notifications import present_notification
  106. from hermes_cli.prune_advisor import advice_lines
  107. delivered = False
  108. scope = _async_profile_runtime_scope(Path(home)) if profile is not None else contextlib.nullcontext()
  109. async with scope:
  110. for target_profile, platform, _cfg, home_ch, transport in list(runner._served_home_channel_transports()):
  111. if target_profile != profile:
  112. continue
  113. prefix = getattr(transport.adapter, "typed_command_prefix", "/") if transport.adapter else "/"
  114. message = "⚠ " + "\n".join(advice_lines(advice, command_prefix=prefix))
  115. sent: list[bool] = []
  116. async def _send(platform=platform, home_ch=home_ch, transport=transport, message=message):
  117. sent.append(await runner._send_home_channel_message(
  118. platform, home_ch, transport, message, "Auto-prune prompt failed for %s:%s: %s"))
  119. await present_notification(_send, platform=platform)
  120. delivered = delivered or any(sent)
  121. return delivered