process_registry_results.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """Bounded, profile-local receipts for completed terminal processes.
  2. Receipts are read through process_manage, never replayed as notifications or
  3. adopted as live PIDs. Each producer writes its own file so independent one-shot
  4. parents cannot overwrite each other's results in the running-PID checkpoint.
  5. """
  6. import json
  7. import logging
  8. import re
  9. import sqlite3
  10. import time
  11. from hermes_dir_acl import make_user_dir
  12. from hermes_constants import get_hermes_home
  13. from utils import atomic_json_write
  14. logger = logging.getLogger("tools.process_registry")
  15. RESULT_RETENTION_SECONDS = 7 * 24 * 60 * 60
  16. MAX_RETAINED_RESULTS = 64
  17. _RESULT_FIELDS = (
  18. "id", "command", "cwd", "task_id", "owner_task_id", "session_key",
  19. "parent_session_id", "started_at", "exit_code", "completion_reason",
  20. "termination_source", "notify_on_complete",
  21. )
  22. def _result_paths():
  23. """Prune by completion time, not start time (jobs can take days)."""
  24. directory = get_hermes_home() / "logs" / "process-results"
  25. cutoff = time.time() - RESULT_RETENTION_SECONDS
  26. retained = []
  27. for path in directory.glob("proc_*.json"):
  28. try:
  29. modified = path.stat().st_mtime
  30. if modified < cutoff:
  31. path.unlink(missing_ok=True)
  32. else:
  33. retained.append((modified, path))
  34. except FileNotFoundError:
  35. continue # Another producer pruned it.
  36. retained.sort(key=lambda item: (item[0], item[1].name), reverse=True)
  37. for _, path in retained[MAX_RETAINED_RESULTS:]:
  38. path.unlink(missing_ok=True)
  39. return [path for _, path in retained[:MAX_RETAINED_RESULTS]]
  40. def save_completed_result(session) -> None:
  41. from agent.redact import redact_sensitive_text, redact_terminal_output
  42. from tools.process_registry import MAX_OUTPUT_CHARS
  43. with session._lock:
  44. record = {key: getattr(session, key) for key in _RESULT_FIELDS}
  45. record["output"] = session.output_buffer[-MAX_OUTPUT_CHARS:]
  46. # Live-output opt-out must not persist raw credentials in durable receipts.
  47. record["output"] = redact_terminal_output(record["output"], record["command"], force=True)
  48. record["command"] = redact_sensitive_text(record["command"], code_file=True, force=True)
  49. directory = get_hermes_home() / "logs" / "process-results"
  50. try:
  51. from hermes_constants import assert_named_profile_home_live
  52. assert_named_profile_home_live(directory)
  53. make_user_dir(directory, mode=0o700, parents=True, exist_ok=True)
  54. atomic_json_write(directory / f"{session.id}.json", record, mode=0o600)
  55. _result_paths()
  56. except OSError:
  57. # Preserve live delivery on disk failure, but never silently claim durability.
  58. logger.warning("Could not retain completed process result %s", session.id, exc_info=True)
  59. def _owns_result(owner: str, parent: str | None) -> bool:
  60. if not parent:
  61. return False
  62. if owner == parent:
  63. return True
  64. from hermes_state import SessionDB
  65. # Pure lineage read on the hot path of every retained-result load; a writable open here
  66. # was one more writer handle per call inside the gateway (#100896).
  67. db = SessionDB(read_only=True)
  68. try:
  69. return db.get_compression_tip(parent) == owner
  70. finally:
  71. db.close()
  72. def load_completed_results(prefix: str = "") -> dict:
  73. """Restore read-only snapshots; no process handles, watchers, or queue events."""
  74. from tools.process_registry import ProcessSession
  75. from gateway.session_context import get_session_env
  76. owner = get_session_env("HERMES_SESSION_ID", "")
  77. if not owner:
  78. return {}
  79. results = {}
  80. try:
  81. paths = _result_paths()
  82. except OSError:
  83. logger.warning("Could not read retained process results", exc_info=True)
  84. return results
  85. for path in paths:
  86. if not path.stem.startswith(prefix):
  87. continue
  88. try:
  89. record = json.loads(path.read_text(encoding="utf-8-sig"))
  90. if record["id"] != path.stem or not re.fullmatch(r"proc_[\w]+", record["id"]):
  91. continue
  92. if not _owns_result(owner, record.get("parent_session_id")):
  93. continue
  94. session = ProcessSession(
  95. **{key: record[key] for key in _RESULT_FIELDS},
  96. exited=True, output_buffer=record["output"],
  97. )
  98. session._completion_event.set()
  99. results[session.id] = session
  100. except (OSError, ValueError, KeyError, TypeError, sqlite3.Error):
  101. logger.debug("Skipping unreadable process result %s", path.name, exc_info=True)
  102. return results