hermes_constants_scratch_detach.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. """The hourly scratch prune runs in a detached child process, not in the process that is due it.
  2. A pass can take seconds to minutes (the idle scan of a large tree, the ``rmtree`` of a multi-GB
  3. entry). Inline, it held every process start; on a daemon thread it died with every short-lived
  4. ``hermes`` command, the hourly stamp already spent. A detached child outlives the command that
  5. launched it, so each due pass runs to the end, with the same rules as an inline one.
  6. """
  7. from __future__ import annotations
  8. import logging
  9. import os
  10. import subprocess
  11. import sys
  12. from pathlib import Path
  13. import hermes_constants_scratch as rules
  14. logger = logging.getLogger(__name__)
  15. # ``-I``: neither the launcher's cwd (a project ``utils/`` would shadow ours) nor PYTHON* env vars
  16. # decide what the child imports; it gets the launcher's own ``sys.path`` (repo root, PM/venv
  17. # site-packages), not ``hermes_bootstrap``, whose boot hook would schedule another prune.
  18. _CHILD = ("import sys; sys.path[:] = sys.argv[3:]; "
  19. "import hermes_constants_scratch_detach as d; d.run(*sys.argv[1:3])")
  20. def spawn_prune(scratch: Path) -> None:
  21. """Start one detached prune of *scratch*, the path the caller resolved (a child never
  22. re-resolves the profile home). A failed spawn is logged and dropped: the stamp is already
  23. spent, so the next pass is the next process past the hourly mark.
  24. This process's ancestry is captured here, before the spawn: once this command and its
  25. parents exit, the child can no longer see that the user's shell was the caller's."""
  26. argv = [sys.executable, "-I"]
  27. if sys.dont_write_bytecode:
  28. argv.append("-B")
  29. if sys.pycache_prefix: # a sealed payload must never get __pycache__ writes
  30. argv += ["-X", f"pycache_prefix={sys.pycache_prefix}"]
  31. argv += ["-c", _CHILD, os.fspath(scratch), _encode_lineage(rules.lineage_with_start_times()), *sys.path]
  32. from hermes_cli._subprocess_compat import windows_detach_flags_without_breakaway, windows_detach_popen_kwargs
  33. stdio = {"stdin": subprocess.DEVNULL, "stdout": subprocess.DEVNULL, "stderr": subprocess.DEVNULL}
  34. try:
  35. try:
  36. subprocess.Popen(argv, close_fds=True, **stdio, **windows_detach_popen_kwargs())
  37. except PermissionError:
  38. if sys.platform != "win32":
  39. raise
  40. # A job object without BREAKAWAY_OK rejects CREATE_BREAKAWAY_FROM_JOB.
  41. subprocess.Popen(argv, close_fds=True, **stdio, creationflags=windows_detach_flags_without_breakaway())
  42. except (OSError, RuntimeError) as exc:
  43. logger.debug("scratch prune: could not start the detached pass for %s: %s", scratch, exc)
  44. def _encode_lineage(chain: list[tuple[int, float]] | None) -> str:
  45. # repr() round-trips a float exactly; "-" is "could not compute" (the child fails closed).
  46. return "-" if chain is None else ",".join(f"{pid}:{started!r}" for pid, started in chain)
  47. def run(scratch: str, lineage: str) -> None:
  48. """Child entry: one prune of *scratch*, sparing the launcher's ancestry. Any failure is one
  49. line in ``scratch-prune.log``; nothing is raised (no one reads this process's stderr)."""
  50. try:
  51. from hermes_constants import prune_scratch_dir
  52. rules._launcher_lineage = None if lineage == "-" else frozenset(
  53. (int(pid), float(started)) for pid, started in (item.split(":") for item in lineage.split(",")))
  54. prune_scratch_dir(Path(scratch))
  55. # health: allow BLE001 -- the detached child's top level: its stderr is /dev/null, so the audit log is the record
  56. except Exception as exc:
  57. audit = rules._open_prune_log(Path(scratch).parent.parent / "logs" / "scratch-prune.log")
  58. audit.info("scratch prune: detached pass failed (%s: %r)", type(exc).__name__, str(exc))
  59. if audit is not rules.logger:
  60. for handler in audit.handlers:
  61. handler.close()