Files
hermes-agent/tests/scripts/test_source_driver.py
BearHuddleston 4957e8e4e9 test(install-e2e): model the case fold in the collision fixtures
On APFS and NTFS the two colliding names are one file. The case-collision
fixtures assumed a write to one name leaves its sibling's bytes alone, so
on the macOS lane writing CRLF into the uppercase path also made the
lowercase entry dirty, and the verifier correctly refused it. The same
assumption broke the binary-CRLF and symlink steps in update-ui.test.mjs
on a Mac.

The CRLF cases now come from a real core.autocrlf=true checkout (what
installers through v2026.9.24 got from Git for Windows), with the
uppercase path holding the bytes of the sibling checked out last. On a
folding filesystem the checkout leaves that state by itself; on a
case-sensitive one the fixture copies the sibling's bytes. The binary and
symlink steps assert on the uppercase path's record instead of counting
dirty entries, since the sibling is dirty too where the names fold.

Verifier code is unchanged.
2026-11-28 11:36:23 -06:00

425 lines
21 KiB
Python

"""Cheap source-driver checks: real shells, disposable installs, no installer."""
import hashlib
import json
import os
from pathlib import Path
import runpy
import shutil
import subprocess
import sys
import sysconfig
import pytest
ASSETS = Path(__file__).resolve().parents[1] / "install/e2e-assets"
@pytest.mark.platforms("posix")
def test_shell_selects_the_installed_command_not_the_phase_or_path(tmp_path):
root = tmp_path / "installed source"
legacy = root / "venv/bin/hermes"
published = root / ".hermes/bin/hermes"
for command, label in ((legacy, "legacy"), (published, "published")):
command.parent.mkdir(parents=True)
command.write_text(f'#!/bin/sh\nprintf "{label}:%s\\n" "$*"\n', encoding="utf-8")
command.chmod(0o755)
env = dict(os.environ, INSTALL_DIR=str(root), ASSETS=str(ASSETS), HOME=str(tmp_path))
result = subprocess.run(["bash", "-euc", '''
source "$ASSETS/source-driver.sh"
command=$(source_hermes "$INSTALL_DIR")
"$command" 'literal argument'
rm "$INSTALL_DIR/venv/bin/hermes"
test "$(source_hermes "$INSTALL_DIR")" = "$command"
printf '#!/bin/sh\nexit 23\n' > "$command"
"$command" || test "$?" = 23
rm "$command"
if source_hermes "$INSTALL_DIR"; then exit 91; fi
printf '#!/bin/sh\nprintf legacy\n' > "$INSTALL_DIR/venv/bin/hermes"
chmod +x "$INSTALL_DIR/venv/bin/hermes"
"$(source_hermes "$INSTALL_DIR")"
# A PM checkout cannot hide missing publication behind its old venv.
mkdir -p "$INSTALL_DIR/pm"
printf '{}' > "$INSTALL_DIR/pm/lock.json"
if source_hermes "$INSTALL_DIR"; then exit 92; fi
'''], env=env, cwd=tmp_path, capture_output=True, text=True, timeout=30, check=False)
assert result.returncode == 0, result.stdout + result.stderr
assert result.stdout.splitlines() == ["published:literal argument", "legacy"]
@pytest.mark.platforms("posix")
def test_installer_marker_is_the_only_dirty_state_a_driver_accepts(tmp_path):
root = tmp_path / "installed source"
root.mkdir()
git = ["git", "-C", str(root), "-c", "user.name=t", "-c", "user.email=t@t"]
subprocess.run([*git, "init", "-q"], check=True)
(root / "tracked").write_text("v1\n", encoding="utf-8")
subprocess.run([*git, "add", "tracked"], check=True)
subprocess.run([*git, "commit", "-qm", "base"], check=True)
(root / ".install_method").write_text("git\n", encoding="utf-8")
env = dict(os.environ, INSTALL_DIR=str(root), ASSETS=str(ASSETS), HOME=str(tmp_path))
def accept():
return subprocess.run(["bash", "-euc", 'source "$ASSETS/source-driver.sh"; accept_installer_marker "$INSTALL_DIR"'],
env=env, capture_output=True, text=True, timeout=30, check=False)
assert accept().returncode == 0
assert (root / ".install_method").read_text(encoding="utf-8") == "git\n"
assert subprocess.run([*git, "status", "--porcelain", "--untracked-files=all"],
capture_output=True, text=True, check=True).stdout == ""
(root / "tracked").write_text("edited\n", encoding="utf-8")
refused = accept()
assert refused.returncode != 0 and "M tracked" in refused.stderr
def _init_case_collision_repo(root: Path, *, ignore_case: bool = True, autocrlf: bool = False) -> list[str]:
root.mkdir(parents=True, exist_ok=True)
git = ["git", "-C", str(root), "-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false"]
subprocess.run([*git, "init", "-q"], check=True)
subprocess.run([*git, "config", "core.autocrlf", "true" if autocrlf else "false"], check=True)
subprocess.run([*git, "config", "core.ignorecase", "true" if ignore_case else "false"], check=True)
blobs = {}
for name, body in (
("tracked", b"v1\n"),
("package-lock.json", b'{"lockfileVersion": 3}\n'),
("contributors/emails/agent@Agents-Mac-mini.local", b"skip-agent\n"),
("contributors/emails/agent@agents-Mac-mini.local", b"momomojo\n"),
):
oid = subprocess.run(
[*git, "hash-object", "-w", "--stdin"],
input=body, capture_output=True, check=True,
).stdout.decode("ascii").strip()
blobs[name] = oid
subprocess.run([*git, "update-index", "--add", "--cacheinfo", f"100644,{oid},{name}"], check=True)
subprocess.run([*git, "commit", "-qm", "base with case collision"], check=True)
subprocess.run([*git, "checkout", "--force", "HEAD"], check=True)
# On APFS/NTFS both names are one file, holding the sibling checked out
# last; copying its bytes gives a case-sensitive filesystem the same state.
emails = root / "contributors/emails"
(emails / "agent@Agents-Mac-mini.local").write_bytes((emails / "agent@agents-Mac-mini.local").read_bytes())
return git
@pytest.mark.platforms("posix")
def test_accept_installer_marker_tolerates_only_head_case_collisions(tmp_path):
root = tmp_path / "installed source"
git = _init_case_collision_repo(root, ignore_case=True)
env = dict(os.environ, INSTALL_DIR=str(root), ASSETS=str(ASSETS), HOME=str(tmp_path))
def accept():
return subprocess.run(
["bash", "-euc", 'source "$ASSETS/source-driver.sh"; accept_installer_marker "$INSTALL_DIR"'],
env=env, capture_output=True, text=True, timeout=30, check=False,
)
assert accept().returncode == 0
ls_v = subprocess.run([*git, "ls-files", "-v", "contributors/emails"], capture_output=True, text=True, check=True).stdout
assert all(line.startswith("H ") for line in ls_v.splitlines())
(root / ".install_method").write_text("git\n", encoding="utf-8")
assert accept().returncode == 0
assert (root / ".install_method").read_text(encoding="utf-8") == "git\n"
(root / "contributors/emails/agent@Agents-Mac-mini.local").write_bytes(b"edited\n")
refused = accept()
assert refused.returncode != 0 and "agent@Agents-Mac-mini.local" in refused.stderr
(root / "contributors/emails/agent@Agents-Mac-mini.local").write_bytes(b"momomojo\n")
(root / "tracked").write_text("edited\n", encoding="utf-8")
refused = accept()
assert refused.returncode != 0 and "M tracked" in refused.stderr
(root / "tracked").write_text("v1\n", encoding="utf-8")
(root / "contributors/emails/agent@Agents-Mac-mini.local").chmod(0o755)
assert accept().returncode != 0
(root / "contributors/emails/agent@Agents-Mac-mini.local").chmod(0o644)
subprocess.run([*git, "-c", "core.ignorecase=false", "add", "contributors/emails/agent@Agents-Mac-mini.local"], check=True)
assert accept().returncode != 0
subprocess.run([*git, "reset", "-q", "HEAD"], check=True)
subprocess.run([*git, "config", "core.ignorecase", "false"], check=True)
assert accept().returncode != 0
@pytest.mark.platforms("posix")
def test_accept_installer_marker_tolerates_a_sibling_checked_out_with_crlf(tmp_path):
# Installers through v2026.9.24 cloned under Git for Windows' core.autocrlf=true.
root = tmp_path / "installed source"
git = _init_case_collision_repo(root, autocrlf=True)
status = subprocess.run([*git, "status", "--porcelain"], capture_output=True, text=True, check=True).stdout
assert status == " M contributors/emails/agent@Agents-Mac-mini.local\n"
assert (root / "contributors/emails/agent@Agents-Mac-mini.local").read_bytes() == b"momomojo\r\n"
env = dict(os.environ, INSTALL_DIR=str(root), ASSETS=str(ASSETS), HOME=str(tmp_path))
accepted = subprocess.run(
["bash", "-euc", 'source "$ASSETS/source-driver.sh"; accept_installer_marker "$INSTALL_DIR"'],
env=env, capture_output=True, text=True, timeout=30, check=False,
)
assert accepted.returncode == 0, accepted.stderr
@pytest.mark.platforms("windows", "posix")
def test_powershell_clears_installer_churn_while_tolerating_only_head_case_collisions(tmp_path):
path = os.pathsep.join(p for p in os.get_exec_path() if ".hermes" not in Path(p).parts)
pwsh = shutil.which("pwsh", path=path) or shutil.which("powershell", path=path)
if not pwsh:
if os.name == "nt":
pytest.fail("native Windows acceptance requires PowerShell")
pytest.skip("PowerShell is not available on this host")
node = shutil.which("node", path=path) or shutil.which("node")
git_exe = shutil.which("git", path=path) or shutil.which("git")
assert node and git_exe
root = tmp_path / "installed source"
work = tmp_path / "work"
work.mkdir()
_init_case_collision_repo(root, ignore_case=True)
(root / "contributors/emails/agent@Agents-Mac-mini.local").write_bytes(b"momomojo\r\n")
(root / "package-lock.json").write_text('{"lockfileVersion": 3, "churn": true}\n', encoding="utf-8")
(root / ".install_method").write_text("git\n", encoding="utf-8")
(root / "bin").mkdir()
(root / "bin/hermes.exe").write_bytes(b"MZ")
harness = tmp_path / "churn-probe.ps1"
harness.write_text('''$ErrorActionPreference = 'Stop'
. (Join-Path $env:ASSETS 'source-driver.ps1')
Clear-SourceInstallerChurn `
-InstallDir $env:INSTALL_DIR -WorkRoot $env:WORK_ROOT `
-RealGitExe $env:GIT_EXE -DriverNode $env:NODE_EXE -AssetsDir $env:ASSETS `
-LegacyLaunchers @('? bin/hermes.exe', '? bin/hermes-acp.exe')
if ((Get-Content -LiteralPath (Join-Path $env:INSTALL_DIR 'package-lock.json') -Raw) -match 'churn') {
throw 'package-lock.json churn was not restored'
}
$colliding = Join-Path $env:INSTALL_DIR 'contributors/emails/agent@Agents-Mac-mini.local'
[System.IO.File]::WriteAllText($colliding, "edited`n", (New-Object System.Text.UTF8Encoding $false))
$refused = $false
try {
Clear-SourceInstallerChurn `
-InstallDir $env:INSTALL_DIR -WorkRoot $env:WORK_ROOT `
-RealGitExe $env:GIT_EXE -DriverNode $env:NODE_EXE -AssetsDir $env:ASSETS
} catch { $refused = $true }
if (-not $refused) { throw 'arbitrary edit to colliding path was accepted' }
[System.IO.File]::WriteAllText($colliding, "momomojo`n", (New-Object System.Text.UTF8Encoding $false))
$otherSibling = Join-Path $env:INSTALL_DIR 'contributors/emails/agent@agents-Mac-mini.local'
[System.IO.File]::WriteAllText($otherSibling, "edited`n", (New-Object System.Text.UTF8Encoding $false))
$refused = $false
try {
Clear-SourceInstallerChurn `
-InstallDir $env:INSTALL_DIR -WorkRoot $env:WORK_ROOT `
-RealGitExe $env:GIT_EXE -DriverNode $env:NODE_EXE -AssetsDir $env:ASSETS
} catch { $refused = $true }
if (-not $refused) { throw 'edit to sibling path was accepted' }
Write-Output 'churn verified'
''', encoding="utf-8")
env = dict(
os.environ,
INSTALL_DIR=str(root),
WORK_ROOT=str(work),
ASSETS=str(ASSETS),
GIT_EXE=git_exe,
NODE_EXE=node,
HOME=str(tmp_path),
)
result = subprocess.run(
[pwsh, "-NoProfile", "-NonInteractive", "-File", str(harness)],
env=env, cwd=tmp_path, capture_output=True, text=True, timeout=60, check=False,
)
assert result.returncode == 0, result.stdout + result.stderr
assert result.stdout.strip().splitlines()[-1] == "churn verified"
@pytest.mark.platforms("windows", "posix")
def test_powershell_selects_exact_exe_or_cmd_and_legacy_fallback(tmp_path):
# These are file-selection rules, not Windows execution emulation. Run
# them in real PowerShell on any host that supplies it (native CI included).
path = os.pathsep.join(p for p in os.get_exec_path() if ".hermes" not in Path(p).parts)
pwsh = shutil.which("pwsh", path=path) or shutil.which("powershell", path=path)
if not pwsh:
if os.name == "nt":
pytest.fail("native Windows acceptance requires PowerShell")
pytest.skip("PowerShell is not available on this host")
root = tmp_path / "installed source"
root.mkdir()
harness = tmp_path / "probe.ps1"
harness.write_text('''$ErrorActionPreference = 'Stop'
. (Join-Path $env:ASSETS 'source-driver.ps1')
$root = $env:INSTALL_DIR
$legacy = Join-Path $root 'venv/Scripts/hermes.exe'
$exe = Join-Path $root '.hermes/bin/hermes.exe'
$cmd = Join-Path $root '.hermes/bin/hermes.cmd'
New-Item -ItemType Directory -Path (Split-Path $legacy), (Split-Path $exe) -Force | Out-Null
Set-Content $legacy 'legacy'
if ((Get-SourceHermes $root) -ne $legacy) { throw 'legacy fallback' }
Set-Content $cmd 'cmd'
if ((Get-SourceHermes $root) -ne $cmd) { throw 'published cmd' }
Set-Content $exe 'exe'
if ((Get-SourceHermes $root) -ne $exe) { throw 'exe precedence' }
Remove-Item $legacy
if ((Get-SourceHermes $root) -ne $exe) { throw 'requires legacy venv' }
Remove-Item $exe, $cmd
$refused = $false
try { Get-SourceHermes $root } catch { $refused = $true }
if (-not $refused) { throw 'missing command accepted' }
Set-Content $legacy 'stale legacy'
New-Item -ItemType Directory -Path (Join-Path $root 'pm') | Out-Null
Set-Content (Join-Path $root 'pm/lock.json') '{}'
$refused = $false
try { Get-SourceHermes $root } catch { $refused = $true }
if (-not $refused) { throw 'missing PM launcher accepted' }
Write-Output 'selection verified'
''', encoding="utf-8")
result = subprocess.run([pwsh, "-NoProfile", "-NonInteractive", "-File", str(harness)],
env=dict(os.environ, INSTALL_DIR=str(root), ASSETS=str(ASSETS), HOME=str(tmp_path)),
cwd=tmp_path, capture_output=True, text=True, timeout=30, check=False)
assert result.returncode == 0, result.stdout + result.stderr
assert result.stdout.strip() == "selection verified"
def test_observer_preserves_no_desktop_and_refuses_incomplete_app(tmp_path):
verify = runpy.run_path(str(ASSETS / "source_driver.py"))["verify_products"]
root = tmp_path / "source"
root.mkdir()
verify(root, "absent")
assert list(root.iterdir()) == []
with pytest.raises(RuntimeError, match="desktop"):
verify(root, "present")
release = root / "apps/desktop/release/linux-unpacked"
release.mkdir(parents=True)
(release / "hermes").write_bytes(b"incomplete fixture")
with pytest.raises(RuntimeError, match="desktop"):
verify(root, "absent")
with pytest.raises(RuntimeError, match="desktop"):
verify(root, "present")
assert (release / "hermes").read_bytes() == b"incomplete fixture"
def test_observer_judges_only_the_live_app_not_rollback_copies_beside_it(tmp_path):
# An update from a release whose raw Windows pack kept release/win-unpacked.bak
# (the stage-and-swap promotion never touches it) must be judged on the live
# win-unpacked the product launches; a rollback copy alone is no desktop at all.
driver = runpy.run_path(str(ASSETS / "source_driver.py"))
release = tmp_path / "source/apps/desktop/release"
for name in ("win-unpacked", "win-unpacked.bak", "win-unpacked.previous", "mac-arm64.previous"):
dist = release / name / "resources/app.asar.unpacked/dist"
dist.mkdir(parents=True)
(dist / "index.html").write_text("<html></html>", encoding="utf-8")
(release / name / "Hermes.exe").write_bytes(b"MZ fixture")
assert driver["desktop_outputs"](tmp_path / "source") == [
release / "win-unpacked/resources/app.asar.unpacked/dist"]
driver["verify_products"](tmp_path / "source", "present")
(release / "win-unpacked/Hermes.exe").unlink()
with pytest.raises(RuntimeError, match="desktop executable is missing or empty"):
driver["verify_products"](tmp_path / "source", "present")
shutil.rmtree(release / "win-unpacked")
with pytest.raises(RuntimeError, match="desktop"):
driver["verify_products"](tmp_path / "source", "present")
@pytest.mark.platforms("posix")
@pytest.mark.parametrize("fault,error", [
("uv-lock", "dependency generation is not current"),
("no-desktop", "dependency generation is not current"),
("foreign-launcher", "belongs to another installation"),
("missing-launcher", ""),
("incomplete", "incomplete"),
("web-changed", "web output"), ("web-missing", "web output"),
])
def test_pm_observer_accepts_ready_fixture_and_leaves_failed_fixture_untouched(tmp_path, fault, error):
# This exercises the complete observer process, not a whole install. The
# fixture borrows prepared test dependencies and records real PM input
# stamps / compiler receipts. It never resolves or acquires a package.
repo = ASSETS.parents[2]
root = tmp_path / "source"
for directory in ("pm", "hermes_cli"):
shutil.copytree(repo / directory, root / directory, ignore=shutil.ignore_patterns("__pycache__"))
shutil.copy2(repo / "hermes_constants.py", root / "hermes_constants.py")
(root / "hermes_bootstrap.py").write_text("raise RuntimeError('bootstrap must not run')", encoding="utf-8")
(root / "uv.lock").write_text("fixture locked graph", encoding="utf-8")
home = tmp_path / "home"
store = tmp_path / "store"
env = dict(os.environ, HOME=str(home), HERMES_HOME=str(home), HERMES_RUNTIME_DIR=str(store),
PYTHONDONTWRITEBYTECODE="1", HERMES_DISABLE_LAZY_INSTALLS="1")
env.pop("HERMES_INSTALL_ROOT", None)
path = os.pathsep.join(p for p in os.get_exec_path() if ".hermes" not in Path(p).parts)
node = shutil.which("node", path=path)
assert node
setup = '''import sys
from pathlib import Path
root, store, node, deps = map(Path, sys.argv[1:])
sys.path.insert(0, str(root))
from pm.lock import Facts, Lockfile
from pm.packages import Venv
from pm.environments import install_state_dir, runtime_facts_path, site_packages
from hermes_cli._launchers import ensure_install_launchers
lock = Lockfile(root / 'pm/lock.json')
lock.set_pin('node', 'fixture', {})
lock.save()
for name, executable in [('python', Path(sys.executable)), ('node', node)]:
binary = store / name / 'bin' / ('python3' if name == 'python' else name)
binary.parent.mkdir(parents=True)
binary.symlink_to(executable)
Facts(store / 'facts.json').record(name, 'fixture', name, {}, store)
selected = install_state_dir(root) / 'environments/fixture/venv'
selected.mkdir(parents=True)
(selected / 'pyvenv.cfg').write_text('home = fixture')
site = site_packages(selected)
site.parent.mkdir(parents=True)
site.symlink_to(deps, target_is_directory=True)
Facts(runtime_facts_path(root)).record_state('venv', Venv(root).expected_stamp([]), [], environment=selected)
ensure_install_launchers(root, root / '.hermes/bin')
'''
subprocess.run([sys.executable, "-I", "-B", "-c", setup, str(root), str(store), node,
sysconfig.get_path("purelib")], env=env, cwd=tmp_path, check=True, timeout=60)
build = root / "scripts/build"
build.mkdir(parents=True)
for name in ("freshness.mjs", "frontend-common.mjs"):
shutil.copy2(repo / "scripts/build" / name, build / name)
record = '''import { mkdirSync, writeFileSync } from 'node:fs';
import { buildInputs, recordProduct } from './scripts/build/freshness.mjs';
const source = process.cwd();
for (const [product, out] of [['tui', 'ui-tui/dist'], ['web', 'hermes_cli/web_dist'], ['desktop', 'apps/desktop/release/linux-unpacked/resources/app.asar.unpacked/dist']]) {
mkdirSync(out, { recursive: true }); writeFileSync(out + '/index.html', 'fixture product');
recordProduct({source, product, out, inputs: buildInputs(source, product)});
}
'''
subprocess.run([node, "--input-type=module", "-e", record], cwd=root, env=env, check=True, timeout=30)
(root / "apps/desktop/release/linux-unpacked/hermes").write_bytes(b"fixture executable")
command = [sys.executable, "-B", str(ASSETS / "source_driver.py"), "--root", str(root),
"--launcher", str(root / ".hermes/bin/hermes"), "--desktop", "present"]
if fault == "no-desktop":
shutil.rmtree(root / "apps/desktop")
command[-1] = "absent"
# The passive query must not write even import caches: -I ignores the
# environment's bytecode switch, so the published query enforces it.
def snapshot():
return {p: (hashlib.sha256(p.read_bytes()).hexdigest(), p.stat().st_mtime_ns) for tree in (root, home, store)
for p in tree.rglob("*") if p.is_file()}
before = snapshot()
result = subprocess.run(command, env=env, cwd=tmp_path, capture_output=True, text=True, timeout=60, check=False)
assert result.returncode == 0, result.stdout + result.stderr
assert snapshot() == before
if fault in {"uv-lock", "no-desktop"}:
(root / "uv.lock").write_text("changed graph without preparation", encoding="utf-8")
elif fault == "foreign-launcher":
foreign = tmp_path / "foreign"
shutil.copytree(root, foreign)
from hermes_cli._launchers import mint_launcher
launcher = mint_launcher("hermes", foreign, root / ".hermes/bin", Path(sys.executable), None)
assert launcher is not None
elif fault == "missing-launcher":
(root / ".hermes/bin/hermes").unlink()
elif fault == "incomplete":
(root / ".update-incomplete").write_text("incomplete", encoding="utf-8")
else:
damaged = root / "hermes_cli/web_dist/index.html"
if fault == "web-missing":
damaged.unlink()
else:
damaged.write_bytes(b"damaged")
before = snapshot()
result = subprocess.run(command, env=env, cwd=tmp_path, capture_output=True, text=True, timeout=60, check=False)
assert result.returncode != 0
assert error in result.stderr
assert "bootstrap must not run" not in result.stderr
assert snapshot() == before