Skip to content

The agent that grows with you

fix(update): resolve code identity by reading .git directly — no subprocess

Teknium1acbeed1461e
diff --git a/hermes_cli/build_info.py b/hermes_cli/build_info.py
index 03fb7ac4d00..e2ae06ba73e 100644
--- a/hermes_cli/build_info.py
+++ b/hermes_cli/build_info.py
@@ -36,6 +36,68 @@ _BUILD_SHA_FILE = Path(__file__).parent.parent / ".hermes_build_sha"
_code_identity_cache: Optional[dict] = None
+def _resolve_git_head_sha(project_root: Path) -> Optional[str]:
+ """Resolve the checkout's HEAD commit sha by reading .git directly.
+
+ Deliberately NOT ``git rev-parse`` in a subprocess: this helper runs
+ inside library paths (gateway runtime-status writes, update receipts)
+ where spawning processes is both slow and hostile to tests that mock
+ ``subprocess.run`` tightly (call-count asserts, sequenced side effects).
+ Handles regular checkouts, worktrees/submodules (``.git`` file with a
+ ``gitdir:`` pointer + ``commondir``), loose refs, and packed-refs.
+ Returns None on any failure.
+ """
+ try:
+ git_path = project_root / ".git"
+ if git_path.is_file():
+ # Worktree/submodule: ".git" is a pointer file.
+ pointer = git_path.read_text(encoding="utf-8", errors="replace").strip()
+ if not pointer.startswith("gitdir:"):
+ return None
+ git_dir = Path(pointer[len("gitdir:"):].strip())
+ if not git_dir.is_absolute():
+ git_dir = (project_root / git_dir).resolve()
+ elif git_path.is_dir():
+ git_dir = git_path
+ else:
+ return None
+
+ # Refs live in the COMMON git dir for worktrees.
+ common_dir = git_dir
+ commondir_file = git_dir / "commondir"
+ if commondir_file.is_file():
+ rel = commondir_file.read_text(encoding="utf-8", errors="replace").strip()
+ common = Path(rel)
+ if not common.is_absolute():
+ common = (git_dir / common).resolve()
+ common_dir = common
+
+ head = (git_dir / "HEAD").read_text(encoding="utf-8", errors="replace").strip()
+ if not head.startswith("ref:"):
+ # Detached HEAD: the file holds the sha itself.
+ return head if len(head) == 40 else None
+ ref_name = head[len("ref:"):].strip()
+
+ loose = common_dir / ref_name
+ if loose.is_file():
+ sha = loose.read_text(encoding="utf-8", errors="replace").strip()
+ return sha if len(sha) == 40 else None
+
+ packed = common_dir / "packed-refs"
+ if packed.is_file():
+ for line in packed.read_text(encoding="utf-8", errors="replace").splitlines():
+ line = line.strip()
+ if not line or line.startswith(("#", "^")):
+ continue
+ parts = line.split(" ", 1)
+ if len(parts) == 2 and parts[1].strip() == ref_name:
+ sha = parts[0].strip()
+ return sha if len(sha) == 40 else None
+ except Exception:
+ return None
+ return None
+
+
def get_code_identity(refresh: bool = False) -> dict:
"""Return the running checkout's code identity as a dict.
@@ -59,24 +121,10 @@ def get_code_identity(refresh: bool = False) -> dict:
sha: Optional[str] = None
source = "unknown"
project_root = Path(__file__).parent.parent
- try:
- import subprocess
-
- proc = subprocess.run(
- ["git", "rev-parse", "HEAD"],
- cwd=project_root,
- capture_output=True,
- text=True,
- encoding="utf-8",
- errors="replace",
- timeout=10,
- )
- candidate = (proc.stdout or "").strip()
- if proc.returncode == 0 and candidate:
- sha = candidate
- source = "git"
- except Exception:
- pass
+ resolved = _resolve_git_head_sha(project_root)
+ if resolved:
+ sha = resolved
+ source = "git"
if sha is None:
baked = get_build_sha(short=0)
if baked:
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index 4608e3f23f9..52cbefda443 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -6428,6 +6428,11 @@ def _cmd_update_impl(args, gateway_mode: bool):
# needed to forward already-restarted units to
# ``_finish_dashboard_update_cleanup`` (review on #83595).
restarted_services: list = []
+ # Same outside-the-try treatment: the post-restart fleet version
+ # check consults killed_pids to decide whether to wait for
+ # freshly-restarted gateways to settle, and the phase's except
+ # path forwards it to the update receipt.
+ killed_pids: set = set()
# Auto-restart ALL gateways after update.
# The code update (git pull) is shared across all profiles, so every
@@ -7271,7 +7276,10 @@ def _cmd_update_impl(args, gateway_mode: bool):
# A brief settle window: freshly restarted gateways need a
# moment to rewrite gateway_state.json with their new identity.
- _time.sleep(2.0)
+ # Skipped when the restart phase touched nothing (no gateways
+ # were running) — nothing to settle.
+ if restarted_services or killed_pids:
+ _time.sleep(2.0)
_fleet_snapshot = collect_fleet_versions()
if print_fleet_version_matrix(_fleet_snapshot):
gateway_fleet_restart_incomplete = True