Files
hermes-agent/tests/hermes_cli/test_post_update_adoption.py
ethernet c13ea774e6 refactor: make install-stamp.json the single runtime version identity
Runtime identity resolved through hermes_cli.__version__ (a static 0.0.0
on source installs, rewritten by release stamping) leaked v0.0.0 into
About, /api/health, User-Agents, and plugin compat, and source updates
showed "couldn't reach update server" because identity and channel
authority disagreed with the checkout.

Now: get_version_info() resolves install stamp -> live git -> unknown,
never pyproject metadata, never a package constant. Source checkouts
derive identity from their reachable release tag; the completion tail of
every successful install/update/historical takeover atomically rewrites
install-stamp.json with that identity; a stale source stamp whose commit
no longer matches HEAD defers to live git. ACP/TUI use derived_version
for display and base_version for protocol fields; all ~44 runtime
__version__ consumers migrated; hermes_cli.__version__ and generated
_version.py are gone; release stamping only touches the native manifests
external builders consume (nix/tauri/cargo) and passes release identity
straight into write_install_stamp.py; pyproject.toml stays inert 0.0.0.
Desktop no longer synthesizes a competing install-stamp.json: the
checkout owns its stamp, and desktop-bootstrap classification keys on
the bootstrap-complete marker. verify-bootstrap-version-stamp.py now
cross-checks the checkout's stamp (baseVersion + commit == HEAD).

Validation: 31-file focused suite green (version identity, stamping,
adoption, providers, gateway, acp/tui runtime identity, api server via
extras env, release graph); desktop tsc + 25 vitest green; real-repo
probe: base=unknown derived=git.0635606.dirty source=git on this
checkout; clean-env imports resolve entirely from this tree; windows
footgun + compat-pointer scans clean.
2026-09-23 11:41:01 -04:00

144 lines
5.5 KiB
Python

"""step_adopt_blessed_checkout — one-time birth certificate for shipped
stampless installs.
Blessed root + .git + no stamp → write {updateMechanism: self} atomically.
Anything else — untouched. A read-only tree fails soft (nix-like layouts).
"""
import json
import os
import stat
import sys
import pytest
from hermes_cli.post_update import step_adopt_blessed_checkout
@pytest.fixture
def blessed_checkout(tmp_path, monkeypatch):
"""A .git checkout at the blessed $HERMES_HOME/hermes-agent root."""
home = tmp_path / ".hermes"
root = home / "hermes-agent"
root.mkdir(parents=True)
(root / ".git").mkdir()
monkeypatch.setenv("HERMES_HOME", str(home))
monkeypatch.setenv("HERMES_INSTALL_ROOT", str(root))
return root
def test_adopts_blessed_stampless_checkout(blessed_checkout):
result = step_adopt_blessed_checkout()
assert result["ok"] is True
assert result.get("adopted") == str(blessed_checkout)
stamp = json.loads((blessed_checkout / "install-stamp.json").read_text())
assert stamp["updateMechanism"] == "self"
assert stamp["source"] == "adoption"
def test_adoption_is_idempotent(blessed_checkout):
"""Second run: the stamp already exists, nothing rewritten."""
step_adopt_blessed_checkout()
first = (blessed_checkout / "install-stamp.json").read_text()
result = step_adopt_blessed_checkout()
assert result.get("skipped") == "already-stamped"
assert (blessed_checkout / "install-stamp.json").read_text() == first
def test_existing_stamp_is_never_touched(blessed_checkout):
"""Any pre-existing stamp wins — adoption must not clobber richer
provenance (an installer-written or build-baked stamp)."""
original = json.dumps({"schemaVersion": 2, "updateMechanism": "self", "source": "installer"})
(blessed_checkout / "install-stamp.json").write_text(original)
result = step_adopt_blessed_checkout()
assert result.get("skipped") == "already-stamped"
assert (blessed_checkout / "install-stamp.json").read_text() == original
def test_non_blessed_checkout_untouched(tmp_path, monkeypatch):
""".git anywhere else → never adopted."""
home = tmp_path / ".hermes"
home.mkdir()
dev = tmp_path / "src" / "hermes-agent"
dev.mkdir(parents=True)
(dev / ".git").mkdir()
monkeypatch.setenv("HERMES_HOME", str(home))
monkeypatch.setenv("HERMES_INSTALL_ROOT", str(dev))
result = step_adopt_blessed_checkout()
assert result.get("skipped") == "not-a-blessed-root"
assert not (dev / "install-stamp.json").exists()
def test_sealed_tree_untouched(tmp_path, monkeypatch):
"""No .git → not a checkout, nothing to adopt (sealed trees always
ship stamps anyway)."""
home = tmp_path / ".hermes"
root = home / "hermes-agent"
root.mkdir(parents=True)
monkeypatch.setenv("HERMES_HOME", str(home))
monkeypatch.setenv("HERMES_INSTALL_ROOT", str(root))
result = step_adopt_blessed_checkout()
assert result.get("skipped") == "not-a-checkout"
assert not (root / "install-stamp.json").exists()
@pytest.mark.platforms("posix")
@pytest.mark.skipif(getattr(os, "geteuid", lambda: 1)() == 0, reason='chmod-based read-only dirs are not enforceable on Windows or as root')
def test_read_only_tree_fails_soft(blessed_checkout):
"""nix-like read-only tree: debug-log skip, never a crash."""
mode = stat.S_IMODE(os.stat(blessed_checkout).st_mode)
os.chmod(blessed_checkout, stat.S_IRUSR | stat.S_IXUSR)
try:
result = step_adopt_blessed_checkout()
assert result["ok"] is True
assert str(result.get("skipped", "")).startswith("unwritable")
finally:
os.chmod(blessed_checkout, mode)
def test_adoption_keeps_the_steward_verdict_checkout(blessed_checkout):
"""End to end: the adopted stamp changes update admission, never the
steward ladder — a .git tree stays a checkout (sealed_steward None)."""
from hermes_cli.steward import sealed_steward
assert sealed_steward(blessed_checkout) is None
step_adopt_blessed_checkout()
assert sealed_steward(blessed_checkout) is None
stamp = json.loads((blessed_checkout / "install-stamp.json").read_text())
assert stamp["updateMechanism"] == "self"
def test_adoption_of_real_checkout_records_full_identity(tmp_path, monkeypatch):
"""A real git checkout gets the full source stamp (commit, base version),
not just the birth-certificate minimum."""
import subprocess
git_env = {"HOME": str(tmp_path), "PATH": os.environ["PATH"]}
home = tmp_path / ".hermes"
root = home / "hermes-agent"
root.mkdir(parents=True)
def git(*args: str) -> None:
subprocess.run(["git", *args], cwd=root, env=git_env, check=True,
capture_output=True)
git("init", "-q")
git("config", "user.name", "Hermes Test")
git("config", "user.email", "hermes@example.invalid")
(root / "tracked").write_text("release\n", encoding="utf-8")
git("add", "tracked")
git("commit", "-qm", "release")
git("tag", "v0.21.4")
monkeypatch.setenv("HERMES_HOME", str(home))
monkeypatch.setenv("HERMES_INSTALL_ROOT", str(root))
result = step_adopt_blessed_checkout()
assert result.get("adopted") == str(root)
stamp = json.loads((root / "install-stamp.json").read_text())
assert stamp["updateMechanism"] == "self"
assert stamp["baseVersion"] == "0.21.4"
head = subprocess.run(["git", "rev-parse", "HEAD"], cwd=root, env=git_env,
check=True, capture_output=True, text=True).stdout.strip()
assert stamp["commit"] == head