Files
hermes-agent/tests/tools/test_git_consumer_environment.py
alt-glitch 458a0046ad refactor(agent): six checkpoint files move from tools/ to agent/checkpoints/
tools/ holds model tools. These six files register none: they snapshot and
restore a working directory for the agent loop, /rollback, /undo's
changed-files line and `hermes checkpoints`. They move next to the agent
loop that takes the checkpoints:

  tools/checkpoint_manager.py                -> agent/checkpoints/manager.py
  tools/checkpoint_maintenance.py            -> agent/checkpoints/maintenance.py
  tools/checkpoint_pruning.py                -> agent/checkpoints/pruning.py
  tools/checkpoint_refs.py                   -> agent/checkpoints/refs.py
  tools/checkpoint_undo_hint.py              -> agent/checkpoints/undo_hint.py
  tools/checkpoint_manager_profile_rename.py -> agent/checkpoints/manager_profile_rename.py

agent/checkpoints/__init__.py is a docstring only. Nine test files move to
tests/agent/checkpoints/. Every importer changes its import line in place,
inside the same function. Nothing stays at the old paths: none of the six
names is on the plugin API list, the shipped-release list or the
old-updater list.

Behaviour is unchanged. The four loggers keep their tools.checkpoint_*
names, so the lines stay under `hermes logs --component tools`. The folder
is not a root checkpoints/ package because the checkpoint store is
HERMES_HOME/checkpoints, which is the same directory as such a package on an
install whose checkout root is HERMES_HOME. agent.* is already packaged, so
pyproject.toml and the lists of root packages do not change.

The import and docs rewrites were made by scripts/move/rewrite_imports.py
and rewrite_docs.py.
2026-11-16 19:21:55 -06:00

91 lines
3.4 KiB
Python

"""Git consumers use the selected package environment without owning its layout."""
import importlib
import shutil
import subprocess
import pytest
import pm
from agent.checkpoints import manager as checkpoint_manager
from tools import working_diff
@pytest.mark.platforms("posix")
def test_checkpoint_and_diff_follow_selected_git_environment(tmp_path, monkeypatch):
git = shutil.which("git")
assert git is not None
selected = tmp_path / "selected-tools"
selected.mkdir()
observed = tmp_path / "git-environment"
wrapper = selected / "git"
import shlex
wrapper.write_text(
'#!/bin/sh\n'
'test "$HERMES_GIT_HELPER" = "selected" || exit 77\n'
f'printf "%s\\n" selected >> {shlex.quote(str(observed))}\n'
f'exec {shlex.quote(git)} "$@"\n',
encoding="utf-8",
)
wrapper.chmod(0o755)
repo = tmp_path / "repo"
repo.mkdir()
subprocess.run([git, "init", str(repo)], check=True, capture_output=True)
(repo / "new.txt").write_text("new content\n", encoding="utf-8")
def selected_git(name, *, base_env):
assert name == "git"
return pm.Runner(name, {
**base_env, "PATH": str(selected), "HERMES_GIT_HELPER": "selected"
})
monkeypatch.setattr(pm, "ensure", selected_git)
monkeypatch.setenv("PATH", str(tmp_path / "missing-path"))
manager = checkpoint_manager.CheckpointManager(enabled=True)
assert manager.ensure_checkpoint(str(repo)) is True
result = working_diff.collect_working_diff(str(repo))
assert result["success"] is True
assert "+new content" in result["diff"]
assert observed.read_text(encoding="utf-8").splitlines()
# A changed PM selection must affect this same process, not a cached command.
monkeypatch.setattr(pm, "ensure", lambda name, *, base_env: pm.Runner(name, {
**base_env, "PATH": str(tmp_path / "missing-path")
}))
assert working_diff.collect_working_diff(str(repo))["success"] is False
manager.new_turn()
assert manager.ensure_checkpoint(str(repo)) is False
@pytest.mark.parametrize("consumer", ["checkpoint", "diff"])
@pytest.mark.parametrize("failure", ["unsupported-target", "acquisition"])
def test_git_consumers_fall_back_to_system_git(tmp_path, monkeypatch, consumer, failure):
git = shutil.which("git")
assert git is not None
repo = tmp_path / "repo"
subprocess.run([git, "init", str(repo)], check=True, capture_output=True)
(repo / "new.txt").write_text("system git content\n", encoding="utf-8")
# Fail at the PM boundary reached by both ensure and env_for, without
# pretending this interpreter is running on a different host OS/CPU.
resolver = importlib.import_module("pm.install")
calls = []
def unavailable(*args, **kwargs):
calls.append(True)
if failure == "unsupported-target":
raise RuntimeError("unsupported architecture")
raise pm.InstallError("git", "managed Git unavailable")
boundary = "current_target" if failure == "unsupported-target" else "_installed_location"
monkeypatch.setattr(resolver, boundary, unavailable)
if consumer == "checkpoint":
manager = checkpoint_manager.CheckpointManager(enabled=True)
assert manager.ensure_checkpoint(str(repo)) is True
else:
result = working_diff.collect_working_diff(str(repo))
assert result["success"] is True
assert "+system git content" in result["diff"]
assert calls