update_cmd_fleet_checkout.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """Checkout ancestry for the update-restart obligation (``update_cmd_fleet`` sibling).
  2. The obligation records the SHA a pull landed on; the checkout may later sit past it by a carried
  3. local commit (a cherry-picked hotfix). Whether that recorded SHA is still *contained* in HEAD is
  4. the question these readers ask, so the live fleet can be held to the code on disk (#119367).
  5. """
  6. from __future__ import annotations
  7. import logging
  8. import subprocess
  9. from hermes_cli._subprocess_compat import NO_LAZY_FETCH_ENV, noninteractive_git_env
  10. logger = logging.getLogger(__name__)
  11. def checkout_contains(sha: str) -> bool:
  12. """True when ``sha`` is an ancestor of (or equal to) the code this checkout runs; False on any
  13. probe failure.
  14. Fail-closed on purpose: an unknown ancestry is not evidence that the fleet serves the update.
  15. A Docker/Cloud image has no ``.git``; its identity is the baked build stamp
  16. (``build_info.get_code_identity`` → ``source == "build-file"``). There is no history to walk, so
  17. "contained" collapses to "equal to the stamp" — without this the probe was always False on an
  18. image and the pending-restart catch-up printed "every gateway serves the checkout" and "still
  19. off the checkout code" in the same breath.
  20. """
  21. from hermes_cli.build_info import get_code_identity
  22. from hermes_cli.update_cmd import _m
  23. identity = get_code_identity() or {}
  24. if identity.get("source") == "build-file":
  25. stamped = str(identity.get("sha") or "")
  26. return bool(stamped) and (stamped == sha or stamped.startswith(sha) or sha.startswith(stamped))
  27. try:
  28. from hermes_cli.update_custody import run_git
  29. result = run_git(
  30. ["git"], ["merge-base", "--is-ancestor", sha, "HEAD"],
  31. cwd=_m().PROJECT_ROOT, capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=10,
  32. # A SHA this checkout lacks is not contained; asking the promisor remote for it would
  33. # download that commit's entire history (on-demand fetches send no "have" lines).
  34. env={**noninteractive_git_env(), **NO_LAZY_FETCH_ENV},
  35. )
  36. return result.returncode == 0
  37. except Exception as exc:
  38. logger.debug("Checkout ancestry probe for %s failed: %s", sha[:10], exc)
  39. return False