Files
hermes-agent/tests/agent/test_verification_stop.py
Teknium 8bfa0160f8 fix(agent): drop the unscored-run warning once newer evidence is recorded
Review finding "Warning names an older run": the remembered unscored run
(`pytest || true`) was cleared only by record_terminal_result. After it, a
failed `hermes verify` still left the nudge calling the older command the
"last check run".

The clear moves to _insert_evidence, the boundary both recording paths
share. `hermes verify` usually records from its own process, though, so the
memory also keeps when the run happened and the nudge shows the warning
only when no evidence was recorded after it.
2026-10-24 18:48:01 -07:00

230 lines
8.7 KiB
Python

import json
import subprocess
import sys
import tempfile
from pathlib import Path
import pytest
from agent.verification_evidence import (
mark_workspace_edited,
record_terminal_result,
)
from agent.verification_stop import (
build_verify_on_stop_nudge,
verify_on_stop_enabled,
)
def _node_project(root: Path) -> None:
(root / "package.json").write_text(
json.dumps({"scripts": {"test": "vitest", "lint": "eslint ."}}),
encoding="utf-8",
)
(root / "pnpm-lock.yaml").write_text("", encoding="utf-8")
def _make_project(root: Path) -> None:
root.mkdir()
_node_project(root)
@pytest.fixture(autouse=True)
def _ledger_on(monkeypatch):
"""The ledger is inert unless verify-on-stop is enabled; ``clear_verify_env`` (requested
explicitly, so it runs after this) strips it again for the enabled()-logic tests."""
monkeypatch.setenv("HERMES_VERIFY_ON_STOP", "1")
@pytest.fixture
def clear_verify_env(monkeypatch):
"""Clear every env signal verify_on_stop_enabled consults.
Tests then set only the variable they exercise, mirroring how the CLI/TUI
set HERMES_SESSION_SOURCE and the gateway sets HERMES_SESSION_PLATFORM.
"""
for var in (
"HERMES_VERIFY_ON_STOP",
"HERMES_PLATFORM",
"HERMES_SESSION_PLATFORM",
"HERMES_SESSION_SOURCE",
):
monkeypatch.delenv(var, raising=False)
return monkeypatch
def test_verify_on_stop_env_can_enable(clear_verify_env):
# Env "1" forces ON regardless of surface (here a messaging platform).
clear_verify_env.setenv("HERMES_VERIFY_ON_STOP", "1")
clear_verify_env.setenv("HERMES_SESSION_PLATFORM", "telegram")
assert verify_on_stop_enabled({"agent": {}}) is True
@pytest.mark.parametrize("source", ["cli", "tui", "desktop", "codex", "local"])
def test_verify_on_stop_auto_on_for_interactive_surfaces(clear_verify_env, source):
# Under "auto", CLI/TUI/desktop coding surfaces resolve ON.
clear_verify_env.setenv("HERMES_SESSION_SOURCE", source)
assert verify_on_stop_enabled({"agent": {"verify_on_stop": "auto"}}) is True
def test_verify_on_stop_missing_value_defaults_off(clear_verify_env):
# A missing/unrecognized config value falls back OFF on every surface,
# matching the opt-in DEFAULT_CONFIG default — only an explicit "auto"
# opts into the legacy surface-aware behavior.
clear_verify_env.setenv("HERMES_SESSION_SOURCE", "cli")
assert verify_on_stop_enabled({"agent": {}}) is False
assert verify_on_stop_enabled({"agent": {"verify_on_stop": "bogus"}}) is False
assert verify_on_stop_enabled({}) is False
def test_nudge_checks_all_edited_workspaces(tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
project_a = tmp_path / "a"
project_b = tmp_path / "b"
_make_project(project_a)
_make_project(project_b)
changed_a = str(project_a / "src" / "app.ts")
changed_b = str(project_b / "src" / "app.ts")
record_terminal_result(
command="pnpm test",
cwd=project_a,
session_id="s1",
exit_code=0,
output="green",
)
mark_workspace_edited(session_id="s1", cwd=project_b, paths=[changed_b])
nudge = build_verify_on_stop_nudge(
session_id="s1",
changed_paths=[changed_a, changed_b],
)
assert nudge is not None
assert changed_b in nudge
@pytest.mark.platforms("posix") # Symlinks require elevated privileges on Windows
def test_no_suite_nudge_uses_canonical_temp_dir(tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
project = tmp_path / "project"
project.mkdir()
(project / "package.json").write_text("{}", encoding="utf-8")
real_temp = tmp_path / "real-temp"
real_temp.mkdir()
linked_temp = tmp_path / "linked-temp"
linked_temp.symlink_to(real_temp, target_is_directory=True)
monkeypatch.setattr(tempfile, "gettempdir", lambda: str(linked_temp))
nudge = build_verify_on_stop_nudge(
session_id="s1",
changed_paths=[str(project / "src" / "app.ts")],
)
assert nudge is not None
assert str(real_temp) in nudge
assert str(linked_temp) not in nudge
def test_ad_hoc_pass_satisfies_no_suite_stop_loop(tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
(tmp_path / "package.json").write_text("{}", encoding="utf-8")
changed = str(tmp_path / "src" / "app.ts")
script = Path(tempfile.gettempdir()) / f"hermes-ad-hoc-stop-{tmp_path.name}.py"
script.write_text("print('ok')\n", encoding="utf-8")
try:
record_terminal_result(
command=f"python {script}",
cwd=tmp_path,
session_id="s1",
exit_code=0,
output="ok",
)
finally:
script.unlink(missing_ok=True)
assert build_verify_on_stop_nudge(session_id="s1", changed_paths=[changed]) is None
def test_nudge_attempts_are_bounded(tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
_node_project(tmp_path)
changed = str(tmp_path / "src" / "app.ts")
mark_workspace_edited(session_id="s1", cwd=tmp_path, paths=[changed])
assert build_verify_on_stop_nudge(
session_id="s1",
changed_paths=[changed],
attempts=2,
max_attempts=2,
) is None
def test_piped_verification_run_satisfies_the_guard(tmp_path, monkeypatch):
"""A piped run counts once the shell's PIPESTATUS is recorded with it."""
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
_node_project(tmp_path)
changed = str(tmp_path / "src" / "app.ts")
mark_workspace_edited(session_id="s1", cwd=tmp_path, paths=[changed])
record_terminal_result(
command="pnpm test 2>&1 | tail -20",
cwd=tmp_path,
session_id="s1",
exit_code=0,
output="ok",
pipeline_status=[0, 0],
)
assert build_verify_on_stop_nudge(session_id="s1", changed_paths=[changed]) is None
def test_nudge_explains_only_a_check_run_the_shell_could_not_attribute(tmp_path, monkeypatch):
"""The how-to-run advice names the hidden-status run, and only until a run is scored."""
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
_node_project(tmp_path)
changed = str(tmp_path / "src" / "app.ts")
mark_workspace_edited(session_id="s1", cwd=tmp_path, paths=[changed])
def nudge() -> str:
text = build_verify_on_stop_nudge(session_id="s1", changed_paths=[changed])
assert text is not None
return text
no_run = nudge()
record_terminal_result(command="pnpm test || true", cwd=tmp_path, session_id="s1", exit_code=0)
hidden = nudge()
record_terminal_result(command="pnpm test 2>&1 | tail -5", cwd=tmp_path, session_id="s1",
exit_code=0, pipeline_status=[1, 0])
scored = nudge()
assert "did not count" not in no_run
assert "Your last check run (`pnpm test || true`) did not count" in hidden
assert "did not count" not in scored
def test_nudge_drops_the_unscored_run_warning_once_hermes_verify_records(tmp_path, monkeypatch):
"""``hermes verify`` records from its own process: its newer failure is the latest check."""
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
_node_project(tmp_path)
changed = str(tmp_path / "src" / "app.ts")
mark_workspace_edited(session_id="s1", cwd=tmp_path, paths=[changed])
record_terminal_result(command="pnpm test || true", cwd=tmp_path, session_id="s1", exit_code=0)
subprocess.run(
[sys.executable, "-c", "import sys; from agent.verification_evidence import record_verify_run; "
"record_verify_run(root=sys.argv[1], session_id='s1', ok=False)", str(tmp_path)],
cwd=Path(__file__).resolve().parents[2], check=True,
)
nudge = build_verify_on_stop_nudge(session_id="s1", changed_paths=[changed])
assert nudge is not None
assert "hermes verify" in nudge and "did not count" not in nudge
# ---------------------------------------------------------------------------
# Fix C: documentation/prose edits carry no verifiable behavior and must never
# trip the nudge, even on an unverified workspace.
# ---------------------------------------------------------------------------
def test_mixed_doc_and_code_edit_still_nudges(tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
_node_project(tmp_path)
doc = str(tmp_path / "README.md")
code = str(tmp_path / "src" / "app.ts")
mark_workspace_edited(session_id="s1", cwd=tmp_path, paths=[code])
nudge = build_verify_on_stop_nudge(
session_id="s1", changed_paths=[doc, code]
)
assert nudge is not None
# The doc path is filtered out of the reported set; the code path remains.
assert code in nudge
assert doc not in nudge