diff --git a/cron/scheduler.py b/cron/scheduler.py index 920006563f..715eabe8bc 100644 --- a/cron/scheduler.py +++ b/cron/scheduler.py @@ -3981,6 +3981,13 @@ def _worktree_maintenance_repos() -> list[str]: """Repos whose ``.worktrees/`` to keep pruned: the hermes checkout, job workdir repo roots and every repo this profile created a worktree in (``worktree_registry``), filtered to those that actually have a ``.worktrees/`` dir.""" + from hermes_platform.resolver.git import resolve_usable_git + + git = resolve_usable_git() + if git is None: + # The prune runs git in every repo found here: with no usable git (missing, or macOS's CLT + # stub, whose every launch opens Apple's install dialog) there is nothing to sweep. + return [] repos: set = set() with contextlib.suppress(Exception): @@ -3995,10 +4002,8 @@ def _worktree_maintenance_repos() -> list[str]: with contextlib.suppress(Exception): from cron.jobs import load_jobs - from hermes_platform.resolver.git import resolve_usable_git - git = resolve_usable_git() - for job in load_jobs() if git else (): + for job in load_jobs(): workdir = str(job.get("workdir") or "").strip() if not workdir or not Path(workdir).is_dir(): continue diff --git a/tests/cron/test_worktree_maintenance_git.py b/tests/cron/test_worktree_maintenance_git.py index ccbe9bf190..513b37ebfb 100644 --- a/tests/cron/test_worktree_maintenance_git.py +++ b/tests/cron/test_worktree_maintenance_git.py @@ -5,12 +5,14 @@ import os import pytest from cron import jobs, scheduler +from cron.scheduler_worktree_maintenance import run_sweep from hermes_cli import worktree_registry from hermes_platform.resolver import git as git_resolver -@pytest.mark.platforms("posix") -def test_cron_discovery_does_not_launch_uninstalled_apple_git(tmp_path, monkeypatch): +@pytest.fixture +def stub_git_log(tmp_path, monkeypatch): + """Make the PATH's only git a trap that logs each launch, marked as the CLT stub.""" spawn_log = tmp_path / "git_spawns.log" trap_git = tmp_path / "apple_usr_bin" / "git" trap_git.parent.mkdir() @@ -25,12 +27,30 @@ def test_cron_discovery_does_not_launch_uninstalled_apple_git(tmp_path, monkeypa lambda exe, env=None, **kw: bool(exe) and os.path.realpath(exe) != trap and real_check(exe, env, **kw), ) monkeypatch.setenv("PATH", str(trap_git.parent)) + monkeypatch.setattr(scheduler, "project_root_str", lambda: str(tmp_path)) + assert git_resolver.inspect_git(cached=False).status == "clt_stub" + return spawn_log + + +@pytest.mark.platforms("posix") +def test_cron_discovery_does_not_launch_uninstalled_apple_git(tmp_path, monkeypatch, stub_git_log): workdir = tmp_path / "job" workdir.mkdir() monkeypatch.setattr(worktree_registry, "registered_repos", list) monkeypatch.setattr(jobs, "load_jobs", lambda: [{"workdir": str(workdir)}]) - monkeypatch.setattr(scheduler, "project_root_str", lambda: str(tmp_path)) - assert git_resolver.inspect_git(cached=False).status == "clt_stub" assert scheduler._worktree_maintenance_repos() == [] - assert not spawn_log.exists(), f"git stub was spawned: {spawn_log.read_text()}" + assert not stub_git_log.exists(), f"git stub was spawned: {stub_git_log.read_text()}" + + +@pytest.mark.platforms("posix") +def test_cron_sweep_of_a_registered_repo_does_not_launch_uninstalled_apple_git(tmp_path, monkeypatch, stub_git_log): + """A repo a worktree was made in earlier (before a macOS upgrade dropped the Command Line Tools) + reaches the six-hourly prune without any job probe.""" + repo = tmp_path / "repo" + (repo / ".worktrees").mkdir(parents=True) + monkeypatch.setattr(worktree_registry, "registered_repos", lambda: [str(repo)]) + monkeypatch.setattr(jobs, "load_jobs", list) + + run_sweep(scheduler._worktree_maintenance_repos, interval_seconds=3600.0) + assert not stub_git_log.exists(), f"git stub was spawned: {stub_git_log.read_text()}"