Skip to content

The agent that grows with you

fix(update): the partial-clone pack tidy's merge runs in the update's custody

Teknium9a21cc168d32
diff --git a/hermes_cli/_subprocess_compat.py b/hermes_cli/_subprocess_compat.py
index 5193f7e6451..57341530ce3 100644
--- a/hermes_cli/_subprocess_compat.py
+++ b/hermes_cli/_subprocess_compat.py
@@ -798,12 +798,10 @@ def bounded_probe_run(
argv: Sequence[str], *, timeout: float, errors: str = "replace",
env: "Mapping[str, str] | None" = None, cwd: "str | os.PathLike[str] | None" = None,
raise_on_spawn_failure: bool = False, input: "str | None" = None,
- popen_kwargs: "Mapping[str, object] | None" = None,
) -> "subprocess.CompletedProcess[str] | None":
"""Deadlock-safe ``subprocess.run(argv, capture_output=True, timeout=…)`` for fail-open probes.
``input`` is written to the child's stdin (closed afterwards); without it stdin is ``DEVNULL``.
- ``popen_kwargs`` adds Popen arguments (the update's custody: ``pass_fds``/``preexec_fn``).
Returns a ``CompletedProcess`` when the child finished within *timeout* (any exit code), or
``None`` on spawn failure or timeout. With ``raise_on_spawn_failure=True`` the ``Popen``
@@ -818,7 +816,6 @@ def bounded_probe_run(
machines (#87134); the git probes hit it first (#68609 / #66037).
"""
_popen_kwargs: dict = {"creationflags": windows_hide_flags()} if IS_WINDOWS else {"process_group": 0}
- _popen_kwargs.update(popen_kwargs or {})
job = None
try:
# Windows: contain the probe in a Job Object. `taskkill /T` walks LIVE parent pids, and a
diff --git a/hermes_cli/git_pack_tidy.py b/hermes_cli/git_pack_tidy.py
index d1f3128fd6a..92f321aefef 100644
--- a/hermes_cli/git_pack_tidy.py
+++ b/hermes_cli/git_pack_tidy.py
@@ -25,6 +25,7 @@ import mmap
import os
import shutil
import struct
+import subprocess
import time
from dataclasses import dataclass
from pathlib import Path
@@ -32,7 +33,6 @@ from typing import Dict, Iterable, Iterator, List, Optional
from hermes_cli._subprocess_compat import (
NO_LAZY_FETCH_ENV,
- bounded_probe_run,
noninteractive_git_env,
windows_hide_flags,
)
@@ -266,6 +266,26 @@ def _erase_redundant_packs(pack_dir: Path, deadline: float, result: TidyResult,
index.close()
+def _merge_git(repo_root: Path, staging: Path, batch: List[Path], timeout: float):
+ """``git pack-objects --stdin-packs`` over *batch* into *staging*, in the update's custody (a
+ mutator: it holds the checkout lock fd, and inside an update it is job-bound on Windows).
+
+ ``None`` when it ran out of time or could not start; a custody refusal propagates, so an update
+ stops the way it does for any updater git Windows will not bind."""
+ from hermes_cli.update_custody import CustodyRefused, run_git
+
+ try:
+ return run_git(
+ ["git"], ["-c", "pack.packSizeLimit=0", "pack-objects", "-q", "--stdin-packs", str(staging / "pack")],
+ cwd=str(repo_root), env=_git_env(), input="".join(p.name + "\n" for p in batch),
+ capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=timeout,
+ creationflags=windows_hide_flags())
+ except CustodyRefused:
+ raise
+ except (OSError, subprocess.SubprocessError):
+ return None
+
+
def _merge_smallest_packs(repo_root: Path, pack_dir: Path, deadline: float, result: TidyResult,
state: dict) -> None:
"""Merge the smallest promisor packs, a batch at a time, until the count is under target."""
@@ -290,10 +310,7 @@ def _merge_smallest_packs(repo_root: Path, pack_dir: Path, deadline: float, resu
try:
# packSizeLimit=0 whatever the user's config says: inputs retire only after everything
# they held is published, and that is one output pack.
- done = bounded_probe_run(
- ["git", "-c", "pack.packSizeLimit=0", "pack-objects", "-q", "--stdin-packs", str(staging / "pack")],
- timeout=left, cwd=str(repo_root), env=_git_env(),
- input="".join(p.name + "\n" for p in batch))
+ done = _merge_git(repo_root, staging, batch, left)
if done is None:
result.out_of_time = True
state["merge_bytes"] = max(state["merge_bytes"] // 2, 1) # next update tries a smaller batch
diff --git a/hermes_cli/update_custody.py b/hermes_cli/update_custody.py
index 291d4b0dbd3..df267fe74ad 100644
--- a/hermes_cli/update_custody.py
+++ b/hermes_cli/update_custody.py
@@ -56,11 +56,12 @@ _MUTATOR_CONFIG = ("-c", "core.fsmonitor=false", "-c", "credential.helper=")
# git subcommands that write the worktree, the index or refs on THIS machine. Only these inherit
# the checkout lock fd: if the updater dies mid-command, the checkout stays locked until git exits.
# No `pull` (fetch + merge: its network half must not hold the fd; the updater fetches, then
-# merges). `gc` packs refs (and its repack writes the object store).
+# merges). `gc` packs refs (and its repack writes the object store); `pack-objects` is the pack
+# tidy's merge, whose output the update moves into the object store.
LOCAL_MUTATORS = frozenset({
"add", "am", "apply", "checkout", "checkout-index", "cherry-pick", "clean", "commit", "gc",
- "merge", "mv", "read-tree", "rebase", "reset", "restore", "revert", "rm", "stash", "switch",
- "update-index", "update-ref", "symbolic-ref", "tag", "branch", "worktree",
+ "merge", "mv", "pack-objects", "read-tree", "rebase", "reset", "restore", "revert", "rm", "stash",
+ "switch", "update-index", "update-ref", "symbolic-ref", "tag", "branch", "worktree",
})
# Mutators that move the checkout to another commit: what they read from a promisor remote is
# fetched first, without the fd (_prefetch_for_move).
diff --git a/tests/hermes_cli/test_git_pack_tidy.py b/tests/hermes_cli/test_git_pack_tidy.py
index c3c0ab1c2c2..1d4c22b081f 100644
--- a/tests/hermes_cli/test_git_pack_tidy.py
+++ b/tests/hermes_cli/test_git_pack_tidy.py
@@ -118,11 +118,20 @@ def test_merges_down_to_the_target_into_one_pack_whatever_the_size_limit(
_age(repo)
held = _objects(repo)
monkeypatch.setattr(tidy, "PACK_COUNT_TARGET", 1)
+ # The merge writes the object store during `hermes update`: it is an updater git, so it runs in
+ # the update's custody holding the checkout lock fd (a killed update's orphan keeps the lock).
+ from hermes_cli import update_custody
+
+ spawned, real_run = [], update_custody.run
+ monkeypatch.setattr(update_custody, "run", lambda argv, *, inherit_lock=False, **kw: (
+ spawned.append((update_custody.git_subcommand(list(argv)[1:]), inherit_lock))
+ or real_run(argv, inherit_lock=inherit_lock, **kw)))
result = tidy.tidy_partial_clone_packs(repo)
assert result.packs_left == 1 and _packs(repo)[0].with_suffix(".promisor").exists()
assert _objects(repo) == held
+ assert ("pack-objects", True) in spawned
_KILLED_MID_ERASE = """